Add stringToArrayBuffer utility

This way we can avoid an extra dependency.
This commit is contained in:
Daniel Gasienica 2018-03-23 15:49:08 -04:00
parent 232e906650
commit 70cdd2b350

View file

@ -0,0 +1,11 @@
exports.stringToArrayBuffer = (string) => {
if (typeof string !== 'string') {
throw new TypeError('`string` must be a string');
}
const array = new Uint8Array(string.length);
for (let i = 0; i < string.length; i += 1) {
array[i] = string.charCodeAt(i);
}
return array.buffer;
};