UNPKG

@graffy/common

Version:

Common libraries that used by various Graffy modules.

155 lines (125 loc) 4.56 kB
function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } it = o[Symbol.iterator](); return it.next.bind(it); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } import { encode as encodeString, decode as decodeString } from './string.js'; import { encode as encodeNumber, decode as decodeNumber } from './number.js'; import { encode as encodeB64, decode as decodeB64 } from './base64.js'; var END = 0; var NULL = 1; var FALSE = 2; var TRUE = 3; var NUM = 4; var STR = 5; export var ARR = 6; export var OBJ = 7; function encodeArray(array) { return [ARR].concat(array.flatMap(function (value) { return encodeParts(value); }), [END]); } function encodeObject(object) { var keys = Object.keys(object).sort(); return [OBJ].concat(keys.flatMap(function (key) { return [STR, encodeString(key), END].concat(encodeParts(object[key])); }), [END]); } function encodeParts(value) { if (value === null) return [NULL]; if (value === false) return [FALSE]; if (value === true) return [TRUE]; if (typeof value === 'number') return [NUM, encodeNumber(value)]; if (typeof value === 'string') return [STR, encodeString(value), END]; if (Array.isArray(value)) return encodeArray(value); if (typeof value === 'object') return encodeObject(value); return [NULL]; } export function encode(value) { var parts = encodeParts(value); while (parts[parts.length - 1] === END) { parts.pop(); } var length = parts.reduce(function (sum, part) { return sum + (typeof part === 'number' ? 1 : part.length); }, 0); var buffer = new Uint8Array(length); var i = 0; for (var _iterator = _createForOfIteratorHelperLoose(parts), _step; !(_step = _iterator()).done;) { var part = _step.value; if (typeof part === 'number') { buffer[i] = part; i++; } else { buffer.set(part, i); i += part.length; } } return encodeB64(buffer); } var nextKey = new WeakMap(); export function decode(key) { var i = 0; var buffer = decodeB64(key, 0); /** @type {Array<{ [prop: string]: any }|Array>} */ var stack = [[]]; function readString() { var start = i; while (i < buffer.length && buffer[i] !== END) { i++; } var str = decodeString(buffer.subarray(start, i)); i++; return str; } function pushToken(type, value) { var current = stack[stack.length - 1]; if (type === ARR || type === OBJ) stack.push(value); if (!current) return; if (Array.isArray(current)) { current.push(value); } else { if (nextKey.has(current)) { current[nextKey.get(current)] = value; nextKey.delete(current); } else { nextKey.set(current, value); } } } function popToken() { stack.pop(); } while (i < buffer.length) { var type = buffer[i]; var start = ++i; switch (type) { case END: popToken(); break; case NULL: pushToken(type, null); break; case FALSE: pushToken(type, false); break; case TRUE: pushToken(type, true); break; case NUM: i += 8; pushToken(type, decodeNumber(buffer.subarray(start, i))); break; case STR: pushToken(type, readString()); break; case ARR: pushToken(type, []); break; case OBJ: pushToken(type, {}); break; default: throw new Error('Invalid byte ' + type + ' at ' + start); } } return stack[0][0]; }