UNPKG

@cloudpss/ubjson

Version:

Opinionated UBJSON encoder/decoder for CloudPSS.

47 lines (43 loc) 1.3 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() { resetDecoderEnv(); resetEncoderEnv(); resetEncoder(); } /** * 输入转为数字数组以便比较 * @param {[ArrayBuffer] | [Uint8Array] | (number | string)[]} args 输入 * @returns {number[]} 数字数组 */ export function toArray(...args) { if (args[0] instanceof ArrayBuffer) { return Array.from(new Uint8Array(args[0])); } if (args[0] instanceof Uint8Array) { return Array.from(args[0]); } return args.map((x) => (typeof x == 'number' ? x : /** @type {string} */ (x).charCodeAt(0))); } /** * 将数字或字符串转换为 Uint8Array * @param {...string | number} args 数字或 char 的数组 * @returns {Uint8Array} Uint8Array */ export function toBuffer(...args) { 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); }