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 9 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"build": "tsc && vite build",
"format": "prettier --write .",
"start": "vite build --watch",
"test": "[ -f ./dist/index.js ] || npm run build && vitest",
"test": "[ -f ./dist/index.js ] || npm run test:build",
"test:build": "npm run build && vitest",
Comment on lines +57 to +58
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't doing quite what you expect - previously it was running the build if it hadn't run already - we actually don't need that conditional run, so can just have vitest on its own - I was mixing up vite preview and vitest!

Suggested change
"test": "[ -f ./dist/index.js ] || npm run test:build",
"test:build": "npm run build && vitest",
"start:test": "vitest",
"test": "vitest run",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run build is required to capture changes to the codebase because tests run specifically against the files in /dist, and without it the test system will only reflect changes to the tests themselves. test:build forces a build of the codebase to ensure all tests run successfully against the latest code.

if there is another way to capture code changes in list I would love to use it because this honestly isn't great.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point - it still needs that ternary for the moment then - I'm about to be pushing a test update that tests the built cjs (currently) via the new commandline tool and a bash script (the intention is that other implementations can support the same arguments and then run the same tests) - currently I can't get nodejs to load binary data properly though, so raw and uint8array both fail to decode :-(

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the test code has been updated and fixed, so it works properly - the test strategy will be changing slightly for the production code so it'll only run when it can (and force it on github itself) - so I'll clean up this when I get to it - can undo the change in the meantime :-)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can now be removed - the test stategy has been updated (including better tests for built code, and an external test tool here - I'll get it running in github actions soon!)

"test:coverage": "vitest run src.main.test.ts --coverage",
"lint": "eslint ."
},
Expand Down
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
13 changes: 13 additions & 0 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type VERSIONS = "v2.0.0"

export function deprecated(
thing: string,
version: VERSIONS,
opts?: { replacement?: string }
): void {
let notice = `LZString | ${thing} is deprecated as of: ${version}`
if (opts?.replacement) {
notice += ` - Please use ${opts.replacement} instead`
}
console.error(notice)
}
7 changes: 7 additions & 0 deletions tests/suite/compressTattoo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from "vitest";

export function compressKnownString_test(compressFunc, data, compressedData) {
test(`expected compression result`, () => {
expect(compressFunc(data)).toEqual(compressedData);
});
}
7 changes: 7 additions & 0 deletions tests/suite/decompressTattoo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from "vitest";

export function decompressKnownString_test(decompressFunc, data, compressedData) {
test(`expected decompression result`, () => {
expect(decompressFunc(compressedData)).toEqual(data);
});
}
27 changes: 27 additions & 0 deletions tests/suite/emptyResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { expect, test } from "vitest";

export function emptyShouldBeEmptyResponse_test(compressFunc, decompressFunc, data) {
test(`"" (empty string)`, () => {
const compressedEmpty = compressFunc(data);

expect(compressedEmpty).toEqual(compressFunc(data));
expect(compressedEmpty).toEqual("");
compressedEmpty instanceof Uint8Array
? expect(compressedEmpty.length).not.toBe(0)
: expect(typeof compressedEmpty).toBe("string");
expect(decompressFunc(compressedEmpty)).toEqual(data);
});
}

export function emptyShouldNotBeEmptyResponse_test(compressFunc, decompressFunc, data) {
test(`"" (empty string)`, () => {
const compressedEmpty = compressFunc(data);

expect(compressedEmpty).toEqual(compressFunc(data));
expect(compressedEmpty).not.toEqual("");
compressedEmpty instanceof Uint8Array
? expect(compressedEmpty.length).not.toBe(0)
: expect(typeof compressedEmpty).toBe("string");
expect(decompressFunc(compressedEmpty)).toEqual(data);
});
}
15 changes: 15 additions & 0 deletions tests/suite/helloWorld.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect, test } from "vitest";

export function helloWorld_test(compressFunc, decompressFunc, data) {
test(`"Hello World"`, () => {
const compressedHw = compressFunc(data);

expect(compressedHw).toEqual(compressFunc(data));
expect(compressedHw).not.toEqual(data);
expect(decompressFunc(compressedHw)).toEqual(data);
});
}




