UNPKG

@nodescript/stdlib

Version:
37 lines (36 loc) 943 B
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}`); } };