UNPKG

@clynn-fe/akfe-editor-jsonc

Version:

JSON Compress by using a map to reduce the size of keys and using gzip

111 lines 3.67 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const pako_1 = __importDefault(require("pako")); const akfe_editor_jsonf_1 = __importDefault(require("@clynn-fe/akfe-editor-jsonf")); const utils_1 = require("./utils"); let _nCode = -1; /** * Returns the string using an array of ASCII values */ const getSpecialKey = (aKeys) => String.fromCharCode(...aKeys); /** * Traverse all the objects looking for keys and set an array with the new keys */ const getKeys = (json, aKeys) => { if (Array.isArray(json)) { aKeys = aKeys.concat(...json.map((item) => (0, utils_1.isObject)(item) || Array.isArray(item) ? (0, utils_1.unique)(getKeys(item, aKeys)) : [])); } else { for (const sKey in json) { if (Object.prototype.hasOwnProperty.call(json, sKey)) { const oItem = json[sKey]; if ((0, utils_1.isObject)(oItem) || Array.isArray(oItem)) { aKeys = aKeys.concat((0, utils_1.unique)(getKeys(oItem, aKeys))); } if (aKeys.every(key => key[1] !== sKey)) { _nCode += 1; const aKey = []; aKey.push(getSpecialKey((0, utils_1.numberToKey)(_nCode)), sKey); aKeys.push(aKey); } } } } return aKeys; }; /** * Method to compress array objects */ const compressArray = (json, aKeys) => json.map(item => compress(item, aKeys)); /** * Method to compress anything but array */ const compressOther = (json, aKeys) => { aKeys = getKeys(json, aKeys); aKeys = (0, utils_1.unique)(aKeys); const oKeys = (0, utils_1.biDimensionalArrayToObject)(aKeys); const oppositeOKeys = Object.entries(oKeys).reduce((prev, [key, value]) => Object.assign(prev, { [value]: key }), {}); return Object.assign((0, utils_1.transformKeys)(json, oppositeOKeys), { _: oKeys }); }; /** * Method to decompress array objects */ const decompressArray = (json) => json.map(item => decompress(item)); /** * Method to decompress anything but array */ const decompressOther = (jsonCopy) => { const oKeys = JSON.parse(JSON.stringify(jsonCopy._)); delete jsonCopy._; return (0, utils_1.transformKeys)(jsonCopy, oKeys); }; /** * Compress a RAW JSON */ const compress = (json, optKeys) => { !optKeys && (_nCode = -1); const aKeys = optKeys || []; return akfe_editor_jsonf_1.default.stringify(Array.isArray(json) ? compressArray(json, aKeys) : compressOther(json, aKeys)); }; /** * Decompress a compressed JSON */ const decompress = (json) => { const data = akfe_editor_jsonf_1.default.parse(json); return Array.isArray(data) ? decompressArray(data) : decompressOther(data); }; /** * Use LZString to get the compressed string. * @param json * @param bCompress * @returns {String} */ const pack = (json, bCompress) => pako_1.default.gzip(globalThis.encodeURIComponent(bCompress ? compress(json) : akfe_editor_jsonf_1.default.stringify(json)), { level: 9, to: 'string' }); /** * Returns the JSON object from the LZW string */ const unpack = (gzipped, bDecompress) => { const str = globalThis.decodeURIComponent(pako_1.default.ungzip(gzipped, { to: 'string' })); return bDecompress ? decompress(str) : akfe_editor_jsonf_1.default.parse(str); }; exports.default = Object.freeze({ compress, decompress, pack, unpack }); //# sourceMappingURL=index.js.map