Add stringToArrayBuffer
utility
This way we can avoid an extra dependency.
This commit is contained in:
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;
|
||||
};
|
Reference in New Issue
Block a user