@@ -3,51 +3,57 @@ import { fromTypedArray } from './array.js'
33
44const { Buffer } = globalThis // Buffer is optional, only used when native
55const haveNativeBuffer = Buffer && ! Buffer . TYPED_ARRAY_SUPPORT
6+ const { toHex : webHex } = Uint8Array . prototype // Modern engines have this
67
78let hexArray
89let dehexArray
910
1011export function toHex ( arr ) {
1112 assertTypedArray ( arr )
12- const u8 =
13- arr instanceof Uint8Array ? arr : new Uint8Array ( arr . buffer , arr . byteOffset , arr . byteLength )
14- if ( Uint8Array . prototype . toHex && u8 . toHex === Uint8Array . prototype . toHex ) return u8 . toHex ( )
13+ if ( ! ( arr instanceof Uint8Array ) ) arr = new Uint8Array ( arr . buffer , arr . byteOffset , arr . byteLength )
14+ if ( webHex && arr . toHex === webHex ) return arr . toHex ( )
1515 if ( haveNativeBuffer ) {
1616 if ( arr . constructor === Buffer && Buffer . isBuffer ( arr ) ) return arr . toString ( 'hex' )
1717 return Buffer . from ( arr . buffer , arr . byteOffset , arr . byteLength ) . toString ( 'hex' )
1818 }
1919
2020 if ( ! hexArray ) hexArray = Array . from ( { length : 256 } , ( _ , i ) => i . toString ( 16 ) . padStart ( 2 , '0' ) )
2121 let out = ''
22- for ( let i = 0 ; i < arr . length ; i ++ ) out += hexArray [ u8 [ i ] ]
22+ for ( let i = 0 ; i < arr . length ; i ++ ) out += hexArray [ arr [ i ] ]
2323 return out
2424}
2525
2626// Unlike Buffer.from(), throws on invalid input
27- export function fromHex ( str , format = 'uint8' ) {
28- if ( Uint8Array . fromHex ) return fromTypedArray ( Uint8Array . fromHex ( str ) , format )
29- if ( typeof str !== 'string' ) throw new TypeError ( 'Input is not a string' )
30- assert ( str . length % 2 === 0 , 'Input is not a hex string' )
31- if ( haveNativeBuffer ) {
32- assert ( ! / [ ^ 0 - 9 a - f ] / iu. test ( str ) , 'Input is not a hex string' )
33- return fromTypedArray ( Buffer . from ( str , 'hex' ) , format )
34- }
27+ let fromHex
28+ if ( Uint8Array . fromHex ) {
29+ fromHex = ( str , format = 'uint8' ) => fromTypedArray ( Uint8Array . fromHex ( str ) , format )
30+ } else {
31+ fromHex = ( str , format = 'uint8' ) => {
32+ if ( typeof str !== 'string' ) throw new TypeError ( 'Input is not a string' )
33+ assert ( str . length % 2 === 0 , 'Input is not a hex string' )
34+ if ( haveNativeBuffer ) {
35+ assert ( ! / [ ^ 0 - 9 a - f ] / iu. test ( str ) , 'Input is not a hex string' )
36+ return fromTypedArray ( Buffer . from ( str , 'hex' ) , format )
37+ }
3538
36- if ( ! dehexArray ) {
37- dehexArray = new Array ( 103 ) // f is 102
38- for ( let i = 0 ; i < 16 ; i ++ ) {
39- const s = i . toString ( 16 )
40- dehexArray [ s . charCodeAt ( 0 ) ] = dehexArray [ s . toUpperCase ( ) . charCodeAt ( 0 ) ] = i
39+ if ( ! dehexArray ) {
40+ dehexArray = new Array ( 103 ) // f is 102
41+ for ( let i = 0 ; i < 16 ; i ++ ) {
42+ const s = i . toString ( 16 )
43+ dehexArray [ s . charCodeAt ( 0 ) ] = dehexArray [ s . toUpperCase ( ) . charCodeAt ( 0 ) ] = i
44+ }
4145 }
42- }
4346
44- const arr = new Uint8Array ( str . length / 2 )
45- let j = 0
46- for ( let i = 0 ; i < arr . length ; i ++ ) {
47- const a = dehexArray [ str . charCodeAt ( j ++ ) ] * 16 + dehexArray [ str . charCodeAt ( j ++ ) ]
48- if ( ! a && Number . isNaN ( a ) ) throw new Error ( 'Input is not a hex string' )
49- arr [ i ] = a
50- }
47+ const arr = new Uint8Array ( str . length / 2 )
48+ let j = 0
49+ for ( let i = 0 ; i < arr . length ; i ++ ) {
50+ const a = dehexArray [ str . charCodeAt ( j ++ ) ] * 16 + dehexArray [ str . charCodeAt ( j ++ ) ]
51+ if ( ! a && Number . isNaN ( a ) ) throw new Error ( 'Input is not a hex string' )
52+ arr [ i ] = a
53+ }
5154
52- return fromTypedArray ( arr , format )
55+ return fromTypedArray ( arr , format )
56+ }
5357}
58+
59+ export { fromHex }
0 commit comments