UNPKG

typescript-libraries

Version:
459 lines (458 loc) 19.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TSLZS = void 0; var TSLZS = /** @class */ (function () { function TSLZS() { this.f = String.fromCharCode; this.keyStrBase64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; this.keyStrUriSafe = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$'; this.baseReverseDic = {}; } TSLZS.prototype.getBaseValue = function (alphabet, character) { if (!this.baseReverseDic[alphabet]) { this.baseReverseDic[alphabet] = {}; for (var i = 0; i < alphabet.length; i++) { this.baseReverseDic[alphabet][alphabet.charAt(i)] = i; } } return this.baseReverseDic[alphabet][character]; }; TSLZS.prototype.compressToBase64 = function (input) { var _this = this; if (input == null) return ''; var res = this._compress(input, 6, function (a) { return _this.keyStrBase64.charAt(a); }); switch (res.length % 4) { // To produce valid Base64 default: // When could this happen ? case 0: return res; case 1: return res + '==='; case 2: return res + '=='; case 3: return res + '='; } }; TSLZS.prototype.decompressFromBase64 = function (input) { var _this = this; if (input == null) return ''; if (input == '') return null; return this._decompress(input.length, 32, function (index) { return _this.getBaseValue(_this.keyStrBase64, input.charAt(index)); }); }; TSLZS.prototype.compressToUTF16 = function (input) { var _this = this; if (input == null) return ''; return this._compress(input, 15, function (a) { return _this.f(a + 32); }) + ' '; }; TSLZS.prototype.decompressFromUTF16 = function (compressed) { if (compressed == null) return ''; if (compressed == '') return null; return this._decompress(compressed.length, 16384, function (index) { return compressed.charCodeAt(index) - 32; }); }; TSLZS.prototype.compressToUint8Array = function (uncompressed) { var compressed = this.compress(uncompressed); var buf = new Uint8Array(compressed.length * 2); // 2 bytes per character for (var i = 0, TotalLen = compressed.length; i < TotalLen; i++) { var current_value = compressed.charCodeAt(i); buf[i * 2] = current_value >>> 8; buf[i * 2 + 1] = current_value % 256; } return buf; }; TSLZS.prototype.decompressFromUint8Array = function (compressed) { var _this = this; if (compressed === null || compressed === undefined) { return this.decompress(compressed); } else { var buf = new Array(compressed.length / 2); // 2 bytes per character for (var i = 0, TotalLen = buf.length; i < TotalLen; i++) { buf[i] = compressed[i * 2] * 256 + compressed[i * 2 + 1]; } var result_1 = []; buf.forEach(function (c) { return result_1.push(_this.f(c)); }); return this.decompress(result_1.join('')); } }; TSLZS.prototype.compressToEncodedURIComponent = function (input) { var _this = this; if (input == null) return ''; return this._compress(input, 6, function (a) { return _this.keyStrUriSafe.charAt(a); }); }; TSLZS.prototype.decompressFromEncodedURIComponent = function (input) { var _this = this; if (input == null) return ''; if (input == '') return null; input = input.replace(/ /g, '+'); return this._decompress(input.length, 32, function (index) { return _this.getBaseValue(_this.keyStrUriSafe, input.charAt(index)); }); }; TSLZS.prototype.compress = function (uncompressed) { var _this = this; return this._compress(uncompressed, 16, function (a) { return _this.f(a); }); }; TSLZS.prototype._compress = function (uncompressed, bitsPerChar, getCharFromInt) { if (uncompressed == null) return ''; var context_dictionary = {}, context_dictionaryToCreate = {}, context_data = []; var i, value, context_c = '', context_wc = '', context_w = '', context_enlargeIn = 2, // Compensate for the first entry which should not count context_dictSize = 3, context_numBits = 2, context_data_val = 0, context_data_position = 0, ii; for (ii = 0; ii < uncompressed.length; ii += 1) { context_c = uncompressed.charAt(ii); if (!Object.prototype.hasOwnProperty.call(context_dictionary, context_c)) { context_dictionary[context_c] = context_dictSize++; context_dictionaryToCreate[context_c] = true; } context_wc = context_w + context_c; if (Object.prototype.hasOwnProperty.call(context_dictionary, context_wc)) { context_w = context_wc; } else { if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate, context_w)) { if (context_w.charCodeAt(0) < 256) { for (i = 0; i < context_numBits; i++) { context_data_val = (context_data_val << 1); if (context_data_position == bitsPerChar - 1) { context_data_position = 0; context_data.push(getCharFromInt(context_data_val)); context_data_val = 0; } else { context_data_position++; } } value = context_w.charCodeAt(0); for (i = 0; i < 8; i++) { context_data_val = (context_data_val << 1) | (value & 1); if (context_data_position == bitsPerChar - 1) { context_data_position = 0; context_data.push(getCharFromInt(context_data_val)); context_data_val = 0; } else { context_data_position++; } value = value >> 1; } } else { value = 1; for (i = 0; i < context_numBits; i++) { context_data_val = (context_data_val << 1) | value; if (context_data_position == bitsPerChar - 1) { context_data_position = 0; context_data.push(getCharFromInt(context_data_val)); context_data_val = 0; } else { context_data_position++; } value = 0; } value = context_w.charCodeAt(0); for (i = 0; i < 16; i++) { context_data_val = (context_data_val << 1) | (value & 1); if (context_data_position == bitsPerChar - 1) { context_data_position = 0; context_data.push(getCharFromInt(context_data_val)); context_data_val = 0; } else { context_data_position++; } value = value >> 1; } } context_enlargeIn--; if (context_enlargeIn == 0) { context_enlargeIn = Math.pow(2, context_numBits); context_numBits++; } delete context_dictionaryToCreate[context_w]; } else { value = context_dictionary[context_w]; for (i = 0; i < context_numBits; i++) { context_data_val = (context_data_val << 1) | (value & 1); if (context_data_position == bitsPerChar - 1) { context_data_position = 0; context_data.push(getCharFromInt(context_data_val)); context_data_val = 0; } else { context_data_position++; } value = value >> 1; } } context_enlargeIn--; if (context_enlargeIn == 0) { context_enlargeIn = Math.pow(2, context_numBits); context_numBits++; } context_dictionary[context_wc] = context_dictSize++; context_w = String(context_c); } } if (context_w !== '') { if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate, context_w)) { if (context_w.charCodeAt(0) < 256) { for (i = 0; i < context_numBits; i++) { context_data_val = (context_data_val << 1); if (context_data_position == bitsPerChar - 1) { context_data_position = 0; context_data.push(getCharFromInt(context_data_val)); context_data_val = 0; } else { context_data_position++; } } value = context_w.charCodeAt(0); for (i = 0; i < 8; i++) { context_data_val = (context_data_val << 1) | (value & 1); if (context_data_position == bitsPerChar - 1) { context_data_position = 0; context_data.push(getCharFromInt(context_data_val)); context_data_val = 0; } else { context_data_position++; } value = value >> 1; } } else { value = 1; for (i = 0; i < context_numBits; i++) { context_data_val = (context_data_val << 1) | value; if (context_data_position == bitsPerChar - 1) { context_data_position = 0; context_data.push(getCharFromInt(context_data_val)); context_data_val = 0; } else { context_data_position++; } value = 0; } value = context_w.charCodeAt(0); for (i = 0; i < 16; i++) { context_data_val = (context_data_val << 1) | (value & 1); if (context_data_position == bitsPerChar - 1) { context_data_position = 0; context_data.push(getCharFromInt(context_data_val)); context_data_val = 0; } else { context_data_position++; } value = value >> 1; } } context_enlargeIn--; if (context_enlargeIn == 0) { context_enlargeIn = Math.pow(2, context_numBits); context_numBits++; } delete context_dictionaryToCreate[context_w]; } else { value = context_dictionary[context_w]; for (i = 0; i < context_numBits; i++) { context_data_val = (context_data_val << 1) | (value & 1); if (context_data_position == bitsPerChar - 1) { context_data_position = 0; context_data.push(getCharFromInt(context_data_val)); context_data_val = 0; } else { context_data_position++; } value = value >> 1; } } context_enlargeIn--; if (context_enlargeIn == 0) { context_enlargeIn = Math.pow(2, context_numBits); context_numBits++; } } value = 2; for (i = 0; i < context_numBits; i++) { context_data_val = (context_data_val << 1) | (value & 1); if (context_data_position == bitsPerChar - 1) { context_data_position = 0; context_data.push(getCharFromInt(context_data_val)); context_data_val = 0; } else { context_data_position++; } value = value >> 1; } while (true) { context_data_val = (context_data_val << 1); if (context_data_position == bitsPerChar - 1) { context_data.push(getCharFromInt(context_data_val)); break; } else context_data_position++; } return context_data.join(''); }; TSLZS.prototype.decompress = function (compressed) { if (compressed == null) return ''; if (compressed == '') return null; return this._decompress(compressed.length, 32768, function (index) { return compressed.charCodeAt(index); }); }; TSLZS.prototype._decompress = function (length, resetValue, getNextValue) { var dictionary = [], result = [], data = { val: getNextValue(0), position: resetValue, index: 1 }; var next, enlargeIn = 4, dictSize = 4, numBits = 3, entry = '', i, w, bits, resb, maxpower, power, c; for (i = 0; i < 3; i += 1) { dictionary[i] = i; } bits = 0; maxpower = Math.pow(2, 2); power = 1; while (power != maxpower) { resb = data.val & data.position; data.position >>= 1; if (data.position == 0) { data.position = resetValue; data.val = getNextValue(data.index++); } bits |= (resb > 0 ? 1 : 0) * power; power <<= 1; } switch (next = bits) { case 0: bits = 0; maxpower = Math.pow(2, 8); power = 1; while (power != maxpower) { resb = data.val & data.position; data.position >>= 1; if (data.position == 0) { data.position = resetValue; data.val = getNextValue(data.index++); } bits |= (resb > 0 ? 1 : 0) * power; power <<= 1; } c = this.f(bits); break; case 1: bits = 0; maxpower = Math.pow(2, 16); power = 1; while (power != maxpower) { resb = data.val & data.position; data.position >>= 1; if (data.position == 0) { data.position = resetValue; data.val = getNextValue(data.index++); } bits |= (resb > 0 ? 1 : 0) * power; power <<= 1; } c = this.f(bits); break; case 2: return ''; } dictionary[3] = c; w = c; result.push(c); while (true) { if (data.index > length) { return ''; } bits = 0; maxpower = Math.pow(2, numBits); power = 1; while (power != maxpower) { resb = data.val & data.position; data.position >>= 1; if (data.position == 0) { data.position = resetValue; data.val = getNextValue(data.index++); } bits |= (resb > 0 ? 1 : 0) * power; power <<= 1; } switch (c = bits) { case 0: bits = 0; maxpower = Math.pow(2, 8); power = 1; while (power != maxpower) { resb = data.val & data.position; data.position >>= 1; if (data.position == 0) { data.position = resetValue; data.val = getNextValue(data.index++); } bits |= (resb > 0 ? 1 : 0) * power; power <<= 1; } dictionary[dictSize++] = this.f(bits); c = dictSize - 1; enlargeIn--; break; case 1: bits = 0; maxpower = Math.pow(2, 16); power = 1; while (power != maxpower) { resb = data.val & data.position; data.position >>= 1; if (data.position == 0) { data.position = resetValue; data.val = getNextValue(data.index++); } bits |= (resb > 0 ? 1 : 0) * power; power <<= 1; } dictionary[dictSize++] = this.f(bits); c = dictSize - 1; enlargeIn--; break; case 2: return result.join(''); } if (enlargeIn == 0) { enlargeIn = Math.pow(2, numBits); numBits++; } if (dictionary[c]) { entry = dictionary[c]; } else { if (c === dictSize) { entry = w + w.charAt(0); } else { return null; } } result.push(entry); dictionary[dictSize++] = w + entry.charAt(0); enlargeIn--; w = entry; if (enlargeIn == 0) { enlargeIn = Math.pow(2, numBits); numBits++; } } }; return TSLZS; }()); exports.TSLZS = TSLZS;