Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues and v2 work updates #189

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 || "";
}
33 changes: 33 additions & 0 deletions src/base64/betterBase64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { _compress } from "../_compress";
import keyStrBase64 from "./keyStrBase64";
import { _decompress } from "../_decompress";
import { getBaseValue } from "../getBaseValue";

export function compressToBetterBase64(input: string): string {
if (!input) {
return "";
}
const res = _compress(input, 6, (a) => keyStrBase64.charAt(a));

// To produce valid Base64
switch (res.length % 3) {
case 0:
return res;
case 1:
return res + "==";
case 2:
return res + "=";
default: // When could this happen ?
console.warn("Something in compressToBetterBase64() is very very wrong.");
return "";
}

}

export function decompressFromBetterBase64(input: string): string {
if (!input) {
return "";
}
const res = _decompress(input.length, 32, (index) => getBaseValue(keyStrBase64, input.charAt(index)));
return res || "";
}
2 changes: 2 additions & 0 deletions src/base64/compressToBase64.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { _compress } from "../_compress";
import keyStrBase64 from "./keyStrBase64";
import { deprecated } from "../utils/misc";

export function compressToBase64(input: string | null): string {
deprecated("compressToBase64()", "v2.0.0", { replacement: "compressToBetterBase64()"})
if (input == null) {
return "";
}
Expand Down
2 changes: 2 additions & 0 deletions src/base64/decompressFromBase64.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { _decompress } from "../_decompress";
import { getBaseValue } from "../getBaseValue";
import keyStrBase64 from "./keyStrBase64";
import { deprecated } from "../utils/misc";

export function decompressFromBase64(input: string | null) {
deprecated("decompressFromBase64()", "v2.0.0", { replacement: "decompressFromBetterBase64()"})
if (input == null) return "";
if (input == "") return null;

Expand Down
2 changes: 2 additions & 0 deletions src/base64/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +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
13 changes: 12 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 } 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 @@ -25,6 +32,10 @@ export default {

compressToBase64,
decompressFromBase64,
compressToBetterBase64,
decompressFromBetterBase64,
compressToBase64URL,
decompressFromBase64URL,

compressToEncodedURIComponent,
decompressFromEncodedURIComponent,
Expand Down
14 changes: 14 additions & 0 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type VERSIONS = "v2.0.0"

export function deprecated(
thing: string,
version: VERSIONS,
opts?: { aggressive?: boolean; replacement?: string }
): void {
let notice = `LZString | ${thing} is deprecated as of: ${version}`
if (opts?.replacement) {
notice += ` - Please use ${opts.replacement} instead`
}
console.error(notice)
if (window && opts?.aggressive) alert(notice)

Check failure on line 13 in src/utils/misc.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Cannot find name 'window'.

Check failure on line 13 in src/utils/misc.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Cannot find name 'alert'.

Check failure on line 13 in src/utils/misc.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Cannot find name 'window'.

Check failure on line 13 in src/utils/misc.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Cannot find name 'alert'.
Rycochet marked this conversation as resolved.
Show resolved Hide resolved
}
14 changes: 14 additions & 0 deletions tests/testFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ export function runAllTests(implementation: typeof LZString) {
test_tattooBase64,
);

runTestSet(
"BetterBase64 Compression and Decompression",
implementation.compressToBetterBase64,
implementation.decompressFromBetterBase64,
test_tattooBase64,
);

runTestSet(
"Base64URL Compression and Decompression",
implementation.compressToBase64URL,
implementation.decompressFromBase64URL,
test_tattooBase64,
);

runTestSet(
"UTF16 Compression and Decompression",
implementation.compressToUTF16,
Expand Down
Loading