7 changes: 7 additions & 0 deletions tests/suite/inchangableChars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from "vitest";

export function interchangableChars_test(decompressFunc, data, compressedData) {
test(`+ and ' ' are interchangeable in decompression`, () => {
expect(data).toEqual(decompressFunc(compressedData));
});
}
12 changes: 12 additions & 0 deletions tests/suite/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export * from "./helloWorld";
export * from "./nullResponse";
export * from "./emptyResponse";
export * from "./undefinedResponse";
export * from "./utf16Response";
export * from "./repeatingString";
export * from "./randomString";
export * from "./longString";
export * from "./urlSafe";
export * from "./inchangableChars";
export * from "./compressTattoo";
export * from "./decompressTattoo";
10 changes: 10 additions & 0 deletions tests/suite/longString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect, test } from "vitest";

export function longString_test(compressFunc, decompressFunc, data, compressedData) {
test(`Long String`, () => {
expect(compressedData).toEqual(compressFunc(data));
expect(compressedData).not.toEqual(data);
expect(compressedData.length).toBeLessThan(data.length);
expect(decompressFunc(compressedData)).toEqual(data);
});
}
18 changes: 18 additions & 0 deletions tests/suite/nullResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect, test } from "vitest";

export function nullShouldNotBeNullResponse_test(compressFunc, decompressFunc, data) {
test(`null`, () => {
const compressedNull = compressFunc(data);

compressedNull instanceof Uint8Array
? expect(compressedNull.length).toBe(0)
: expect(compressedNull).toEqual("");
});
}

export function nullShouldBeNullResponse_test(compressFunc, decompressFunc, data) {
test(`null`, () => {
const compressedNull = compressFunc(data);
expect(compressedNull).toEqual(null);
});
}
14 changes: 14 additions & 0 deletions tests/suite/randomString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect, test } from "vitest";
import { test_randomString_fn } from "../testValues";

export function randomString_test(compressFunc, decompressFunc) {
// Note that this is designed to be uncompressible
test(`Random String`, () => {
const test_randomString = test_randomString_fn(); // Unique per test
const compressedRandomString = compressFunc(test_randomString);

expect(compressedRandomString).toEqual(compressFunc(test_randomString));
expect(compressedRandomString).not.toEqual(test_randomString);
expect(decompressFunc(compressedRandomString)).toEqual(test_randomString);
});
}
12 changes: 12 additions & 0 deletions tests/suite/repeatingString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { expect, test } from "vitest";

export function repeatingString_test(compressFunc, decompressFunc, data) {
test(`Repeating String`, () => {
const compressedRepeat = compressFunc(data);

expect(compressedRepeat).toEqual(compressFunc(data));
expect(compressedRepeat).not.toEqual(data);
expect(compressedRepeat.length).toBeLessThan(data.length);
expect(decompressFunc(compressedRepeat)).toEqual(data);
});
}
11 changes: 11 additions & 0 deletions tests/suite/undefinedResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect, test } from "vitest";

export function undefinedResponse_test(compressFunc, decompressFunc, data) {
test(`undefined`, () => {
const compressedUndefined = compressFunc(data);

compressedUndefined instanceof Uint8Array
? expect(compressedUndefined.length).toBe(0)
: expect(compressedUndefined).toBe("");
});
}
9 changes: 9 additions & 0 deletions tests/suite/urlSafe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect, test } from "vitest";

export function allCharsUrlSafe_test(compressFunc, decompressFunc, data, compressedData) {
test(`All chars are URL safe`, () => {
expect(compressedData.indexOf("=")).toBe(-1);
expect(compressedData.indexOf("/")).toBe(-1);
expect(decompressFunc(compressedData)).toBe(data);
});
}
11 changes: 11 additions & 0 deletions tests/suite/utf16Response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect, test } from "vitest";

export function utf16Response_test(compressFunc, decompressFunc, data) {
test(`utf16`, () => {
const compressedUtf16 = compressFunc(data);

expect(compressedUtf16).toEqual(compressFunc(data));
expect(compressedUtf16).not.toEqual(data);
expect(decompressFunc(compressedUtf16)).toEqual(data);
});
}
Loading