[SUGGESTION]: setFromBase64/Hex `offset` argument
Hi,
I'm playing with this new api, and I thing it could be great to have an offset argument on the setFromBase64/Hex methods:
Issue: on mdn
const uint8Array = new Uint8Array(8);
// Start writing at offset 2
const result = uint8Array.subarray(2).setFromHex("cafed00d");
In my opinion, this feels sub-optimal.
Proposal:
We could mimic the .set method.
interface Uint8Array {
setFromHex(input: string, offset?: number): { read: number; written: number; };
}
const uint8Array = new Uint8Array(8);
uint8Array.setFromHex("cafed00d", 2);
What do you think ?
See https://github.com/tc39/proposal-arraybuffer-base64/pull/33#issuecomment-1853453315 / https://github.com/tc39/proposal-arraybuffer-base64/pull/34. This was discussed but left out to match the very similar TextEncoder.prototype.encodeInto, which doesn't have such a parameter.
I expected it to have an offset parameter to match TypedArray.prototype.set . Isn't that more natural than to align with TextEncoder?
Well, set is directly attached to Uint8Array where TextEncoder.encodeInto is not. So I don't thing we should compare or align both . Moreover the necessity to perform a trick on TextEncoder.encodeInto feels... wrong.
Writing data at specific a position is a common practice with encoders/decoders, and I think we should keep simplicity and efficiency when possible.
For the record, I’m sympathetic to this intuition. There’s no world where working around this limitation with a subarray allocation would be faster than passing an offset. I like having the option.