@nodescript/stdlib
Version:
Standard Node Definitions
37 lines (36 loc) • 943 B
JavaScript
import { binaryStringToBuffer } from '@nodescript/binary-utils';
export const module = {
version: '1.1.4',
moduleName: 'String / To Bytes',
description: 'Converts a string into an ArrayBuffer.',
keywords: ['buffer'],
params: {
string: {
schema: {
type: 'string',
},
},
encoding: {
schema: {
type: 'string',
enum: ['utf-8', 'binary'],
default: 'utf-8',
},
}
},
result: {
schema: { type: 'any' },
},
};
export const compute = params => {
const { string, encoding } = params;
switch (encoding) {
case 'binary':
return binaryStringToBuffer(string);
case 'utf-8': {
return new TextEncoder().encode(string).buffer;
}
default:
throw new Error(`Unsupported encoding: ${encoding}`);
}
};