Skip to content

Commit bed2b46

Browse files
committed
feat: add hex-string {en,de}code
1 parent 60d5533 commit bed2b46

File tree

8 files changed

+41
-21
lines changed

8 files changed

+41
-21
lines changed

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
"module": "./esm/index.js",
99
"types": "./esm/index.d.ts",
1010
"type": "module",
11-
"files": ["esm", "src"],
11+
"files": [
12+
"esm",
13+
"src"
14+
],
1215
"scripts": {
1316
"build": "tsc -p ./tsconfig.json"
1417
},
1518
"devDependencies": {
1619
"@types/lodash-es": "^4.17.6",
1720
"prettier": "^2.5.1",
18-
"typescript": "^4.7.0-dev.20220223"
21+
"typescript": "^4.7.0-dev.20220223",
22+
"user-agent-data-types": "^0.2.0"
1923
},
2024
"dependencies": {
2125
"lodash-es": "^4.17.21"

pnpm-lock.yaml

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/media/file.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'user-agent-data-types'
2+
13
export function formatFileSize(input = 0, si = isMacPlatform(), fractionDigits = 1) {
24
if (input === 0 || Number.isNaN(input)) {
35
return '0 B'
@@ -13,7 +15,7 @@ export function formatFileSize(input = 0, si = isMacPlatform(), fractionDigits =
1315
/** @internal */
1416
function isMacPlatform() {
1517
try {
16-
return /^Mac/.test(navigator.platform)
18+
return /^Mac/.test(navigator.userAgentData?.platform ?? navigator.platform)
1719
} catch {
1820
return false
1921
}

src/web/blob.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
export const blobToDataURL = factory('readAsDataURL')
2-
export const blobToText = factory('readAsText')
1+
export const blobToDataURL = factory<string>('DataURL')
2+
export const blobToText = factory<string>('Text')
33

4-
interface Methods {
5-
readAsDataURL: string
6-
readAsText: string
7-
}
8-
9-
function factory<T extends keyof Methods>(method: T) {
4+
function factory<T extends string | ArrayBuffer>(method: 'ArrayBuffer' | 'BinaryString' | 'DataURL' | 'Text') {
105
return (blob: Blob) => {
11-
return new Promise<Methods[T]>((resolve, reject) => {
6+
return new Promise<T>((resolve, reject) => {
127
const reader = new FileReader()
138
reader.addEventListener('error', () => {
149
reject(reader.error)
1510
})
1611
reader.addEventListener('load', () => {
17-
resolve(reader.result as Methods[T])
12+
resolve(reader.result as T)
1813
})
19-
reader[method](blob)
14+
reader[`readAs${method}`](blob)
2015
})
2116
}
2217
}

src/web/cookie.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
export function getCookieValue(name: string): string | undefined {
2-
const { cookie } = document
1+
export function getCookieValue(name: string, cookie = document.cookie): string | undefined {
32
const start = cookie.indexOf(`${name}=`)
43
if (start === -1) return
54
let end: number | undefined = cookie.indexOf('; ', start)

src/web/encode-text-arraybuffer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ export function encodeText(input: string) {
22
return new TextEncoder().encode(input)
33
}
44

5-
export function decodeText(input: ArrayBuffer) {
6-
return new TextDecoder().decode(input)
5+
export function decodeText(input: Iterable<number>) {
6+
return new TextDecoder().decode(Uint8Array.from(input))
77
}
88

99
export function decodeArrayBuffer(input: string) {
@@ -15,9 +15,9 @@ export function decodeArrayBuffer(input: string) {
1515
return buffer.buffer
1616
}
1717

18-
export function encodeArrayBuffer(input: ArrayBuffer) {
18+
export function encodeArrayBuffer(input: Iterable<number>) {
1919
let encoded = ''
20-
for (const code of new Uint8Array(input)) {
20+
for (const code of Uint8Array.from(input)) {
2121
encoded += String.fromCharCode(code)
2222
}
2323
return btoa(encoded)

src/web/hex-string.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function encodeHexString(input: Uint8Array) {
2+
return [...input].map((value) => (value < 10 ? '0' : '') + value.toString(16)).join('')
3+
}
4+
5+
export function decodeHexString(input: string) {
6+
const values: number[] = []
7+
for (let index = 0; index < input.length; index += 2) {
8+
const value = Number.parseInt(input.slice(index, index + 2), 16)
9+
if (Number.isNaN(value)) throw new Error('The string not is hex string')
10+
values.push(value)
11+
}
12+
return Uint8Array.from(values)
13+
}

src/web/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export * from './cookie.js'
55
export * from './delay.js'
66
export * from './document.readyState.js'
77
export * from './encode-text-arraybuffer.js'
8+
export * from './hex-string.js'
89
export * from './timeout.js'

0 commit comments

Comments
 (0)