Skip to content

Commit

Permalink
Chore: implements Base64URL suggestion (/pieroxy/pull/127)
Browse files Browse the repository at this point in the history
  • Loading branch information
karnthis committed Jan 17, 2024
1 parent aec06d2 commit 32e6ed1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/base64/base64URL.ts
Original file line number Diff line number Diff line change
@@ -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 || "";
}
1 change: 1 addition & 0 deletions src/base64/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { compressToBase64 } from "./compressToBase64";
export { decompressFromBase64 } from "./decompressFromBase64";
export { compressToBetterBase64, decompressFromBetterBase64 } from "./betterBase64";
export { compressToBase64URL, decompressFromBase64URL } from "./base64URL";
2 changes: 2 additions & 0 deletions src/encodedURI/compressToEncodedURI.ts
Original file line number Diff line number Diff line change
@@ -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));
Expand Down
2 changes: 2 additions & 0 deletions src/encodedURI/decompressFromEncodedURI.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -27,6 +34,8 @@ export default {
decompressFromBase64,
compressToBetterBase64,
decompressFromBetterBase64,
compressToBase64URL,
decompressFromBase64URL,

compressToEncodedURIComponent,
decompressFromEncodedURIComponent,
Expand Down
7 changes: 7 additions & 0 deletions tests/testFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 32e6ed1

Please sign in to comment.