Add stringToArrayBuffer
utility
This way we can avoid an extra dependency.
This commit is contained in:
parent
232e906650
commit
70cdd2b350
1 changed files with 11 additions and 0 deletions
11
js/modules/string_to_array_buffer.js
Normal file
11
js/modules/string_to_array_buffer.js
Normal 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;
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue