Why does hex()
implementation do not work as expected but the hex2()
works fine.
JavaScript
x
13
13
1
class Bytes extends Uint8Array {
2
hex() {
3
return this.map(x => x.toString(16).padStart(2, "0")).join('');
4
}
5
6
hex2() {
7
return [this].map(x => x.toString(16).padStart(2, "0")).join('');
8
}
9
}
10
11
const a = new Bytes([1, 2, 30]);
12
console.log(a.hex()); // 120
13
console.log(a.hex2()); // 01021e
Advertisement
Answer
All “legit” .map
methods will return an object of the same interface than the one on which it exists.
So on a TypedArray, it will return a TypedArray.
JavaScript
1
2
1
const arr = new Uint8Array();
2
console.log( arr.map( () => {} ) instanceof Uint8Array );
But a TypedArray can’t hold Strings, only numbers, and for the Uint8Array Typed Array, only integer numbers in the range 0 to 255.
So Uint8Array.map( fnThatReturnsStrings )
will try to cast all the return values to numbers in that range.
"01"
=> 1
"02"
=> 2
"1e"
=> NaN
=> 0