From 32e6ed15741a38a1036eccbfb7628a4ba98ec7c8 Mon Sep 17 00:00:00 2001 From: Erin Rivas Date: Wed, 17 Jan 2024 01:40:00 -0600 Subject: [PATCH] Chore: implements Base64URL suggestion (/pieroxy/lz-string/pull/127) --- src/base64/base64URL.ts | 20 ++++++++++++++++++++ src/base64/index.ts | 1 + src/encodedURI/compressToEncodedURI.ts | 2 ++ src/encodedURI/decompressFromEncodedURI.ts | 2 ++ src/index.ts | 11 ++++++++++- tests/testFunctions.ts | 7 +++++++ 6 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/base64/base64URL.ts diff --git a/src/base64/base64URL.ts b/src/base64/base64URL.ts new file mode 100644 index 0000000..f22721e --- /dev/null +++ b/src/base64/base64URL.ts @@ -0,0 +1,20 @@ +import { _compress } from "../_compress"; +import { _decompress } from "../_decompress"; +import { getBaseValue } from "../getBaseValue"; + +const keyStrBase64URL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; + +export function compressToBase64URL(input: string): string { + if (!input) { + return ""; + } + return _compress(input, 6, (a) => keyStrBase64URL.charAt(a)); +} + +export function decompressFromBase64URL(input: string): string { + if (!input) { + return ""; + } + const res = _decompress(input.length, 32, (index) => getBaseValue(keyStrBase64URL, input.charAt(index))); + return res || ""; +} diff --git a/src/base64/index.ts b/src/base64/index.ts index 386c55c..bf94bf9 100644 --- a/src/base64/index.ts +++ b/src/base64/index.ts @@ -1,3 +1,4 @@ export { compressToBase64 } from "./compressToBase64"; export { decompressFromBase64 } from "./decompressFromBase64"; export { compressToBetterBase64, decompressFromBetterBase64 } from "./betterBase64"; +export { compressToBase64URL, decompressFromBase64URL } from "./base64URL"; diff --git a/src/encodedURI/compressToEncodedURI.ts b/src/encodedURI/compressToEncodedURI.ts index 5408df6..cbe292e 100644 --- a/src/encodedURI/compressToEncodedURI.ts +++ b/src/encodedURI/compressToEncodedURI.ts @@ -1,7 +1,9 @@ import { _compress } from "../_compress"; import keyStrUriSafe from "./keyStrUriSafe"; +import { deprecated } from "../utils/misc"; export function compressToEncodedURIComponent(input: string | null) { + deprecated("compressToEncodedURIComponent()", "v2.0.0", { replacement: "compressToBase64URL()"}) if (input == null) return ""; return _compress(input, 6, (a) => keyStrUriSafe.charAt(a)); diff --git a/src/encodedURI/decompressFromEncodedURI.ts b/src/encodedURI/decompressFromEncodedURI.ts index 3bb3e70..85898d2 100644 --- a/src/encodedURI/decompressFromEncodedURI.ts +++ b/src/encodedURI/decompressFromEncodedURI.ts @@ -1,8 +1,10 @@ import keyStrUriSafe from "./keyStrUriSafe"; import { _decompress } from "../_decompress"; import { getBaseValue } from "../getBaseValue"; +import { deprecated } from "../utils/misc"; export function decompressFromEncodedURIComponent(input: string | null) { + deprecated("decompressFromEncodedURIComponent()", "v2.0.0", { replacement: "decompressFromBase64URL()"}) if (input == null) return ""; if (input == "") return null; diff --git a/src/index.ts b/src/index.ts index e0603d4..c33a018 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,7 +10,14 @@ import { _compress } from "./_compress"; import { _decompress } from "./_decompress"; -import { compressToBase64, decompressFromBase64, compressToBetterBase64, decompressFromBetterBase64 } from "./base64"; +import { + compressToBase64, + decompressFromBase64, + compressToBetterBase64, + decompressFromBetterBase64, + compressToBase64URL, + decompressFromBase64URL, +} from "./base64"; import { compressToEncodedURIComponent, decompressFromEncodedURIComponent } from "./encodedURI"; import { compress, decompress } from "./stock"; import { compressToUint8Array, decompressFromUint8Array } from "./Uint8Array"; @@ -27,6 +34,8 @@ export default { decompressFromBase64, compressToBetterBase64, decompressFromBetterBase64, + compressToBase64URL, + decompressFromBase64URL, compressToEncodedURIComponent, decompressFromEncodedURIComponent, diff --git a/tests/testFunctions.ts b/tests/testFunctions.ts index 2e5da20..c29bfff 100644 --- a/tests/testFunctions.ts +++ b/tests/testFunctions.ts @@ -39,6 +39,13 @@ export function runAllTests(implementation: typeof LZString) { test_tattooBase64, ); + runTestSet( + "Base64URL Compression and Decompression", + implementation.compressToBase64URL, + implementation.decompressFromBase64URL, + test_tattooBase64, + ); + runTestSet( "UTF16 Compression and Decompression", implementation.compressToUTF16,