Skip to content

Commit 106ebfd

Browse files
committed
Cleanup tests part 2
1 parent 14a6bb9 commit 106ebfd

File tree

11 files changed

+197
-7
lines changed

11 files changed

+197
-7
lines changed

bin/cli.cjs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#! /usr/bin/env node
2+
/* eslint-disable @typescript-eslint/no-var-requires */
3+
4+
const lzString = require("../dist/index.cjs");
5+
const fs = require("fs");
6+
const pkg = require("../package.json");
7+
const { Option, program } = require("commander");
8+
9+
function compressContent(format, content) {
10+
switch (format) {
11+
case "base64":
12+
return lzString.compressToBase64(content);
13+
case "encodeduri":
14+
return lzString.compressToEncodedURIComponent(content);
15+
case "raw":
16+
default:
17+
return lzString.compress(content);
18+
case "uint8array":
19+
return lzString.compressToUint8Array(content);
20+
case "utf16":
21+
return lzString.compressToUTF16(content);
22+
}
23+
}
24+
25+
function decompressContent(format, content) {
26+
switch (format) {
27+
case "base64":
28+
return lzString.decompressFromBase64(content);
29+
case "encodeduri":
30+
return lzString.decompressFromEncodedURIComponent(content);
31+
case "raw":
32+
default:
33+
return lzString.decompress(content);
34+
case "uint8array":
35+
return lzString.decompressFromUint8Array(content);
36+
case "utf16":
37+
return lzString.decompressFromUTF16(content);
38+
}
39+
}
40+
41+
program
42+
.version(pkg.version)
43+
.description("Use lz-string to compress or decompress a file")
44+
.addOption(new Option("-d, --decompress", "if unset then this will compress"))
45+
.addOption(
46+
new Option("-f, --format <type>", "formatter to use")
47+
.choices(["base64", "encodeduri", "raw", "uint8array", "utf16"])
48+
.default("raw"),
49+
)
50+
.addOption(new Option("-v, --validate", "validate before returning").default(true))
51+
.addOption(new Option("-o, --output <output-file>", "output file"))
52+
.addOption(new Option("-q, --quiet", "don't print any error messages"))
53+
.argument("<input-file>", "file to process")
54+
.showHelpAfterError()
55+
.action((file, { format, decompress, output, quiet, validate }) => {
56+
if (!fs.existsSync(file)) {
57+
if (!quiet) process.stderr.write(`Unable to find ${file}`);
58+
process.exit(1);
59+
}
60+
try {
61+
fs.accessSync(file, fs.constants.R_OK);
62+
} catch {
63+
if (!quiet) process.stderr.write(`Unable to read ${file}`);
64+
process.exit(1);
65+
}
66+
const unprocessed = fs.readFileSync(file).toString();
67+
const processed = decompress ? decompressContent(format, unprocessed) : compressContent(format, unprocessed);
68+
69+
if (validate) {
70+
const validated = decompress ? compressContent(format, processed) : decompressContent(format, processed);
71+
let valid = unprocessed.length === validated.length;
72+
73+
for (let i = 0; valid && i < unprocessed.length; i++) {
74+
if (unprocessed[i] !== validated[i]) {
75+
valid = false;
76+
}
77+
}
78+
if (!valid) {
79+
if (!quiet) process.stderr.write(`Unable to validate ${file}`);
80+
process.exit(1);
81+
}
82+
}
83+
if (processed == null) {
84+
if (!quiet) process.stderr.write(`Unable to process ${file}`);
85+
process.exit(1);
86+
}
87+
if (output) {
88+
fs.writeFileSync(output, processed, null);
89+
} else {
90+
process.stdout.write(processed);
91+
}
92+
})
93+
.parse();

package-lock.json

