From 6f3fe1c4ccde6730f192a981d634e3b71f5a517a Mon Sep 17 00:00:00 2001 From: Ryc O'Chet Date: Sun, 14 Jan 2024 20:21:38 +0000 Subject: [PATCH] Split as true typescript project --- .eslintignore | 3 + .eslintrc | 6 +- .prettierrc | 8 +- README.md | 2 +- package-lock.json | 515 ------------------ package.json | 5 +- src/UTF16/compressToUTF16.ts | 7 + src/UTF16/decompressFromUTF16.ts | 8 + src/UTF16/index.ts | 2 + src/Uint8Array/compressToUint8Array.ts | 14 + src/Uint8Array/decompressFromUint8Array.ts | 21 + src/Uint8Array/index.ts | 2 + src/_compress.ts | 219 ++++++++ src/_decompress.ts | 166 ++++++ src/{ => base64}/base64-string.ts | 28 +- src/base64/compressToBase64.ts | 22 + src/base64/decompressFromBase64.ts | 10 + src/base64/index.ts | 2 + src/base64/keyStrBase64.ts | 1 + src/encodedURI/compressToEncodedURI.ts | 10 + src/encodedURI/decompressFromEncodedURI.ts | 12 + src/encodedURI/index.ts | 2 + src/encodedURI/keyStrUriSafe.ts | 1 + src/getBaseValue.ts | 14 + src/index.ts | 37 ++ src/interfaces/DecompressionTracker.ts | 5 - src/interfaces/index.ts | 1 - src/main.ts | 605 --------------------- src/stock/compress.ts | 5 + src/stock/decompress.ts | 8 + src/stock/index.ts | 2 + src/types/Dictionary.ts | 1 - src/types/DictionaryCollection.ts | 3 - src/types/PendingDictionary.ts | 1 - src/types/index.ts | 3 - tests/dist.cjs.main.test.ts | 2 +- tests/dist.es.main.test.ts | 2 +- tests/src.main.test.ts | 2 +- tests/testFunctions.ts | 46 +- tests/testValues.ts | 65 +-- tsconfig.json | 15 +- vite.config.ts | 34 +- 42 files changed, 657 insertions(+), 1260 deletions(-) create mode 100644 .eslintignore create mode 100644 src/UTF16/compressToUTF16.ts create mode 100644 src/UTF16/decompressFromUTF16.ts create mode 100644 src/UTF16/index.ts create mode 100644 src/Uint8Array/compressToUint8Array.ts create mode 100644 src/Uint8Array/decompressFromUint8Array.ts create mode 100644 src/Uint8Array/index.ts create mode 100644 src/_compress.ts create mode 100644 src/_decompress.ts rename src/{ => base64}/base64-string.ts (92%) create mode 100644 src/base64/compressToBase64.ts create mode 100644 src/base64/decompressFromBase64.ts create mode 100644 src/base64/index.ts create mode 100644 src/base64/keyStrBase64.ts create mode 100644 src/encodedURI/compressToEncodedURI.ts create mode 100644 src/encodedURI/decompressFromEncodedURI.ts create mode 100644 src/encodedURI/index.ts create mode 100644 src/encodedURI/keyStrUriSafe.ts create mode 100644 src/getBaseValue.ts create mode 100644 src/index.ts delete mode 100644 src/interfaces/DecompressionTracker.ts delete mode 100644 src/interfaces/index.ts delete mode 100644 src/main.ts create mode 100644 src/stock/compress.ts create mode 100644 src/stock/decompress.ts create mode 100644 src/stock/index.ts delete mode 100644 src/types/Dictionary.ts delete mode 100644 src/types/DictionaryCollection.ts delete mode 100644 src/types/PendingDictionary.ts delete mode 100644 src/types/index.ts diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..18f2b36 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,3 @@ +coverage +dist +node_modules diff --git a/.eslintrc b/.eslintrc index 5a82ec7..10ff3ae 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,9 +1,5 @@ { - "extends": [ - "eslint:recommended", - "plugin:@typescript-eslint/recommended", - "eslint-config-prettier" - ], + "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "eslint-config-prettier"], "settings": { "import/resolver": { "node": { diff --git a/.prettierrc b/.prettierrc index 2259cff..f4df036 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,6 +1,8 @@ { - "trailingComma": "all", - "tabWidth": 4, + "printWidth": 120, + "quoteProps": "consistent", "semi": true, - "singleQuote": false + "singleQuote": false, + "tabWidth": 4, + "trailingComma": "all" } diff --git a/README.md b/README.md index 47e487f..e83e514 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ LZ-based compression algorithm for JavaScript > The file layout has changed in version 2, this is now a joint `commonjs` / `esmodule` project so modern build tools should be happy with it, but if importing a file directly (such as in a direct javascript project) it is important to use the correct one. > [!TIP] -> The "old style" minified UMD file is available as `dist/index.umd.cjs` via various CDNs or package managers. +> The "old style" minified AMD file is available as `dist/index.umd.js` via various CDNs or package managers. ## Install via [npm](https://www.npmjs.com/package/lz-string) diff --git a/package-lock.json b/package-lock.json index dcb9739..795c6f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,6 @@ "@vitest/coverage-v8": "1.2.0", "eslint": "8.56.0", "eslint-config-prettier": "9.1.0", - "tiny-invariant": "1.3.1", "tslib": "2.6.2", "typescript": "5.3.3", "vite": "5.0.11", @@ -113,86 +112,6 @@ "url": "https://github.com/prettier/prettier?sponsor=1" } }, - "node_modules/@esbuild/aix-ppc64": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.19.11.tgz", - "integrity": "sha512-FnzU0LyE3ySQk7UntJO4+qIiQgI7KoODnZg5xzXIrFJlKd2P2gwHsHY4927xj9y5PJmJSzULiUCWmv7iWnNa7g==", - "cpu": [ - "ppc64" - ], - "dev": true, - "optional": true, - "os": [ - "aix" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/android-arm": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.19.11.tgz", - "integrity": "sha512-5OVapq0ClabvKvQ58Bws8+wkLCV+Rxg7tUVbo9xu034Nm536QTII4YzhaFriQ7rMrorfnFKUsArD2lqKbFY4vw==", - "cpu": [ - "arm" - ], - "dev": true, - "optional": true, - "os": [ - "android" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/android-arm64": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.19.11.tgz", - "integrity": "sha512-aiu7K/5JnLj//KOnOfEZ0D90obUkRzDMyqd/wNAUQ34m4YUPVhRZpnqKV9uqDGxT7cToSDnIHsGooyIczu9T+Q==", - "cpu": [ - "arm64" - ], - "dev": true, - "optional": true, - "os": [ - "android" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/android-x64": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.19.11.tgz", - "integrity": "sha512-eccxjlfGw43WYoY9QgB82SgGgDbibcqyDTlk3l3C0jOVHKxrjdc9CTwDUQd0vkvYg5um0OH+GpxYvp39r+IPOg==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "android" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/darwin-arm64": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.19.11.tgz", - "integrity": "sha512-ETp87DRWuSt9KdDVkqSoKoLFHYTrkyz2+65fj9nfXsaV3bMhTCjtQfw3y+um88vGRKRiF7erPrh/ZuIdLUIVxQ==", - "cpu": [ - "arm64" - ], - "dev": true, - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": ">=12" - } - }, "node_modules/@esbuild/darwin-x64": { "version": "0.19.11", "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.19.11.tgz", @@ -209,278 +128,6 @@ "node": ">=12" } }, - "node_modules/@esbuild/freebsd-arm64": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.19.11.tgz", - "integrity": "sha512-lhoSp5K6bxKRNdXUtHoNc5HhbXVCS8V0iZmDvyWvYq9S5WSfTIHU2UGjcGt7UeS6iEYp9eeymIl5mJBn0yiuxA==", - "cpu": [ - "arm64" - ], - "dev": true, - "optional": true, - "os": [ - "freebsd" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/freebsd-x64": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.19.11.tgz", - "integrity": "sha512-JkUqn44AffGXitVI6/AbQdoYAq0TEullFdqcMY/PCUZ36xJ9ZJRtQabzMA+Vi7r78+25ZIBosLTOKnUXBSi1Kw==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "freebsd" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-arm": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.19.11.tgz", - "integrity": "sha512-3CRkr9+vCV2XJbjwgzjPtO8T0SZUmRZla+UL1jw+XqHZPkPgZiyWvbDvl9rqAN8Zl7qJF0O/9ycMtjU67HN9/Q==", - "cpu": [ - "arm" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-arm64": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.19.11.tgz", - "integrity": "sha512-LneLg3ypEeveBSMuoa0kwMpCGmpu8XQUh+mL8XXwoYZ6Be2qBnVtcDI5azSvh7vioMDhoJFZzp9GWp9IWpYoUg==", - "cpu": [ - "arm64" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-ia32": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.19.11.tgz", - "integrity": "sha512-caHy++CsD8Bgq2V5CodbJjFPEiDPq8JJmBdeyZ8GWVQMjRD0sU548nNdwPNvKjVpamYYVL40AORekgfIubwHoA==", - "cpu": [ - "ia32" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-loong64": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.19.11.tgz", - "integrity": "sha512-ppZSSLVpPrwHccvC6nQVZaSHlFsvCQyjnvirnVjbKSHuE5N24Yl8F3UwYUUR1UEPaFObGD2tSvVKbvR+uT1Nrg==", - "cpu": [ - "loong64" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-mips64el": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.19.11.tgz", - "integrity": "sha512-B5x9j0OgjG+v1dF2DkH34lr+7Gmv0kzX6/V0afF41FkPMMqaQ77pH7CrhWeR22aEeHKaeZVtZ6yFwlxOKPVFyg==", - "cpu": [ - "mips64el" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-ppc64": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.19.11.tgz", - "integrity": "sha512-MHrZYLeCG8vXblMetWyttkdVRjQlQUb/oMgBNurVEnhj4YWOr4G5lmBfZjHYQHHN0g6yDmCAQRR8MUHldvvRDA==", - "cpu": [ - "ppc64" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-riscv64": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.19.11.tgz", - "integrity": "sha512-f3DY++t94uVg141dozDu4CCUkYW+09rWtaWfnb3bqe4w5NqmZd6nPVBm+qbz7WaHZCoqXqHz5p6CM6qv3qnSSQ==", - "cpu": [ - "riscv64" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-s390x": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.19.11.tgz", - "integrity": "sha512-A5xdUoyWJHMMlcSMcPGVLzYzpcY8QP1RtYzX5/bS4dvjBGVxdhuiYyFwp7z74ocV7WDc0n1harxmpq2ePOjI0Q==", - "cpu": [ - "s390x" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-x64": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.19.11.tgz", - "integrity": "sha512-grbyMlVCvJSfxFQUndw5mCtWs5LO1gUlwP4CDi4iJBbVpZcqLVT29FxgGuBJGSzyOxotFG4LoO5X+M1350zmPA==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/netbsd-x64": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.19.11.tgz", - "integrity": "sha512-13jvrQZJc3P230OhU8xgwUnDeuC/9egsjTkXN49b3GcS5BKvJqZn86aGM8W9pd14Kd+u7HuFBMVtrNGhh6fHEQ==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "netbsd" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/openbsd-x64": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.19.11.tgz", - "integrity": "sha512-ysyOGZuTp6SNKPE11INDUeFVVQFrhcNDVUgSQVDzqsqX38DjhPEPATpid04LCoUr2WXhQTEZ8ct/EgJCUDpyNw==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "openbsd" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/sunos-x64": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.19.11.tgz", - "integrity": "sha512-Hf+Sad9nVwvtxy4DXCZQqLpgmRTQqyFyhT3bZ4F2XlJCjxGmRFF0Shwn9rzhOYRB61w9VMXUkxlBy56dk9JJiQ==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "sunos" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/win32-arm64": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.19.11.tgz", - "integrity": "sha512-0P58Sbi0LctOMOQbpEOvOL44Ne0sqbS0XWHMvvrg6NE5jQ1xguCSSw9jQeUk2lfrXYsKDdOe6K+oZiwKPilYPQ==", - "cpu": [ - "arm64" - ], - "dev": true, - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/win32-ia32": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.19.11.tgz", - "integrity": "sha512-6YOrWS+sDJDmshdBIQU+Uoyh7pQKrdykdefC1avn76ss5c+RN6gut3LZA4E2cH5xUEp5/cA0+YxRaVtRAb0xBg==", - "cpu": [ - "ia32" - ], - "dev": true, - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/win32-x64": { - "version": "0.19.11", - "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.19.11.tgz", - "integrity": "sha512-vfkhltrjCAb603XaFhqhAF4LGDi2M4OrCRrFusyQ+iTLQ/o60QQXxc9cZC/FFpihBI9N1Grn6SMKVJ4KP7Fuiw==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": ">=12" - } - }, "node_modules/@eslint-community/eslint-utils": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.4.0.tgz", @@ -817,45 +464,6 @@ } } }, - "node_modules/@rollup/rollup-android-arm-eabi": { - "version": "4.9.5", - "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.9.5.tgz", - "integrity": "sha512-idWaG8xeSRCfRq9KpRysDHJ/rEHBEXcHuJ82XY0yYFIWnLMjZv9vF/7DOq8djQ2n3Lk6+3qfSH8AqlmHlmi1MA==", - "cpu": [ - "arm" - ], - "dev": true, - "optional": true, - "os": [ - "android" - ] - }, - "node_modules/@rollup/rollup-android-arm64": { - "version": "4.9.5", - "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm64/-/rollup-android-arm64-4.9.5.tgz", - "integrity": "sha512-f14d7uhAMtsCGjAYwZGv6TwuS3IFaM4ZnGMUn3aCBgkcHAYErhV1Ad97WzBvS2o0aaDv4mVz+syiN0ElMyfBPg==", - "cpu": [ - "arm64" - ], - "dev": true, - "optional": true, - "os": [ - "android" - ] - }, - "node_modules/@rollup/rollup-darwin-arm64": { - "version": "4.9.5", - "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-arm64/-/rollup-darwin-arm64-4.9.5.tgz", - "integrity": "sha512-ndoXeLx455FffL68OIUrVr89Xu1WLzAG4n65R8roDlCoYiQcGGg6MALvs2Ap9zs7AHg8mpHtMpwC8jBBjZrT/w==", - "cpu": [ - "arm64" - ], - "dev": true, - "optional": true, - "os": [ - "darwin" - ] - }, "node_modules/@rollup/rollup-darwin-x64": { "version": "4.9.5", "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-x64/-/rollup-darwin-x64-4.9.5.tgz", @@ -869,123 +477,6 @@ "darwin" ] }, - "node_modules/@rollup/rollup-linux-arm-gnueabihf": { - "version": "4.9.5", - "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-gnueabihf/-/rollup-linux-arm-gnueabihf-4.9.5.tgz", - "integrity": "sha512-Q0LcU61v92tQB6ae+udZvOyZ0wfpGojtAKrrpAaIqmJ7+psq4cMIhT/9lfV6UQIpeItnq/2QDROhNLo00lOD1g==", - "cpu": [ - "arm" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ] - }, - "node_modules/@rollup/rollup-linux-arm64-gnu": { - "version": "4.9.5", - "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-gnu/-/rollup-linux-arm64-gnu-4.9.5.tgz", - "integrity": "sha512-dkRscpM+RrR2Ee3eOQmRWFjmV/payHEOrjyq1VZegRUa5OrZJ2MAxBNs05bZuY0YCtpqETDy1Ix4i/hRqX98cA==", - "cpu": [ - "arm64" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ] - }, - "node_modules/@rollup/rollup-linux-arm64-musl": { - "version": "4.9.5", - "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-musl/-/rollup-linux-arm64-musl-4.9.5.tgz", - "integrity": "sha512-QaKFVOzzST2xzY4MAmiDmURagWLFh+zZtttuEnuNn19AiZ0T3fhPyjPPGwLNdiDT82ZE91hnfJsUiDwF9DClIQ==", - "cpu": [ - "arm64" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ] - }, - "node_modules/@rollup/rollup-linux-riscv64-gnu": { - "version": "4.9.5", - "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-gnu/-/rollup-linux-riscv64-gnu-4.9.5.tgz", - "integrity": "sha512-HeGqmRJuyVg6/X6MpE2ur7GbymBPS8Np0S/vQFHDmocfORT+Zt76qu+69NUoxXzGqVP1pzaY6QIi0FJWLC3OPA==", - "cpu": [ - "riscv64" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ] - }, - "node_modules/@rollup/rollup-linux-x64-gnu": { - "version": "4.9.5", - "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-gnu/-/rollup-linux-x64-gnu-4.9.5.tgz", - "integrity": "sha512-Dq1bqBdLaZ1Gb/l2e5/+o3B18+8TI9ANlA1SkejZqDgdU/jK/ThYaMPMJpVMMXy2uRHvGKbkz9vheVGdq3cJfA==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ] - }, - "node_modules/@rollup/rollup-linux-x64-musl": { - "version": "4.9.5", - "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-musl/-/rollup-linux-x64-musl-4.9.5.tgz", - "integrity": "sha512-ezyFUOwldYpj7AbkwyW9AJ203peub81CaAIVvckdkyH8EvhEIoKzaMFJj0G4qYJ5sw3BpqhFrsCc30t54HV8vg==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ] - }, - "node_modules/@rollup/rollup-win32-arm64-msvc": { - "version": "4.9.5", - "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-arm64-msvc/-/rollup-win32-arm64-msvc-4.9.5.tgz", - "integrity": "sha512-aHSsMnUw+0UETB0Hlv7B/ZHOGY5bQdwMKJSzGfDfvyhnpmVxLMGnQPGNE9wgqkLUs3+gbG1Qx02S2LLfJ5GaRQ==", - "cpu": [ - "arm64" - ], - "dev": true, - "optional": true, - "os": [ - "win32" - ] - }, - "node_modules/@rollup/rollup-win32-ia32-msvc": { - "version": "4.9.5", - "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-ia32-msvc/-/rollup-win32-ia32-msvc-4.9.5.tgz", - "integrity": "sha512-AiqiLkb9KSf7Lj/o1U3SEP9Zn+5NuVKgFdRIZkvd4N0+bYrTOovVd0+LmYCPQGbocT4kvFyK+LXCDiXPBF3fyA==", - "cpu": [ - "ia32" - ], - "dev": true, - "optional": true, - "os": [ - "win32" - ] - }, - "node_modules/@rollup/rollup-win32-x64-msvc": { - "version": "4.9.5", - "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.9.5.tgz", - "integrity": "sha512-1q+mykKE3Vot1kaFJIDoUFv5TuW+QQVaf2FmTT9krg86pQrGStOSJJ0Zil7CFagyxDuouTepzt5Y5TVzyajOdQ==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "win32" - ] - }, "node_modules/@rushstack/node-core-library": { "version": "3.62.0", "resolved": "https://registry.npmjs.org/@rushstack/node-core-library/-/node-core-library-3.62.0.tgz", @@ -3601,12 +3092,6 @@ "integrity": "sha512-N+8UisAXDGk8PFXP4HAzVR9nbfmVJ3zYLAWiTIoqC5v5isinhr+r5uaO8+7r3BMfuNIufIsA7RdpVgacC2cSpw==", "dev": true }, - "node_modules/tiny-invariant": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/tiny-invariant/-/tiny-invariant-1.3.1.tgz", - "integrity": "sha512-AD5ih2NlSssTCwsMznbvwMZpJ1cbhkGd2uueNxzv2jDlEeZdU04JQfRnggJQ8DrcVBGjAsCKwFBbDlVNtEMlzw==", - "dev": true - }, "node_modules/tinybench": { "version": "2.5.1", "resolved": "https://registry.npmjs.org/tinybench/-/tinybench-2.5.1.tgz", diff --git a/package.json b/package.json index d8f4742..6605242 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ } }, "type": "module", - "main": "./dist/index.umd.cjs", + "main": "./dist/index.umd.js", "module": "./dist/index.js", "types": "./dist/index.d.ts", "files": [ @@ -47,7 +47,7 @@ "scripts": { "build": "tsc && vite build", "format": "prettier --write .", - "start": "vite", + "start": "vite build --watch", "test": "[ -f ./dist/index.js ] || npm run build && vitest", "test:coverage": "vitest run src.main.test.ts --coverage", "lint": "eslint ." @@ -60,7 +60,6 @@ "@vitest/coverage-v8": "1.2.0", "eslint": "8.56.0", "eslint-config-prettier": "9.1.0", - "tiny-invariant": "1.3.1", "tslib": "2.6.2", "typescript": "5.3.3", "vite": "5.0.11", diff --git a/src/UTF16/compressToUTF16.ts b/src/UTF16/compressToUTF16.ts new file mode 100644 index 0000000..5d7ac23 --- /dev/null +++ b/src/UTF16/compressToUTF16.ts @@ -0,0 +1,7 @@ +import { _compress } from "../_compress"; + +export function compressToUTF16(input: string): string { + if (input == null) return ""; + + return _compress(input, 15, (a) => String.fromCharCode(a + 32)) + " "; +} diff --git a/src/UTF16/decompressFromUTF16.ts b/src/UTF16/decompressFromUTF16.ts new file mode 100644 index 0000000..d0c421c --- /dev/null +++ b/src/UTF16/decompressFromUTF16.ts @@ -0,0 +1,8 @@ +import { _decompress } from "../_decompress"; + +export function decompressFromUTF16(compressed: string): string | null { + if (compressed == null) return ""; + if (compressed == "") return null; + + return _decompress(compressed.length, 16384, (index) => compressed.charCodeAt(index) - 32); +} diff --git a/src/UTF16/index.ts b/src/UTF16/index.ts new file mode 100644 index 0000000..9a88b42 --- /dev/null +++ b/src/UTF16/index.ts @@ -0,0 +1,2 @@ +export { compressToUTF16 } from "./compressToUTF16"; +export { decompressFromUTF16 } from "./decompressFromUTF16"; diff --git a/src/Uint8Array/compressToUint8Array.ts b/src/Uint8Array/compressToUint8Array.ts new file mode 100644 index 0000000..4ec023d --- /dev/null +++ b/src/Uint8Array/compressToUint8Array.ts @@ -0,0 +1,14 @@ +import { compress } from "../stock/compress"; + +export function compressToUint8Array(uncompressed: string): Uint8Array { + const compressed: string = compress(uncompressed); + const buf: Uint8Array = new Uint8Array(compressed.length * 2); // 2 bytes per character + + for (let i = 0, TotalLen = compressed.length; i < TotalLen; i++) { + const current_value = compressed.charCodeAt(i); + buf[i * 2] = current_value >>> 8; + buf[i * 2 + 1] = current_value % 256; + } + + return buf; +} diff --git a/src/Uint8Array/decompressFromUint8Array.ts b/src/Uint8Array/decompressFromUint8Array.ts new file mode 100644 index 0000000..20459ca --- /dev/null +++ b/src/Uint8Array/decompressFromUint8Array.ts @@ -0,0 +1,21 @@ +import { decompress } from "../stock/decompress"; + +export function decompressFromUint8Array(compressed: Uint8Array): string | null { + if (compressed === null || compressed === undefined) { + return decompress(compressed); + } else { + const buf: number[] = new Array(compressed.length / 2); // 2 bytes per character + + for (let i = 0, TotalLen = buf.length; i < TotalLen; i++) { + buf[i] = compressed[i * 2] * 256 + compressed[i * 2 + 1]; + } + + const result: string[] = []; + + buf.forEach(function (c) { + result.push(String.fromCharCode(c)); + }); + + return decompress(result.join("")); + } +} diff --git a/src/Uint8Array/index.ts b/src/Uint8Array/index.ts new file mode 100644 index 0000000..f56bdce --- /dev/null +++ b/src/Uint8Array/index.ts @@ -0,0 +1,2 @@ +export { compressToUint8Array } from "./compressToUint8Array"; +export { decompressFromUint8Array } from "./decompressFromUint8Array"; diff --git a/src/_compress.ts b/src/_compress.ts new file mode 100644 index 0000000..afeb91e --- /dev/null +++ b/src/_compress.ts @@ -0,0 +1,219 @@ +export type Dictionary = Record; +export type PendingDictionary = Record; + +export function _compress(uncompressed: null, bitsPerChar: number, getCharFromInt: (a: number) => string): ""; +export function _compress(uncompressed: string, bitsPerChar: number, getCharFromInt: (a: number) => string): string; +export function _compress( + uncompressed: string | null, + bitsPerChar: number, + getCharFromInt: (a: number) => string, +): string { + if (uncompressed == null) return ""; + + let i: number; + let value: number; + const context_dictionary: Dictionary = {}; + const context_dictionaryToCreate: PendingDictionary = {}; + let context_c: string = ""; + let context_wc: string = ""; + let context_w: string = ""; + let context_enlargeIn: number = 2; // Compensate for the first entry which should not count + let context_dictSize: number = 3; + let context_numBits: number = 2; + const context_data: string[] = []; + let context_data_val: number = 0; + let context_data_position: number = 0; + let ii: number; + + 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++; + } + // Add wc to the dictionary. + context_dictionary[context_wc] = context_dictSize++; + context_w = String(context_c); + } + } + // Output the code for w. + 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++; + } + } + // Mark the end of the stream + 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; + } + // Flush the last char + // eslint-disable-next-line no-constant-condition + 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(""); +} diff --git a/src/_decompress.ts b/src/_decompress.ts new file mode 100644 index 0000000..905d78f --- /dev/null +++ b/src/_decompress.ts @@ -0,0 +1,166 @@ +export interface DecompressionTracker { + val: number; + position: number; + index: number; +} + +export function _decompress(length: number, resetValue: number, getNextValue: (a: number) => number): string | null { + const dictionary: string[] = []; + let enlargeIn: number = 4; + let dictSize: number = 4; + let numBits: number = 3; + let entry: string = ""; + const result: string[] = []; + let i: number; + let w: string; + let bits: number; + let resb: number; + let maxpower: number; + let power: number; + let c: string | number; + const data: DecompressionTracker = { + val: getNextValue(0), + position: resetValue, + index: 1, + }; + + for (i = 0; i < 3; i += 1) { + dictionary[i] = String(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 (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 ""; + } + if (c! === undefined) { + throw new Error("No character found"); + } + dictionary[3] = String(c); + w = String(c); + result.push(String(c)); + // eslint-disable-next-line no-constant-condition + 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 = String(dictionary[c]); + } else { + if (c === dictSize) { + 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++; + } + } +} diff --git a/src/base64-string.ts b/src/base64/base64-string.ts similarity index 92% rename from src/base64-string.ts rename to src/base64/base64-string.ts index c936f08..8c7b905 100644 --- a/src/base64-string.ts +++ b/src/base64/base64-string.ts @@ -10,8 +10,6 @@ // Base64 compression / decompression for already compressed content (gif, png, jpg, mp3, ...) // version 1.4.1 -import invariant from "tiny-invariant"; - interface Base64String { compress: (input: string) => string; decompress: (input: string) => string; @@ -93,12 +91,7 @@ const Base64String: Base64String = { current = (c & 16383) << 1; break; case 14: - output.push( - String.fromCharCode( - current + (c >> 15) + 32, - (c & 32767) + 32, - ), - ); + output.push(String.fromCharCode(current + (c >> 15) + 32, (c & 32767) + 32)); status = 0; break; } @@ -191,8 +184,7 @@ const Base64String: Base64String = { }, // private property - _keyStr: - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", + _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", decompress: function (input: string): string { const output: string[] = []; @@ -205,8 +197,7 @@ const Base64String: Base64String = { if (i % 2 == 0) { chr1 = input.charCodeAt(i / 2) >> 8; chr2 = input.charCodeAt(i / 2) & 255; - if (i / 2 + 1 < input.length) - chr3 = input.charCodeAt(i / 2 + 1) >> 8; + if (i / 2 + 1 < input.length) chr3 = input.charCodeAt(i / 2 + 1) >> 8; else chr3 = NaN; } else { chr1 = input.charCodeAt((i - 1) / 2) & 255; @@ -271,7 +262,9 @@ const Base64String: Base64String = { flush = true; } } else { - invariant(output_! !== undefined, "Output error 1"); + if (output_! === undefined) { + throw new Error("Impossible output error 1"); + } output.push(String.fromCharCode(output_ | chr1)); flush = false; @@ -280,7 +273,6 @@ const Base64String: Base64String = { flush = true; } if (enc4 != 64) { - invariant(output_! !== undefined, "Output error 2"); output.push(String.fromCharCode(output_ | chr3)); flush = false; } @@ -289,12 +281,12 @@ const Base64String: Base64String = { } if (flush) { - invariant(output_! !== undefined, "Output error 3"); + if (output_! === undefined) { + throw new Error("Impossible output error 1"); + } output.push(String.fromCharCode(output_)); output = output.join(""); - output = - String.fromCharCode(output.charCodeAt(0) | 256) - + output.substring(1); + output = String.fromCharCode(output.charCodeAt(0) | 256) + output.substring(1); } else { output = output.join(""); } diff --git a/src/base64/compressToBase64.ts b/src/base64/compressToBase64.ts new file mode 100644 index 0000000..091922f --- /dev/null +++ b/src/base64/compressToBase64.ts @@ -0,0 +1,22 @@ +import { _compress } from "../_compress"; +import keyStrBase64 from "./keyStrBase64"; + +export function compressToBase64(input: string): string { + if (input == null) return ""; + + const res: string = _compress(input, 6, (a) => 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 + "="; + } +} diff --git a/src/base64/decompressFromBase64.ts b/src/base64/decompressFromBase64.ts new file mode 100644 index 0000000..52f5fb7 --- /dev/null +++ b/src/base64/decompressFromBase64.ts @@ -0,0 +1,10 @@ +import { _decompress } from "../_decompress"; +import { getBaseValue } from "../getBaseValue"; +import keyStrBase64 from "./keyStrBase64"; + +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))); +} diff --git a/src/base64/index.ts b/src/base64/index.ts new file mode 100644 index 0000000..0a67abd --- /dev/null +++ b/src/base64/index.ts @@ -0,0 +1,2 @@ +export { compressToBase64 } from "./compressToBase64"; +export { decompressFromBase64 } from "./decompressFromBase64"; diff --git a/src/base64/keyStrBase64.ts b/src/base64/keyStrBase64.ts new file mode 100644 index 0000000..d2ffd44 --- /dev/null +++ b/src/base64/keyStrBase64.ts @@ -0,0 +1 @@ +export default "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; diff --git a/src/encodedURI/compressToEncodedURI.ts b/src/encodedURI/compressToEncodedURI.ts new file mode 100644 index 0000000..371cb4f --- /dev/null +++ b/src/encodedURI/compressToEncodedURI.ts @@ -0,0 +1,10 @@ +import { _compress } from "../_compress"; +import keyStrUriSafe from "./keyStrUriSafe"; + +export function compressToEncodedURIComponent(input: string): string { + if (input == null) return ""; + + return _compress(input, 6, function (a) { + return keyStrUriSafe.charAt(a); + }); +} diff --git a/src/encodedURI/decompressFromEncodedURI.ts b/src/encodedURI/decompressFromEncodedURI.ts new file mode 100644 index 0000000..ac0ab62 --- /dev/null +++ b/src/encodedURI/decompressFromEncodedURI.ts @@ -0,0 +1,12 @@ +import keyStrUriSafe from "./keyStrUriSafe"; +import { _decompress } from "../_decompress"; +import { getBaseValue } from "../getBaseValue"; + +export function decompressFromEncodedURIComponent(input: string): string | null { + if (input == null) return ""; + if (input == "") return null; + + input = input.replace(/ /g, "+"); + + return _decompress(input.length, 32, (index) => getBaseValue(keyStrUriSafe, input.charAt(index))); +} diff --git a/src/encodedURI/index.ts b/src/encodedURI/index.ts new file mode 100644 index 0000000..e97c524 --- /dev/null +++ b/src/encodedURI/index.ts @@ -0,0 +1,2 @@ +export { compressToEncodedURIComponent } from "./compressToEncodedURI"; +export { decompressFromEncodedURIComponent } from "./decompressFromEncodedURI"; diff --git a/src/encodedURI/keyStrUriSafe.ts b/src/encodedURI/keyStrUriSafe.ts new file mode 100644 index 0000000..ac67586 --- /dev/null +++ b/src/encodedURI/keyStrUriSafe.ts @@ -0,0 +1 @@ +export default "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$"; diff --git a/src/getBaseValue.ts b/src/getBaseValue.ts new file mode 100644 index 0000000..c0f922b --- /dev/null +++ b/src/getBaseValue.ts @@ -0,0 +1,14 @@ +export type Dictionary = Record; +export type DictionaryCollection = Record; + +const baseReverseDic: DictionaryCollection = {}; + +export function getBaseValue(alphabet: string, character: string): number { + if (!baseReverseDic[alphabet]) { + baseReverseDic[alphabet] = {}; + for (let i = 0; i < alphabet.length; i++) { + baseReverseDic[alphabet][alphabet.charAt(i)] = i; + } + } + return baseReverseDic[alphabet][character]; +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..4a09bd7 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,37 @@ +// Copyright (c) 2013 Pieroxy +// This work is free. You can redistribute it and/or modify it +// under the terms of the WTFPL, Version 2 +// For more information see LICENSE.txt or http://www.wtfpl.net/ +// +// For more information, the home page: +// http://pieroxy.net/blog/pages/lz-string/testing.html +// +// LZ-based compression algorithm, version 1.4.5 + +import { _compress } from "./_compress"; +import { _decompress } from "./_decompress"; +import { compressToBase64, decompressFromBase64 } from "./base64"; +import { compressToEncodedURIComponent, decompressFromEncodedURIComponent } from "./encodedURI"; +import { compress, decompress } from "./stock"; +import { compressToUint8Array, decompressFromUint8Array } from "./Uint8Array"; +import { compressToUTF16, decompressFromUTF16 } from "./UTF16"; + +export default { + _compress, + _decompress, + + compress, + decompress, + + compressToBase64, + decompressFromBase64, + + compressToEncodedURIComponent, + decompressFromEncodedURIComponent, + + compressToUint8Array, + decompressFromUint8Array, + + compressToUTF16, + decompressFromUTF16, +}; diff --git a/src/interfaces/DecompressionTracker.ts b/src/interfaces/DecompressionTracker.ts deleted file mode 100644 index f9c46ca..0000000 --- a/src/interfaces/DecompressionTracker.ts +++ /dev/null @@ -1,5 +0,0 @@ -export interface DecompressionTracker { - val: number; - position: number; - index: number; -} diff --git a/src/interfaces/index.ts b/src/interfaces/index.ts deleted file mode 100644 index 63b0e45..0000000 --- a/src/interfaces/index.ts +++ /dev/null @@ -1 +0,0 @@ -export type { DecompressionTracker } from "@/interfaces/DecompressionTracker"; diff --git a/src/main.ts b/src/main.ts deleted file mode 100644 index 79eb8f8..0000000 --- a/src/main.ts +++ /dev/null @@ -1,605 +0,0 @@ -// Copyright (c) 2013 Pieroxy -// This work is free. You can redistribute it and/or modify it -// under the terms of the WTFPL, Version 2 -// For more information see LICENSE.txt or http://www.wtfpl.net/ -// -// For more information, the home page: -// http://pieroxy.net/blog/pages/lz-string/testing.html -// -// LZ-based compression algorithm, version 1.4.5 - -import type { DecompressionTracker } from "./interfaces"; -import type { - Dictionary, - DictionaryCollection, - PendingDictionary, -} from "./types"; -import invariant from "tiny-invariant"; - -export interface LZString { - _compress: ( - uncompressed: string, - bitsPerChar: number, - getCharFromInt: (value: number) => string, - ) => string; - _decompress: ( - length: number, - resetValue: number, - getNextValue: (index: number) => number, - ) => string | null; - compress: (uncompressed: string) => string; - decompress: (compressed: string) => string | null; - - compressToBase64: (input: string) => string; - decompressFromBase64: (input: string) => string | null; - compressToUTF16: (input: string) => string; - decompressFromUTF16: (compressed: string) => string | null; - compressToUint8Array: (uncompressed: string) => Uint8Array; - decompressFromUint8Array: (compressed: Uint8Array) => string | null; - compressToEncodedURIComponent: (input: string) => string; - decompressFromEncodedURIComponent: (input: string) => string | null; -} - -export const LZString: LZString = (function () { - // private property - const f = String.fromCharCode; - const keyStrBase64: string = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; - const keyStrUriSafe: string = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$"; - const baseReverseDic: DictionaryCollection = {}; - - function getBaseValue(alphabet: string, character: string): number { - if (!baseReverseDic[alphabet]) { - baseReverseDic[alphabet] = {}; - for (let i = 0; i < alphabet.length; i++) { - baseReverseDic[alphabet][alphabet.charAt(i)] = i; - } - } - return baseReverseDic[alphabet][character]; - } - - const LZString: LZString = { - compressToBase64: function (input: string): string { - if (input == null) return ""; - const res: string = LZString._compress(input, 6, function (a) { - return 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 + "="; - } - }, - - decompressFromBase64: function (input): string | null { - if (input == null) return ""; - if (input == "") return null; - return LZString._decompress(input.length, 32, function (index) { - return getBaseValue(keyStrBase64, input.charAt(index)); - }); - }, - - compressToUTF16: function (input: string): string { - if (input == null) return ""; - return ( - LZString._compress(input, 15, function (a) { - return f(a + 32); - }) + " " - ); - }, - - decompressFromUTF16: function (compressed: string): string | null { - if (compressed == null) return ""; - if (compressed == "") return null; - return LZString._decompress( - compressed.length, - 16384, - function (index) { - return compressed.charCodeAt(index) - 32; - }, - ); - }, - - //compress into uint8array (UCS-2 big endian format) - compressToUint8Array: function (uncompressed: string): Uint8Array { - const compressed: string = LZString.compress(uncompressed); - const buf: Uint8Array = new Uint8Array(compressed.length * 2); // 2 bytes per character - for (let i = 0, TotalLen = compressed.length; i < TotalLen; i++) { - const current_value = compressed.charCodeAt(i); - buf[i * 2] = current_value >>> 8; - buf[i * 2 + 1] = current_value % 256; - } - return buf; - }, - - //decompress from uint8array (UCS-2 big endian format) - decompressFromUint8Array: function ( - compressed: Uint8Array, - ): string | null { - if (compressed === null || compressed === undefined) { - return LZString.decompress(compressed); - } else { - const buf: number[] = new Array(compressed.length / 2); // 2 bytes per character - for (let i = 0, TotalLen = buf.length; i < TotalLen; i++) { - buf[i] = compressed[i * 2] * 256 + compressed[i * 2 + 1]; - } - const result: string[] = []; - buf.forEach(function (c) { - result.push(f(c)); - }); - return LZString.decompress(result.join("")); - } - }, - - //compress into a string that is already URI encoded - compressToEncodedURIComponent: function (input: string): string { - if (input == null) return ""; - return LZString._compress(input, 6, function (a) { - return keyStrUriSafe.charAt(a); - }); - }, - - //decompress from an output of compressToEncodedURIComponent - decompressFromEncodedURIComponent: function ( - input: string, - ): string | null { - if (input == null) return ""; - if (input == "") return null; - input = input.replace(/ /g, "+"); - return LZString._decompress(input.length, 32, function (index) { - return getBaseValue(keyStrUriSafe, input.charAt(index)); - }); - }, - - compress: function (uncompressed: string): string { - return LZString._compress(uncompressed, 16, function (a) { - return f(a); - }); - }, - - _compress: function ( - uncompressed, - bitsPerChar, - getCharFromInt, - ): string { - if (uncompressed == null) return ""; - let i: number; - let value: number; - const context_dictionary: Dictionary = {}; - const context_dictionaryToCreate: PendingDictionary = {}; - let context_c: string = ""; - let context_wc: string = ""; - let context_w: string = ""; - let context_enlargeIn: number = 2; // Compensate for the first entry which should not count - let context_dictSize: number = 3; - let context_numBits: number = 2; - const context_data: string[] = []; - let context_data_val: number = 0; - let context_data_position: number = 0; - let ii: number; - - 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++; - } - // Add wc to the dictionary. - context_dictionary[context_wc] = context_dictSize++; - context_w = String(context_c); - } - } - // Output the code for w. - 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++; - } - } - // Mark the end of the stream - 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; - } - // Flush the last char - // eslint-disable-next-line no-constant-condition - 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(""); - }, - - decompress: function (compressed: string): string | null { - if (compressed == null) return ""; - if (compressed == "") return null; - return LZString._decompress( - compressed.length, - 32768, - function (index) { - return compressed.charCodeAt(index); - }, - ); - }, - - _decompress: function ( - length, - resetValue, - getNextValue, - ): string | null { - const dictionary: string[] = []; - let enlargeIn: number = 4; - let dictSize: number = 4; - let numBits: number = 3; - let entry: string = ""; - const result: string[] = []; - let i: number; - let w: string; - let bits: number; - let resb: number; - let maxpower: number; - let power: number; - let c: string | number; - const data: DecompressionTracker = { - val: getNextValue(0), - position: resetValue, - index: 1, - }; - - for (i = 0; i < 3; i += 1) { - dictionary[i] = String(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 (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 = 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 = f(bits); - break; - case 2: - return ""; - } - /* TODO - address during code cleanup */ - invariant(c! !== undefined, "No character found"); - dictionary[3] = String(c); - w = String(c); - result.push(String(c)); - // eslint-disable-next-line no-constant-condition - 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++] = 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++] = 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 = String(dictionary[c]); - } else { - if (c === dictSize) { - 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++; - } - } - }, - }; - return LZString; -})(); diff --git a/src/stock/compress.ts b/src/stock/compress.ts new file mode 100644 index 0000000..39cb9c9 --- /dev/null +++ b/src/stock/compress.ts @@ -0,0 +1,5 @@ +import { _compress } from "../_compress"; + +export function compress(uncompressed: string): string { + return _compress(uncompressed, 16, (a: number) => String.fromCharCode(a)); +} diff --git a/src/stock/decompress.ts b/src/stock/decompress.ts new file mode 100644 index 0000000..e95326b --- /dev/null +++ b/src/stock/decompress.ts @@ -0,0 +1,8 @@ +import { _decompress } from "../_decompress"; + +export function decompress(compressed: string): string | null { + if (compressed == null) return ""; + if (compressed == "") return null; + + return _decompress(compressed.length, 32768, (index) => compressed.charCodeAt(index)); +} diff --git a/src/stock/index.ts b/src/stock/index.ts new file mode 100644 index 0000000..b65af8f --- /dev/null +++ b/src/stock/index.ts @@ -0,0 +1,2 @@ +export { compress } from "./compress"; +export { decompress } from "./decompress"; diff --git a/src/types/Dictionary.ts b/src/types/Dictionary.ts deleted file mode 100644 index a28a065..0000000 --- a/src/types/Dictionary.ts +++ /dev/null @@ -1 +0,0 @@ -export type Dictionary = Record; diff --git a/src/types/DictionaryCollection.ts b/src/types/DictionaryCollection.ts deleted file mode 100644 index 15f6845..0000000 --- a/src/types/DictionaryCollection.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { Dictionary } from "@/types"; - -export type DictionaryCollection = Record; diff --git a/src/types/PendingDictionary.ts b/src/types/PendingDictionary.ts deleted file mode 100644 index 1c767e6..0000000 --- a/src/types/PendingDictionary.ts +++ /dev/null @@ -1 +0,0 @@ -export type PendingDictionary = Record; diff --git a/src/types/index.ts b/src/types/index.ts deleted file mode 100644 index 6d74e62..0000000 --- a/src/types/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export type { DictionaryCollection } from "@/types/DictionaryCollection"; -export type { Dictionary } from "@/types/Dictionary"; -export type { PendingDictionary } from "@/types/PendingDictionary"; diff --git a/tests/dist.cjs.main.test.ts b/tests/dist.cjs.main.test.ts index 22adb12..42a407e 100644 --- a/tests/dist.cjs.main.test.ts +++ b/tests/dist.cjs.main.test.ts @@ -1,5 +1,5 @@ import { describe } from "vitest"; -import { LZString } from "../dist/index.cjs"; +import LZString from "../dist/index.cjs"; import { runAllTests } from "./testFunctions"; describe("dist/index.cjs (commonjs)", () => { diff --git a/tests/dist.es.main.test.ts b/tests/dist.es.main.test.ts index 2bf8427..7dba0ea 100644 --- a/tests/dist.es.main.test.ts +++ b/tests/dist.es.main.test.ts @@ -1,5 +1,5 @@ import { describe } from "vitest"; -import { LZString } from "../dist/index.js"; +import LZString from "../dist/index.js"; import { runAllTests } from "./testFunctions"; describe("dist/index.js (esmodule)", () => { diff --git a/tests/src.main.test.ts b/tests/src.main.test.ts index 1e8d9ae..54fcb4a 100644 --- a/tests/src.main.test.ts +++ b/tests/src.main.test.ts @@ -1,5 +1,5 @@ import { describe } from "vitest"; -import { LZString } from "../src/main"; +import LZString from "../src"; import { runAllTests } from "./testFunctions"; describe("src/ (uncompiled)", () => { diff --git a/tests/testFunctions.ts b/tests/testFunctions.ts index 591d469..eb5a36c 100644 --- a/tests/testFunctions.ts +++ b/tests/testFunctions.ts @@ -23,11 +23,7 @@ import type { LZString } from "../src/main"; * methods. */ export function runAllTests(implementation: LZString) { - runTestSet( - "Stock Compression and Decompression", - implementation.compress, - implementation.decompress, - ); + runTestSet("Stock Compression and Decompression", implementation.compress, implementation.decompress); runTestSet( "Base64 Compression and Decompression", @@ -69,13 +65,7 @@ export function runAllTests(implementation: LZString) { * - Compression makes thing smaller * - Check against a known good value */ -function runTestSet( - desc: string, - compressFunc, - decompressFunc, - compressedTattoo?, - testEncodedURI?: boolean, -) { +function runTestSet(desc: string, compressFunc, decompressFunc, compressedTattoo?, testEncodedURI?: boolean) { describe(desc, () => { test(`"Hello World"`, () => { const compressedHw = compressFunc(test_hw); @@ -134,13 +124,9 @@ function runTestSet( const test_randomString = test_randomString_fn(); // Unique per test const compressedRandomString = compressFunc(test_randomString); - expect(compressedRandomString).toEqual( - compressFunc(test_randomString), - ); + expect(compressedRandomString).toEqual(compressFunc(test_randomString)); expect(compressedRandomString).not.toEqual(test_randomString); - expect(decompressFunc(compressedRandomString)).toEqual( - test_randomString, - ); + expect(decompressFunc(compressedRandomString)).toEqual(test_randomString); }); const test_longString = test_longString_fn(); // Unique per run @@ -149,40 +135,28 @@ function runTestSet( test(`Long String`, () => { expect(compressedLongString).toEqual(compressFunc(test_longString)); expect(compressedLongString).not.toEqual(test_longString); - expect(compressedLongString.length).toBeLessThan( - test_longString.length, - ); - expect(decompressFunc(compressedLongString)).toEqual( - test_longString, - ); + expect(compressedLongString.length).toBeLessThan(test_longString.length); + expect(decompressFunc(compressedLongString)).toEqual(test_longString); }); if (testEncodedURI) { test(`All chars are URL safe`, () => { expect(compressedLongString.indexOf("=")).toBe(-1); expect(compressedLongString.indexOf("/")).toBe(-1); - expect(decompressFunc(compressedLongString)).toBe( - test_longString, - ); + expect(decompressFunc(compressedLongString)).toBe(test_longString); }); test(`+ and ' ' are interchangeable in decompression`, () => { - expect(test_tattooSource).toEqual( - decompressFunc(test_tattooEncodedURIComponent), - ); + expect(test_tattooSource).toEqual(decompressFunc(test_tattooEncodedURIComponent)); }); } if (compressedTattoo) { test(`expected compression result`, () => { - expect(compressFunc(test_tattooSource)).toEqual( - compressedTattoo, - ); + expect(compressFunc(test_tattooSource)).toEqual(compressedTattoo); }); test(`expected decompression result`, () => { - expect(decompressFunc(compressedTattoo)).toEqual( - test_tattooSource, - ); + expect(decompressFunc(compressedTattoo)).toEqual(test_tattooSource); }); } }); diff --git a/tests/testValues.ts b/tests/testValues.ts index f4bed3b..6d7cadf 100644 --- a/tests/testValues.ts +++ b/tests/testValues.ts @@ -81,45 +81,32 @@ export const test_tattooBase64 = /** Known output for Uint8 compression */ export const test_tattooUint8Array = new Uint8Array([ - 8, 133, 112, 78, 9, 96, 118, 14, 96, 4, 1, 112, 33, 130, 16, 123, 55, 70, 1, - 163, 180, 13, 103, 128, 206, 121, 64, 21, 128, 166, 3, 24, 33, 64, 38, 167, - 168, 128, 22, 21, 196, 126, 210, 237, 4, 8, 66, 150, 56, 72, 161, 224, 11, - 106, 36, 20, 54, 96, 41, 16, 0, 230, 138, 17, 10, 185, 132, 50, 161, 64, 13, - 166, 146, 84, 147, 111, 167, 0, 17, 40, 164, 84, 193, 163, 156, 201, 12, 89, - 70, 226, 139, 64, 13, 205, 180, 38, 9, 89, 9, 148, 136, 84, 6, 70, 40, 80, - 224, 64, 229, 236, 61, 92, 161, 240, 0, 232, 224, 0, 85, 61, 77, 205, 45, - 173, 109, 116, 144, 192, 192, 1, 61, 216, 209, 68, 216, 208, 0, 204, 88, 35, - 9, 221, 60, 0, 140, 208, 233, 50, 1, 200, 73, 53, 51, 68, 172, 224, 160, - 171, 101, 113, 202, 64, 16, 114, 242, 88, 131, 210, 216, 10, 32, 12, 24, 1, - 221, 121, 153, 73, 8, 137, 145, 178, 228, 187, 112, 41, 69, 203, 232, 233, - 13, 161, 139, 217, 56, 160, 99, 226, 80, 234, 225, 71, 173, 187, 77, 241, - 101, 55, 145, 80, 48, 224, 156, 32, 136, 33, 203, 52, 217, 36, 214, 193, 54, - 57, 160, 99, 129, 245, 152, 208, 65, 238, 216, 0, 85, 8, 11, 140, 15, 112, - 66, 212, 72, 0, 65, 39, 149, 14, 0, 3, 23, 209, 156, 160, 214, 81, 49, 14, - 6, 177, 114, 105, 44, 130, 31, 58, 14, 65, 3, 210, 104, 224, 230, 64, 154, - 35, 196, 21, 25, 160, 192, 248, 18, 57, 91, 44, 131, 2, 216, 248, 176, 77, - 162, 66, 197, 97, 179, 156, 41, 221, 107, 11, 142, 3, 37, 51, 65, 12, 65, - 112, 187, 23, 143, 146, 41, 139, 46, 232, 52, 12, 64, 7, 33, 69, 24, 56, - 204, 28, 148, 185, 209, 207, 200, 217, 48, 168, 138, 34, 8, 23, 166, 43, - 144, 201, 110, 127, 34, 3, 78, 0, 73, 129, 228, 160, 8, 0, 45, 16, 196, 98, - 170, 74, 115, 82, 190, 6, 56, 68, 74, 32, 128, 192, 192, 40, 54, 25, 77, - 128, 210, 106, 77, 90, 107, 34, 34, 197, 203, 105, 3, 232, 45, 200, 109, - 188, 22, 57, 182, 169, 177, 198, 30, 98, 168, 134, 36, 96, 3, 170, 176, 68, - 186, 166, 186, 80, 75, 196, 65, 160, 224, 154, 36, 50, 140, 7, 111, 42, 87, - 12, 50, 235, 160, 185, 207, 166, 224, 136, 142, 132, 201, 166, 79, 238, 192, - 160, 6, 42, 224, 37, 46, 12, 84, 67, 208, 100, 176, 67, 138, 166, 142, 235, - 67, 6, 182, 88, 119, 18, 93, 119, 10, 48, 160, 213, 249, 225, 159, 84, 129, - 131, 227, 161, 128, 64, 240, 30, 70, 45, 11, 34, 128, 213, 186, 222, 109, - 54, 79, 150, 192, 145, 81, 38, 133, 2, 157, 177, 156, 203, 128, 80, 10, 5, - 106, 2, 27, 15, 100, 241, 16, 144, 16, 58, 11, 54, 205, 87, 25, 5, 163, 65, - 186, 103, 194, 129, 101, 19, 40, 27, 36, 41, 54, 86, 140, 5, 49, 137, 15, - 159, 50, 208, 116, 92, 8, 131, 44, 187, 16, 16, 228, 80, 207, 30, 205, 129, - 241, 177, 110, 158, 14, 128, 10, 10, 220, 64, 17, 20, 24, 128, 4, 145, 16, - 10, 51, 11, 243, 129, 107, 101, 1, 132, 81, 54, 99, 77, 0, 208, 136, 18, 16, - 241, 92, 106, 80, 41, 137, 141, 47, 96, 158, 229, 129, 151, 54, 14, 135, - 194, 32, 230, 0, 134, 41, 64, 241, 155, 65, 98, 136, 216, 52, 128, 162, 144, - 42, 47, 128, 227, 250, 101, 45, 67, 193, 186, 42, 68, 4, 209, 183, 26, 4, - 72, 180, 86, 95, 15, 131, 181, 200, 202, 52, 199, 64, 178, 40, 136, 0, 0, + 8, 133, 112, 78, 9, 96, 118, 14, 96, 4, 1, 112, 33, 130, 16, 123, 55, 70, 1, 163, 180, 13, 103, 128, 206, 121, 64, + 21, 128, 166, 3, 24, 33, 64, 38, 167, 168, 128, 22, 21, 196, 126, 210, 237, 4, 8, 66, 150, 56, 72, 161, 224, 11, + 106, 36, 20, 54, 96, 41, 16, 0, 230, 138, 17, 10, 185, 132, 50, 161, 64, 13, 166, 146, 84, 147, 111, 167, 0, 17, 40, + 164, 84, 193, 163, 156, 201, 12, 89, 70, 226, 139, 64, 13, 205, 180, 38, 9, 89, 9, 148, 136, 84, 6, 70, 40, 80, 224, + 64, 229, 236, 61, 92, 161, 240, 0, 232, 224, 0, 85, 61, 77, 205, 45, 173, 109, 116, 144, 192, 192, 1, 61, 216, 209, + 68, 216, 208, 0, 204, 88, 35, 9, 221, 60, 0, 140, 208, 233, 50, 1, 200, 73, 53, 51, 68, 172, 224, 160, 171, 101, + 113, 202, 64, 16, 114, 242, 88, 131, 210, 216, 10, 32, 12, 24, 1, 221, 121, 153, 73, 8, 137, 145, 178, 228, 187, + 112, 41, 69, 203, 232, 233, 13, 161, 139, 217, 56, 160, 99, 226, 80, 234, 225, 71, 173, 187, 77, 241, 101, 55, 145, + 80, 48, 224, 156, 32, 136, 33, 203, 52, 217, 36, 214, 193, 54, 57, 160, 99, 129, 245, 152, 208, 65, 238, 216, 0, 85, + 8, 11, 140, 15, 112, 66, 212, 72, 0, 65, 39, 149, 14, 0, 3, 23, 209, 156, 160, 214, 81, 49, 14, 6, 177, 114, 105, + 44, 130, 31, 58, 14, 65, 3, 210, 104, 224, 230, 64, 154, 35, 196, 21, 25, 160, 192, 248, 18, 57, 91, 44, 131, 2, + 216, 248, 176, 77, 162, 66, 197, 97, 179, 156, 41, 221, 107, 11, 142, 3, 37, 51, 65, 12, 65, 112, 187, 23, 143, 146, + 41, 139, 46, 232, 52, 12, 64, 7, 33, 69, 24, 56, 204, 28, 148, 185, 209, 207, 200, 217, 48, 168, 138, 34, 8, 23, + 166, 43, 144, 201, 110, 127, 34, 3, 78, 0, 73, 129, 228, 160, 8, 0, 45, 16, 196, 98, 170, 74, 115, 82, 190, 6, 56, + 68, 74, 32, 128, 192, 192, 40, 54, 25, 77, 128, 210, 106, 77, 90, 107, 34, 34, 197, 203, 105, 3, 232, 45, 200, 109, + 188, 22, 57, 182, 169, 177, 198, 30, 98, 168, 134, 36, 96, 3, 170, 176, 68, 186, 166, 186, 80, 75, 196, 65, 160, + 224, 154, 36, 50, 140, 7, 111, 42, 87, 12, 50, 235, 160, 185, 207, 166, 224, 136, 142, 132, 201, 166, 79, 238, 192, + 160, 6, 42, 224, 37, 46, 12, 84, 67, 208, 100, 176, 67, 138, 166, 142, 235, 67, 6, 182, 88, 119, 18, 93, 119, 10, + 48, 160, 213, 249, 225, 159, 84, 129, 131, 227, 161, 128, 64, 240, 30, 70, 45, 11, 34, 128, 213, 186, 222, 109, 54, + 79, 150, 192, 145, 81, 38, 133, 2, 157, 177, 156, 203, 128, 80, 10, 5, 106, 2, 27, 15, 100, 241, 16, 144, 16, 58, + 11, 54, 205, 87, 25, 5, 163, 65, 186, 103, 194, 129, 101, 19, 40, 27, 36, 41, 54, 86, 140, 5, 49, 137, 15, 159, 50, + 208, 116, 92, 8, 131, 44, 187, 16, 16, 228, 80, 207, 30, 205, 129, 241, 177, 110, 158, 14, 128, 10, 10, 220, 64, 17, + 20, 24, 128, 4, 145, 16, 10, 51, 11, 243, 129, 107, 101, 1, 132, 81, 54, 99, 77, 0, 208, 136, 18, 16, 241, 92, 106, + 80, 41, 137, 141, 47, 96, 158, 229, 129, 151, 54, 14, 135, 194, 32, 230, 0, 134, 41, 64, 241, 155, 65, 98, 136, 216, + 52, 128, 162, 144, 42, 47, 128, 227, 250, 101, 45, 67, 193, 186, 42, 68, 4, 209, 183, 26, 4, 72, 180, 86, 95, 15, + 131, 181, 200, 202, 52, 199, 64, 178, 40, 136, 0, 0, ]); /** Known output for UTF16 compression */ diff --git a/tsconfig.json b/tsconfig.json index 9a74af6..9fb7eb2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,9 +1,8 @@ { "compilerOptions": { "target": "ESNext", - "useDefineForClassFields": true, "module": "ESNext", - "lib": ["ESNext", "DOM", "DOM.Iterable"], + "lib": ["ESNext"], "skipLibCheck": true, /* Bundler mode */ @@ -12,22 +11,14 @@ "declaration": true, "declarationMap": true, "moduleResolution": "Node", - "resolveJsonModule": true, - "isolatedModules": true, "noEmit": true, /* Linting */ "strict": true, "noUnusedLocals": true, "noUnusedParameters": true, - "noFallthroughCasesInSwitch": true, - "baseUrl": ".", - "paths": { - "@/*": ["src/*"] - }, - "typeRoots": ["./src/interfaces", "./src/types"] + "baseUrl": "." }, - "include": ["src"], - "exclude": ["node_modules", "tests"] + "include": ["src"] } diff --git a/vite.config.ts b/vite.config.ts index d241218..f0a659a 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,23 +1,45 @@ /// -import { resolve } from "path"; +import { parse, relative, resolve } from "path"; import { defineConfig } from "vite"; import dts from "vite-plugin-dts"; import eslint from "vite-plugin-eslint"; import tsconfigPaths from "vite-tsconfig-paths"; +const root = resolve(__dirname, "src"); + export default defineConfig({ - base: "./", plugins: [tsconfigPaths(), dts({ rollupTypes: true }), eslint()], build: { minify: true, reportCompressedSize: true, sourcemap: true, lib: { - entry: resolve(__dirname, "src/main.ts"), + entry: resolve(__dirname, "src/index.ts"), fileName: "index", - formats: ["es", "cjs", "umd"], - name: "LZString", + }, + rollupOptions: { + output: [ + { + format: "cjs", + name: "LZString", + entryFileNames: "[name].cjs", + }, + { + format: "es", + chunkFileNames: "[name].js", + manualChunks: (id: string) => { + const { dir, name } = parse(relative(root, id)); + + // If it's in node_modules then don't export + return dir.startsWith(".") ? null : name || "common"; + }, + }, + { + format: "umd", + name: "LZString", + entryFileNames: "[name].[format].js", + }, + ], }, }, - test: {}, });