UNPKG

@cloudpss/ubjson

Version:

48 lines (44 loc) 1.37 kB
import { resetEnv as resetDecoderEnv } from '../dist/helper/string-decoder.js'; import { resetEnv as resetEncoderEnv } from '../dist/helper/string-encoder.js'; import { resetEncoder } from '../dist/encoder.js'; import '../dist/helper/constants.js'; import '../dist/options.js'; /** * 重设所有环境 */ export function resetEnv(): void { resetDecoderEnv(); resetEncoderEnv(); resetEncoder(); } /** * 输入转为数字数组以便比较 * @param args 输入 * @returns 数字数组 */ export function toArray(...args: [ArrayBuffer] | [Uint8Array] | Array<number | string>): number[] { if (args[0] instanceof ArrayBuffer) { return Array.from(new Uint8Array(args[0])); } if (args[0] instanceof Uint8Array) { return Array.from(args[0]); } // eslint-disable-next-line unicorn/prefer-code-point return args.map((x) => (typeof x == 'number' ? x : (x as string).charCodeAt(0))); } /** * 将数字或字符串转换为 Uint8Array * @param args 数字或 char 的数组 * @returns Uint8Array */ export function toBuffer(...args: Array<string | number>): Uint8Array<ArrayBuffer> { const data = []; for (const x of args) { if (typeof x == 'number') { data.push(x); } else { data.push(...Buffer.from(x, 'ascii')); } } return Uint8Array.from(data); }