Lines changed: 20 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
"compression",
3131
"string"
3232
],
33+
"bin": {
34+
"lz-string": "bin/cli.cjs"
35+
},
3336
"exports": {
3437
".": {
3538
"types": "./dist/index.d.ts",
@@ -42,6 +45,7 @@
4245
"module": "./dist/index.js",
4346
"types": "./dist/index.d.ts",
4447
"files": [
48+
"bin",
4549
"dist"
4650
],
4751
"scripts": {
@@ -70,5 +74,8 @@
7074
},
7175
"override": {
7276
"prettier": "npm:@btmills/[email protected]"
77+
},
78+
"dependencies": {
79+
"commander": "11.1.0"
7380
}
7481
}

src/__tests__/testFunctions.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export const test_longString_fn = () => {
5555
return testValue.join(" ");
5656
};
5757

58-
5958
/**
6059
* This will run a series of tests against each compress / decompress pair.
6160
*

test.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#! /usr/bin/env bash
2+
3+
ANSI_RESET="\033[0m"
4+
5+
FG_RED="\033[31m"
6+
FG_GREEN="\033[32m"
7+
FG_YELLOW="\033[33m"
8+
9+
Help() {
10+
echo -e "\nUsage: $0 [-u]"
11+
echo "-h: Show this help"
12+
echo "-u: Update any failed tests"
13+
exit
14+
}
15+
16+
OPT_UPDATE=0
17+
18+
# ---===###===--- Code starts here ---===###===--- #
19+
20+
while getopts ":hu" opt; do
21+
case ${opt} in
22+
"h") Help ;;
23+
"u") OPT_UPDATE=1 ; echo "Updating test files" ;;
24+
esac
25+
done
26+
27+
# file1, file2, success
28+
compare() {
29+
if cmp -s $1 $2; then
30+
printf " ...${FG_GREEN}%s${ANSI_RESET}" "pass"
31+
return 0
32+
else
33+
printf " ...${FG_RED}%s${ANSI_RESET}" "fail"
34+
return 1
35+
fi
36+
}
37+
38+
# format, folder
39+
process() {
40+
OUTPUT=$(mktemp)
41+
VALIDATE=$(mktemp)
42+
43+
printf "\nCompress: %-12s " $1
44+
node bin/cli.cjs -v -q -f $1 testdata/$2/source.bin -o $OUTPUT
45+
if ! compare testdata/$2/$1.bin $OUTPUT; then
46+
node bin/cli.cjs -q -d -f $1 $OUTPUT -o $VALIDATE
47+
if cmp -s $VALIDATE testdata/$2/source.bin; then
48+
if [ ! -f testdata/$2/$1.bin ] || [ $OPT_UPDATE -eq 1 ]; then
49+
cp $OUTPUT testdata/$2/$1.bin
50+
printf " ...${FG_YELLOW}%s${ANSI_RESET}" "updated"
51+
else
52+
printf " ...${FG_RED}%s${ANSI_RESET}" "unsafe"
53+
fi
54+
else
55+
printf " ...${FG_RED}%s${ANSI_RESET}" "validation failed"
56+
fi
57+
fi
58+
59+
printf "\nDecompress: %-12s " $1
60+
node bin/cli.cjs -v -q -d -f $1 testdata/$2/$1.bin -o $OUTPUT
61+
compare testdata/$2/source.bin $OUTPUT
62+
63+
rm $OUTPUT $VALIDATE
64+
}
65+
66+
# process raw tattoo
67+
process base64 tattoo
68+
process encodeduri tattoo
69+
# process uint8array tattoo
70+
process utf16 tattoo
71+
72+
printf "\n\nDone\n"

testdata/tattoo/base64.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CIVwTglgdg5gBAFwIYIQezdGAaO0DWeAznlAFYCmAxghQCanqIAWFcR+0u0ECEKWOEih4AtqJBQ2YCkQAOaKEQq5hDKhQA2mklSTb6cAESikVMGjnMkMWUbii0ANzbQmCVkJlIhUBkYoUOBA5ew9XKHwAOjgAFU9Tc0trW10kMDAAT3Y0UTY0ADMWCMJ3TwAjNDpMgHISTUzRKzgoKtlccpAEHLyWIPS2AogDBgB3XmZSQiJkbLku3ApRcvo6Q2hi9k4oGPiUOrhR627TfFlN5FQMOCcIIghyzTZJNbBNjmgY4H1mNBB7tgAVQgLjA9wQtRIAEEnlQ4AAxfRnKDWUTEOBrFyaSyCHzoOQQPSaODmQJojxBUZoMD4EjlbLIMC2PiwTaJCxWGznCndawuOAyUzQQxBcLsXj5Ipiy7oNAxAByFFGDjMHJS50c/I2TCoiiIIF6YrkMlufyIDTgBJgeSgCAAtEMRiqkpzUr4GOERKIIDAwCg2GU2A0mpNWmsiIsXLaQPoLchtvBY5tqmxxh5iqIYkYAOqsES6prpQS8RBoOCaJDKMB28qVwwy66C5z6bgiI6EyaZP7sCgBirgJS4MVEPQZLBDiqaO60MGtlh3El13CjCg1fnhn1SBg+OhgEDwHkYtCyKA1brebTZPlsCRUSaFAp2xnMuAUAoFagIbD2TxEJAQOgs2zVcZBaNBumfCgWUTKBskKTZWjAUxiQ+fMtB0XAiDLLsQEORQzx7NgfGxbp4OgAoK3EARFBiABJEQCjML84FrZQGEUTZjTQDQiBIQ8VxqUCmJjS9gnuWBlzYOh8Ig5gCGKUDxm0FiiNg0gKKQKi+A4/plLUPBuipEBNG3GgRItFZfD4O1yMo0x0CyKIgA

testdata/tattoo/base64x.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CIVwTglgdg5gBAFwIYIQezdGAaO0DWeAznlAFYCmAxghQCanqIAWFcR 0u0ECEKWOEih4AtqJBQ2YCkQAOaKEQq5hDKhQA2mklSTb6cAESikVMGjnMkMWUbii0ANzbQmCVkJlIhUBkYoUOBA5ew9XKHwAOjgAFU9Tc0trW10kMDAAT3Y0UTY0ADMWCMJ3TwAjNDpMgHISTUzRKzgoKtlccpAEHLyWIPS2AogDBgB3XmZSQiJkbLku3ApRcvo6Q2hi9k4oGPiUOrhR627TfFlN5FQMOCcIIghyzTZJNbBNjmgY4H1mNBB7tgAVQgLjA9wQtRIAEEnlQ4AAxfRnKDWUTEOBrFyaSyCHzoOQQPSaODmQJojxBUZoMD4EjlbLIMC2PiwTaJCxWGznCndawuOAyUzQQxBcLsXj5Ipiy7oNAxAByFFGDjMHJS50c-I2TCoiiIIF6YrkMlufyIDTgBJgeSgCAAtEMRiqkpzUr4GOERKIIDAwCg2GU2A0mpNWmsiIsXLaQPoLchtvBY5tqmxxh5iqIYkYAOqsES6prpQS8RBoOCaJDKMB28qVwwy66C5z6bgiI6EyaZP7sCgBirgJS4MVEPQZLBDiqaO60MGtlh3El13CjCg1fnhn1SBg OhgEDwHkYtCyKA1brebTZPlsCRUSaFAp2xnMuAUAoFagIbD2TxEJAQOgs2zVcZBaNBumfCgWUTKBskKTZWjAUxiQ fMtB0XAiDLLsQEORQzx7NgfGxbp4OgAoK3EARFBiABJEQCjML84FrZQGEUTZjTQDQiBIQ8VxqUCmJjS9gnuWBlzYOh8Ig5gCGKUDxm0FiiNg0gKKQKi A4-plLUPBuipEBNG3GgRItFZfD4O1yMo0x0CyKIgAAA$$

testdata/tattoo/encodeduri.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CIVwTglgdg5gBAFwIYIQezdGAaO0DWeAznlAFYCmAxghQCanqIAWFcR+0u0ECEKWOEih4AtqJBQ2YCkQAOaKEQq5hDKhQA2mklSTb6cAESikVMGjnMkMWUbii0ANzbQmCVkJlIhUBkYoUOBA5ew9XKHwAOjgAFU9Tc0trW10kMDAAT3Y0UTY0ADMWCMJ3TwAjNDpMgHISTUzRKzgoKtlccpAEHLyWIPS2AogDBgB3XmZSQiJkbLku3ApRcvo6Q2hi9k4oGPiUOrhR627TfFlN5FQMOCcIIghyzTZJNbBNjmgY4H1mNBB7tgAVQgLjA9wQtRIAEEnlQ4AAxfRnKDWUTEOBrFyaSyCHzoOQQPSaODmQJojxBUZoMD4EjlbLIMC2PiwTaJCxWGznCndawuOAyUzQQxBcLsXj5Ipiy7oNAxAByFFGDjMHJS50c-I2TCoiiIIF6YrkMlufyIDTgBJgeSgCAAtEMRiqkpzUr4GOERKIIDAwCg2GU2A0mpNWmsiIsXLaQPoLchtvBY5tqmxxh5iqIYkYAOqsES6prpQS8RBoOCaJDKMB28qVwwy66C5z6bgiI6EyaZP7sCgBirgJS4MVEPQZLBDiqaO60MGtlh3El13CjCg1fnhn1SBg+OhgEDwHkYtCyKA1brebTZPlsCRUSaFAp2xnMuAUAoFagIbD2TxEJAQOgs2zVcZBaNBumfCgWUTKBskKTZWjAUxiQ+fMtB0XAiDLLsQEORQzx7NgfGxbp4OgAoK3EARFBiABJEQCjML84FrZQGEUTZjTQDQiBIQ8VxqUCmJjS9gnuWBlzYOh8Ig5gCGKUDxm0FiiNg0gKKQKi+A4-plLUPBuipEBNG3GgRItFZfD4O1yMo0x0CyKIgA

testdata/tattoo/source.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
During tattooing, ink is injected into the skin, initiating an immune response, and cells called "macrophages" move into the area and "eat up" the ink. The macrophages carry some of the ink to the body's lymph nodes, but some that are filled with ink stay put, embedded in the skin. That's what makes the tattoo visible under the skin. Dalhousie Uiversity's Alec Falkenham is developing a topical cream that works by targeting the macrophages that have remained at the site of the tattoo. New macrophages move in to consume the previously pigment-filled macrophages and then migrate to the lymph nodes, eventually taking all the dye with them. "When comparing it to laser-based tattoo removal, in which you see the burns, the scarring, the blisters, in this case, we've designed a drug that doesn't really have much off-target effect," he said. "We're not targeting any of the normal skin cells, so you won't see a lot of inflammation. In fact, based on the process that we're actually using, we don't think there will be any inflammation at all and it would actually be anti-inflammatory.

testdata/tattoo/uint8array.bin

664 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)