Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/bundlejs-api.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473/** * Based on `@amoutonbrady/lz-string` (https://npmjs.com/@amoutonbrady/lz-string) by @amoutonbrady (https://github.com/amoutonbrady) */
// private propertyexport const keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";export const keyStrUriSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$";export const baseReverseDic: Record<string, Record<string, number>> = {};
export function getBaseValue(alphabet: string, character: string) { if (!baseReverseDic[alphabet]) { baseReverseDic[alphabet] = {};
for (let i = 0; i < alphabet.length; i++) { baseReverseDic[alphabet][alphabet.charAt(i)] = i; } }
return baseReverseDic[alphabet][character];}
export function compressToBase64(input: string): string { if (input === null) return ""; const res = _compress(input, 6, (a) => keyStrBase64.charAt(a));
// To produce valid Base64 switch (res.length % 4) { default: // When could this happen ? case 0: return res; case 1: return res + "==="; case 2: return res + "=="; case 3: return res + "="; }}
export function decompressFromBase64(input: string): string | null { if (input === null) return ""; if (input === "") return null; return _decompress(input.length, 32, (index) => getBaseValue(keyStrBase64, input.charAt(index)));}
//compress into a string that is already URI encodedexport function compressToURL(input: string): string { if (input === null) return ""; return _compress(input, 6, (a) => keyStrUriSafe.charAt(a));}
//decompress from an output of decompressFromURLexport function decompressFromURL(input: string): string | null { if (input === null) return ""; if (input === "") return null; input = input.replaceAll(" ", "+");
return _decompress(input.length, 32, (index) => getBaseValue(keyStrUriSafe, input.charAt(index)));}
export function compressToUTF16(input: string) { if (input === null) return ""; return _compress(input, 15, function (a) { return String.fromCharCode(a + 32); }) + " ";}
export function decompressFromUTF16 (compressed: string) { if (compressed === null) return ""; if (compressed === "") return null; return _decompress(compressed.length, 16384, (index) => compressed.charCodeAt(index) - 32);}
export function compress(uncompressed: string): string { return _compress(uncompressed, 16, String.fromCharCode);}
export function decompress(compressed: null | string): string | null { if (compressed === null) return ""; if (compressed === "") return null; return _decompress(compressed.length, 32768, (index) => compressed.charCodeAt(index));}
/** * @internal */export function _compress( uncompressed: null | string, bitsPerChar: number, getCharFromInt: (int: number) => string,) { if (uncompressed === null) return "";
const contextData = []; const contextDictionary: Record<string, number> = {}; const contextDictionaryToCreate: Record<string, boolean> = {};
let i: number; let j: number; let value: number;
let contextC = ""; let contextW = ""; let contextWc = "";
// Compensate for the first entry which should not count let contextEnlargeIn = 2; let contextDictSize = 3; let contextNumBits = 2; let contextDataVal = 0; let contextDataPosition = 0;
for (j = 0; j < uncompressed.length; j += 1) { contextC = uncompressed.charAt(j);
if (!Object.hasOwn(contextDictionary, contextC)) { contextDictionary[contextC] = contextDictSize++; contextDictionaryToCreate[contextC] = true; }
contextWc = contextW + contextC;
if (Object.hasOwn(contextDictionary, contextWc)) { contextW = contextWc; } else { if (Object.hasOwn(contextDictionaryToCreate, contextW)) { if (contextW.charCodeAt(0) < 256) { for (i = 0; i < contextNumBits; i++) { contextDataVal = contextDataVal << 1; if (contextDataPosition === bitsPerChar - 1) { contextDataPosition = 0; contextData.push(getCharFromInt(contextDataVal)); contextDataVal = 0; } else { contextDataPosition++; } } value = contextW.charCodeAt(0); for (i = 0; i < 8; i++) { contextDataVal = (contextDataVal << 1) | (value & 1); if (contextDataPosition === bitsPerChar - 1) { contextDataPosition = 0; contextData.push(getCharFromInt(contextDataVal)); contextDataVal = 0; } else { contextDataPosition++; } value = value >> 1; } } else { value = 1; for (i = 0; i < contextNumBits; i++) { contextDataVal = (contextDataVal << 1) | value; if (contextDataPosition === bitsPerChar - 1) { contextDataPosition = 0; contextData.push(getCharFromInt(contextDataVal)); contextDataVal = 0; } else { contextDataPosition++; } value = 0; } value = contextW.charCodeAt(0); for (i = 0; i < 16; i++) { contextDataVal = (contextDataVal << 1) | (value & 1); if (contextDataPosition === bitsPerChar - 1) { contextDataPosition = 0; contextData.push(getCharFromInt(contextDataVal)); contextDataVal = 0; } else { contextDataPosition++; } value = value >> 1; } } contextEnlargeIn--; if (contextEnlargeIn === 0) { contextEnlargeIn = Math.pow(2, contextNumBits); contextNumBits++; } delete contextDictionaryToCreate[contextW]; } else { value = contextDictionary[contextW]; for (i = 0; i < contextNumBits; i++) { contextDataVal = (contextDataVal << 1) | (value & 1); if (contextDataPosition === bitsPerChar - 1) { contextDataPosition = 0; contextData.push(getCharFromInt(contextDataVal)); contextDataVal = 0; } else { contextDataPosition++; } value = value >> 1; } } contextEnlargeIn--; if (contextEnlargeIn === 0) { contextEnlargeIn = Math.pow(2, contextNumBits); contextNumBits++; } // Add wc to the dictionary. contextDictionary[contextWc] = contextDictSize++; contextW = String(contextC); } }
// Output the code for w. if (contextW !== "") { if (Object.hasOwn(contextDictionaryToCreate, contextW)) { if (contextW.charCodeAt(0) < 256) { for (i = 0; i < contextNumBits; i++) { contextDataVal = contextDataVal << 1; if (contextDataPosition === bitsPerChar - 1) { contextDataPosition = 0; contextData.push(getCharFromInt(contextDataVal)); contextDataVal = 0; } else { contextDataPosition++; } } value = contextW.charCodeAt(0); for (i = 0; i < 8; i++) { contextDataVal = (contextDataVal << 1) | (value & 1); if (contextDataPosition === bitsPerChar - 1) { contextDataPosition = 0; contextData.push(getCharFromInt(contextDataVal)); contextDataVal = 0; } else { contextDataPosition++; } value = value >> 1; } } else { value = 1; for (i = 0; i < contextNumBits; i++) { contextDataVal = (contextDataVal << 1) | value; if (contextDataPosition === bitsPerChar - 1) { contextDataPosition = 0; contextData.push(getCharFromInt(contextDataVal)); contextDataVal = 0; } else { contextDataPosition++; } value = 0; } value = contextW.charCodeAt(0); for (i = 0; i < 16; i++) { contextDataVal = (contextDataVal << 1) | (value & 1); if (contextDataPosition === bitsPerChar - 1) { contextDataPosition = 0; contextData.push(getCharFromInt(contextDataVal)); contextDataVal = 0; } else { contextDataPosition++; } value = value >> 1; } } contextEnlargeIn--; if (contextEnlargeIn === 0) { contextEnlargeIn = Math.pow(2, contextNumBits); contextNumBits++; } delete contextDictionaryToCreate[contextW]; } else { value = contextDictionary[contextW]; for (i = 0; i < contextNumBits; i++) { contextDataVal = (contextDataVal << 1) | (value & 1); if (contextDataPosition === bitsPerChar - 1) { contextDataPosition = 0; contextData.push(getCharFromInt(contextDataVal)); contextDataVal = 0; } else { contextDataPosition++; } value = value >> 1; } } contextEnlargeIn--; if (contextEnlargeIn === 0) { contextEnlargeIn = Math.pow(2, contextNumBits); contextNumBits++; } }
// Mark the end of the stream value = 2;
for (i = 0; i < contextNumBits; i++) { contextDataVal = (contextDataVal << 1) | (value & 1); if (contextDataPosition === bitsPerChar - 1) { contextDataPosition = 0; contextData.push(getCharFromInt(contextDataVal)); contextDataVal = 0; } else contextDataPosition++;
value = value >> 1; }
// Flush the last char while (true) { contextDataVal = contextDataVal << 1; if (contextDataPosition === bitsPerChar - 1) { contextData.push(getCharFromInt(contextDataVal)); break; } else contextDataPosition++; }
return contextData.join("");}
/** * @internal */export function _decompress(length: number, resetValue: number, getNextValue: (index: number) => number) { const dictionary: (string | number)[] = []; let next: number; let enlargeIn = 4; let dictSize = 4; let numBits = 3; let entry = ""; const result = []; let i: number; let w: string | number; let bits: number; let resb: number; let maxpower: number; let power: number; let c: string | number; const data = { val: getNextValue(0), position: resetValue, index: 1 };
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 = String.fromCharCode(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 = String.fromCharCode(bits); break; case 2: return ""; } dictionary[3] = c! as string; 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++] = String.fromCharCode(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++] = String.fromCharCode(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] as string; } else { if (c === dictSize && typeof w === "string") { entry = w + w.charAt(0); } else { return null; } } result.push(entry);
// Add w+entry[0] to the dictionary. dictionary[dictSize++] = w + entry.charAt(0); enlargeIn--;
w = entry;
if (enlargeIn === 0) { enlargeIn = Math.pow(2, numBits); numBits++; } }}