diff --git a/.gitignore b/.gitignore index 14fa8b2..681ac3c 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,4 @@ dist .dub trace.* -test/fixtures/*-minified.json +test/fixtures/**/*-minified.json diff --git a/Readme.md b/Readme.md index 8bada63..30fe105 100644 --- a/Readme.md +++ b/Readme.md @@ -1,6 +1,6 @@ # minijson -Minify JSON files **blazing fast**! Written in D. +Minify JSON files **blazing fast**! Supports Comments. Written in D, 55 times faster than jsonminify! @@ -30,14 +30,22 @@ dub build --config=executable --build=release-nobounds --compiler=ldc2 ### CLI Usage -``` -Usage: minify json +```shell +❯ minijson --help + +minijson: minify json files with support for comments minijson --file file1.json --file file2.json + minijson --file file1_with_comment.json --file file2_with_comment.json --comment + minijson --string '{"some_json": "string_here"}' + minijson --string '{"some_json": "string_here"} //comment' --comment + + More information at https://github.com/aminya/minijson - --file an array of files to minify - --string a json string to minify --h --help This help information. + --file an array of files to minify + --string a json string to minify + --comment a flag to support comments in json +-h --help This help information. ``` ### Node API @@ -48,8 +56,14 @@ import { minifyFiles, minifyString } from "minijson" // minify the files in-place and in parallel await minifyFiles(["file1.json", "file2.json"]) +// supports comments by passing true as the second argument +await minifyFiles(["file1_with_comment.json", "file2_with_comment.json"], true) + // minify the given string const minifiedString = minifyString(`{"some_json": "here"}`) + +// supports comments by passing true as the second argument +const minifiedString2 = minifyString(`{"some_json": "here"}//comment`, true) ``` **Note**: in the Nodejs API, prefer `minifyFiles` over other functions, as it minifies the files in parallel with the least amount of resources. @@ -62,8 +76,14 @@ import minijson: minifyString, minifyFiles; // minify the given string const minifiedString = minifyString(`{"some_json": "here"}`); +// supports comments by passing true as the second argument +const minifiedString2 = minifyString(`{"some_json": "here"}//comment`, true); + // minify the files in-place and in parallel minifyFiles(["file1.json", "file2.json"]); + +// supports comments by passing true as the second argument +minifyFiles(["file1.json", "file2.json"], true); ``` ### Benchmarks diff --git a/benchmark/js-benchmark.mjs b/benchmark/js-benchmark.mjs index 827f81d..01ac9f9 100644 --- a/benchmark/js-benchmark.mjs +++ b/benchmark/js-benchmark.mjs @@ -2,7 +2,7 @@ import { readFile, writeFile } from "fs/promises" import { performance } from "perf_hooks" import jsonMinify from "jsonminify" -import { jsonFiles } from "../test/fixtures.mjs" +import { standardFiles } from "../test/fixtures.mjs" // warmup const tmp = await jsonMinify("{}") @@ -10,7 +10,7 @@ const tmp = await jsonMinify("{}") const t1 = performance.now() await Promise.all( - jsonFiles.map(async (jsonFile) => { + standardFiles.map(async (jsonFile) => { const jsonString = await readFile(jsonFile, "utf8") const data = await jsonMinify(jsonString) return await writeFile(jsonFile, data) diff --git a/benchmark/native-benchmark.mjs b/benchmark/native-benchmark.mjs index f5c7b10..a1b81e9 100644 --- a/benchmark/native-benchmark.mjs +++ b/benchmark/native-benchmark.mjs @@ -1,11 +1,11 @@ import { performance } from "perf_hooks" import { minifyFiles } from "../dist/lib.js" -import { jsonFiles } from "../test/fixtures.mjs" +import { standardFiles } from "../test/fixtures.mjs" const t1 = performance.now() -await minifyFiles(jsonFiles) +await minifyFiles(standardFiles) const t2 = performance.now() console.log(((t2 - t1) / 1000).toFixed(3), "seconds") diff --git a/package.json b/package.json index aeb1f09..3ba5230 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@aminya/minijson", "author": "Amin Yahyaabdi", "version": "0.4.1", - "description": "Minify JSON files blazing fast! Written in D", + "description": "Minify JSON files blazing fast! Supports Comments. Written in D.", "homepage": "https://github.com/aminya/minijson", "license": "MIT", "files": [ @@ -17,7 +17,7 @@ "test.format": "prettier . --check", "lint": "eslint . --fix", "test.lint": "eslint .", - "test": "jasmine ./test/index-test.mjs", + "test": "jasmine ./test/index-test.mjs || shx rm -rf ./test/fixtures/**/*-minified.json", "clean": "shx rm -rf dist", "build.native": "dub build --config=executable", "build.native.release": "pnpm build.native -- --build release-nobounds --compiler=ldc2", @@ -42,7 +42,8 @@ "parcel": "^2.0.0-beta.3.1", "prettier-config-atomic": "^2.0.5", "servor": "^4.0.2", - "shx": "0.3.3" + "shx": "0.3.3", + "strip-json-comments": "^3.1.1" }, "targets": { "browser": { @@ -64,6 +65,17 @@ "mini", "min", "minifier", + "jsonc", + "comment", + "comments", + "remove", + "delete", + "multiline", + "strip", + "trim", + "parse", + "jsonc", + "json5", "js", "build", "build-tool", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 478f6ea..95ecc1d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,7 @@ specifiers: prettier-config-atomic: ^2.0.5 servor: ^4.0.2 shx: 0.3.3 + strip-json-comments: ^3.1.1 devDependencies: '@types/jasmine': 3.7.7 @@ -23,6 +24,7 @@ devDependencies: prettier-config-atomic: 2.0.5 servor: 4.0.2 shx: 0.3.3 + strip-json-comments: 3.1.1 packages: diff --git a/src/native/cli.d b/src/native/cli.d index e01a800..296acd4 100644 --- a/src/native/cli.d +++ b/src/native/cli.d @@ -7,9 +7,14 @@ import std.getopt : getopt, defaultGetoptPrinter, GetoptResult; /** Print help */ void printHelp(GetoptResult optResult) @trusted { - return defaultGetoptPrinter(`Usage: minify json + return defaultGetoptPrinter(`minijson: minify json files with support for comments minijson --file file1.json --file file2.json + minijson --file file1_with_comment.json --file file2_with_comment.json --comment + minijson --string '{"some_json": "string_here"}' + minijson --string '{"some_json": "string_here"} //comment' --comment + + More information at https://github.com/aminya/minijson `, optResult.options); } @@ -17,9 +22,10 @@ void main(string[] args) @trusted { string[] files; string jsonString; + bool hasComment = false; auto optResult = getopt(args, "file", "an array of files to minify", &files, "string", - "a json string to minify", &jsonString); + "a json string to minify", &jsonString, "comment", "a flag to support comments in json", &hasComment); if (optResult.helpWanted || (!files && !jsonString)) { @@ -29,7 +35,7 @@ void main(string[] args) @trusted // minify the given files if (files) { - minifyFiles(files); + minifyFiles(files, hasComment); } // minify the given string and print to stdout @@ -37,6 +43,6 @@ void main(string[] args) @trusted { import std : write; - write(minifyString(jsonString)); + write(minifyString(jsonString, hasComment)); } } diff --git a/src/native/lib.d b/src/native/lib.d index ad8700f..601ae73 100644 --- a/src/native/lib.d +++ b/src/native/lib.d @@ -15,12 +15,12 @@ const repeatingBackSlashRegex = ctRegex!(`(\\)*$`); Params: jsonString = the json string you want to minify - hasComments = a switch that indicates if the json string has comments. Pass `true` to support parsing comments. Default: `false`. + hasComment = a boolean to support comments in json. Default: `false`. Return: the minified json string */ -string minifyString(string jsonString, bool hasComments = false) @trusted +string minifyString(string jsonString, bool hasComment = false) @trusted { auto in_string = false; auto in_multiline_comment = false; @@ -29,7 +29,7 @@ string minifyString(string jsonString, bool hasComments = false) @trusted size_t from = 0; auto rightContext = ""; - const tokenizer = !hasComments ? tokenizerNoComment : tokenizerWithComment; + const tokenizer = !hasComment ? tokenizerNoComment : tokenizerWithComment; auto match = jsonString.matchAll(tokenizer); @@ -43,7 +43,8 @@ string minifyString(string jsonString, bool hasComments = false) @trusted const prevFrom = from; from = jsonString.length - rightContext.length; // lastIndex - const noCommentOrNotInComment = !hasComments || (!in_multiline_comment && !in_singleline_comment); + const notInComment = (!in_multiline_comment && !in_singleline_comment); + const noCommentOrNotInComment = !hasComment || notInComment; if (noCommentOrNotInComment) { @@ -65,15 +66,11 @@ string minifyString(string jsonString, bool hasComments = false) @trusted --from; // include " character in next catch rightContext = jsonString[from .. $]; } - else if (matchFrontHit.matchFirst(spaceOrBreakRegex).empty()) - { - new_str ~= matchFrontHit; - } } // comments - if (hasComments && !in_string) + if (hasComment && !in_string) { - if (!in_multiline_comment && !in_singleline_comment) + if (notInComment) { if (matchFrontHit == "/*") { @@ -83,6 +80,10 @@ string minifyString(string jsonString, bool hasComments = false) @trusted { in_singleline_comment = true; } + else if (notSlashAndNoSpaceOrBreak(matchFrontHit)) + { + new_str ~= matchFrontHit; + } } else if (in_multiline_comment && !in_singleline_comment && matchFrontHit == "*/") { @@ -93,27 +94,36 @@ string minifyString(string jsonString, bool hasComments = false) @trusted in_singleline_comment = false; } } - + if (!hasComment && notSlashAndNoSpaceOrBreak(matchFrontHit)) + { + new_str ~= matchFrontHit; + } match.popFront(); } new_str ~= rightContext; return new_str.array().join(""); } -bool hasNoSlashOrEvenNumberOfSlashes(string leftContext) @safe +private bool hasNoSlashOrEvenNumberOfSlashes(string leftContext) @safe { auto leftContextMatch = leftContext.matchFirst(repeatingBackSlashRegex); // if not matched the hit length will be 0 (== leftContextMatch.empty()) return leftContextMatch.hit().length % 2 == 0; } +private bool notSlashAndNoSpaceOrBreak(string matchFrontHit) +{ + return matchFrontHit != "\"" && matchFrontHit.matchFirst(spaceOrBreakRegex).empty(); +} + /** Minify the given files in place. It minifies the files in parallel. Params: files = the paths to the files. + hasComment = a boolean to support comments in json. Default: `false`. */ -void minifyFiles(string[] files) +void minifyFiles(string[] files, bool hasComment = false) { import std.parallelism : parallel; import std.file : readText, write; @@ -121,7 +131,7 @@ void minifyFiles(string[] files) foreach (ref file; files.parallel()) { const string jsonString = readText(file); - const minifiedJsonString = minifyString(jsonString); + const minifiedJsonString = minifyString(jsonString, hasComment); write(file, minifiedJsonString); } } diff --git a/src/native/libc.d b/src/native/libc.d index e0229bd..4165f06 100644 --- a/src/native/libc.d +++ b/src/native/libc.d @@ -9,17 +9,17 @@ extern (C): Params: jsonString = the json string you want to minify - hasComments = a switch that indicates if the json string has comments. Pass `true` to support parsing comments. Default: `false`. + hasComment = a boolean to support comments in json. Default: `false`. Return: the minified json string */ -auto minifyString(char* jsonCString, bool hasComments = false) +auto minifyString(char* jsonCString, bool hasComment = false) { import std : fromStringz, toStringz; const string jsonString = fromStringz(jsonCString).idup; - const minifiedString = minifyString(jsonString, hasComments); + const minifiedString = minifyString(jsonString, hasComment); return toStringz(minifiedString); } diff --git a/src/node/lib.ts b/src/node/lib.ts index 3e5849a..c252b24 100644 --- a/src/node/lib.ts +++ b/src/node/lib.ts @@ -5,10 +5,11 @@ import { join } from "path" * Minify all the given JSON files in place. It minifies the files in parallel. * * @param files An array of paths to the files + * @param hasComment A boolean to support comments in json. Default `false`. * @returns {Promise} Returns a void promise that resolves when all the files are minified * @throws {Promise} The promise is rejected with the reason for failure */ -export async function minifyFiles(files: string[]): Promise { +export async function minifyFiles(files: string[], hasComment = false): Promise { const filesNum = files.length if (filesNum === 0) { return Promise.resolve() @@ -21,6 +22,10 @@ export async function minifyFiles(files: string[]): Promise { args.splice(iSplice, 0, "--file") } + if (hasComment) { + args.push("--comment") + } + await spawnMinijson(args) } @@ -28,12 +33,17 @@ export async function minifyFiles(files: string[]): Promise { * Minify the given JSON string * * @param jsonString The json string you want to minify + * @param hasComment A boolean to support comments in json. Default `false`. * @returns {Promise} The minified json string * @throws {Promise} The promise is rejected with the reason for failure */ -export async function minifyString(jsonString: string): Promise { +export async function minifyString(jsonString: string, hasComment = false): Promise { + const args = ["--string", jsonString] + if (hasComment) { + args.push("--comment") + } // trim is needed due to using stdout for interop - return (await spawnMinijson(["--string", jsonString])).trim() + return (await spawnMinijson(args)).trim() } const exeExtention = process.platform === "win32" ? ".exe" : "" diff --git a/test/fixtures.mjs b/test/fixtures.mjs index c17af68..cc32510 100644 --- a/test/fixtures.mjs +++ b/test/fixtures.mjs @@ -4,5 +4,8 @@ import { join } from "path" import { dirname as mjsDirname } from "mjs-dirname" const __dirname = mjsDirname(import.meta.url) -const directory = join(__dirname, "fixtures") -export const jsonFiles = readdirSync(directory).map((file) => join(directory, file)) +const standardDirectory = join(__dirname, "fixtures", "standard") +export const standardFiles = readdirSync(standardDirectory).map((file) => join(standardDirectory, file)) + +const withCommentDirectory = join(__dirname, "fixtures", "with-comment") +export const withCommentFiles = readdirSync(withCommentDirectory).map((file) => join(withCommentDirectory, file)) diff --git a/test/fixtures/0.10.0.json b/test/fixtures/standard/0.10.0.json similarity index 100% rename from test/fixtures/0.10.0.json rename to test/fixtures/standard/0.10.0.json diff --git a/test/fixtures/0.11.0.json b/test/fixtures/standard/0.11.0.json similarity index 100% rename from test/fixtures/0.11.0.json rename to test/fixtures/standard/0.11.0.json diff --git a/test/fixtures/0.12.0.json b/test/fixtures/standard/0.12.0.json similarity index 100% rename from test/fixtures/0.12.0.json rename to test/fixtures/standard/0.12.0.json diff --git a/test/fixtures/0.13.0.json b/test/fixtures/standard/0.13.0.json similarity index 100% rename from test/fixtures/0.13.0.json rename to test/fixtures/standard/0.13.0.json diff --git a/test/fixtures/0.14.0.json b/test/fixtures/standard/0.14.0.json similarity index 100% rename from test/fixtures/0.14.0.json rename to test/fixtures/standard/0.14.0.json diff --git a/test/fixtures/0.15.0.json b/test/fixtures/standard/0.15.0.json similarity index 100% rename from test/fixtures/0.15.0.json rename to test/fixtures/standard/0.15.0.json diff --git a/test/fixtures/0.16.0.json b/test/fixtures/standard/0.16.0.json similarity index 100% rename from test/fixtures/0.16.0.json rename to test/fixtures/standard/0.16.0.json diff --git a/test/fixtures/0.17.0.json b/test/fixtures/standard/0.17.0.json similarity index 100% rename from test/fixtures/0.17.0.json rename to test/fixtures/standard/0.17.0.json diff --git a/test/fixtures/0.18.0.json b/test/fixtures/standard/0.18.0.json similarity index 100% rename from test/fixtures/0.18.0.json rename to test/fixtures/standard/0.18.0.json diff --git a/test/fixtures/0.19.0.json b/test/fixtures/standard/0.19.0.json similarity index 100% rename from test/fixtures/0.19.0.json rename to test/fixtures/standard/0.19.0.json diff --git a/test/fixtures/0.20.0.json b/test/fixtures/standard/0.20.0.json similarity index 100% rename from test/fixtures/0.20.0.json rename to test/fixtures/standard/0.20.0.json diff --git a/test/fixtures/0.21.0.json b/test/fixtures/standard/0.21.0.json similarity index 100% rename from test/fixtures/0.21.0.json rename to test/fixtures/standard/0.21.0.json diff --git a/test/fixtures/0.22.0.json b/test/fixtures/standard/0.22.0.json similarity index 100% rename from test/fixtures/0.22.0.json rename to test/fixtures/standard/0.22.0.json diff --git a/test/fixtures/0.23.0.json b/test/fixtures/standard/0.23.0.json similarity index 100% rename from test/fixtures/0.23.0.json rename to test/fixtures/standard/0.23.0.json diff --git a/test/fixtures/0.24.0.json b/test/fixtures/standard/0.24.0.json similarity index 100% rename from test/fixtures/0.24.0.json rename to test/fixtures/standard/0.24.0.json diff --git a/test/fixtures/0.25.0.json b/test/fixtures/standard/0.25.0.json similarity index 100% rename from test/fixtures/0.25.0.json rename to test/fixtures/standard/0.25.0.json diff --git a/test/fixtures/0.26.0.json b/test/fixtures/standard/0.26.0.json similarity index 100% rename from test/fixtures/0.26.0.json rename to test/fixtures/standard/0.26.0.json diff --git a/test/fixtures/0.4.2.json b/test/fixtures/standard/0.4.2.json similarity index 100% rename from test/fixtures/0.4.2.json rename to test/fixtures/standard/0.4.2.json diff --git a/test/fixtures/0.5.0.json b/test/fixtures/standard/0.5.0.json similarity index 100% rename from test/fixtures/0.5.0.json rename to test/fixtures/standard/0.5.0.json diff --git a/test/fixtures/0.5.1.json b/test/fixtures/standard/0.5.1.json similarity index 100% rename from test/fixtures/0.5.1.json rename to test/fixtures/standard/0.5.1.json diff --git a/test/fixtures/0.6.0.json b/test/fixtures/standard/0.6.0.json similarity index 100% rename from test/fixtures/0.6.0.json rename to test/fixtures/standard/0.6.0.json diff --git a/test/fixtures/0.7.0.json b/test/fixtures/standard/0.7.0.json similarity index 100% rename from test/fixtures/0.7.0.json rename to test/fixtures/standard/0.7.0.json diff --git a/test/fixtures/0.7.2.json b/test/fixtures/standard/0.7.2.json similarity index 100% rename from test/fixtures/0.7.2.json rename to test/fixtures/standard/0.7.2.json diff --git a/test/fixtures/0.8.0.json b/test/fixtures/standard/0.8.0.json similarity index 100% rename from test/fixtures/0.8.0.json rename to test/fixtures/standard/0.8.0.json diff --git a/test/fixtures/0.9.0.json b/test/fixtures/standard/0.9.0.json similarity index 100% rename from test/fixtures/0.9.0.json rename to test/fixtures/standard/0.9.0.json diff --git a/test/fixtures/1.0.0.json b/test/fixtures/standard/1.0.0.json similarity index 100% rename from test/fixtures/1.0.0.json rename to test/fixtures/standard/1.0.0.json diff --git a/test/fixtures/1.json b/test/fixtures/standard/1.json similarity index 100% rename from test/fixtures/1.json rename to test/fixtures/standard/1.json diff --git a/test/fixtures/asyncresource.json b/test/fixtures/standard/asyncresource.json similarity index 100% rename from test/fixtures/asyncresource.json rename to test/fixtures/standard/asyncresource.json diff --git a/test/fixtures/counter.json b/test/fixtures/standard/counter.json similarity index 100% rename from test/fixtures/counter.json rename to test/fixtures/standard/counter.json diff --git a/test/fixtures/counterstore.json b/test/fixtures/standard/counterstore.json similarity index 100% rename from test/fixtures/counterstore.json rename to test/fixtures/standard/counterstore.json diff --git a/test/fixtures/cssanimations.json b/test/fixtures/standard/cssanimations.json similarity index 100% rename from test/fixtures/cssanimations.json rename to test/fixtures/standard/cssanimations.json diff --git a/test/fixtures/escaped_quote.json b/test/fixtures/standard/escaped_quote.json similarity index 100% rename from test/fixtures/escaped_quote.json rename to test/fixtures/standard/escaped_quote.json diff --git a/test/fixtures/forms.json b/test/fixtures/standard/forms.json similarity index 100% rename from test/fixtures/forms.json rename to test/fixtures/standard/forms.json diff --git a/test/fixtures/routing.json b/test/fixtures/standard/routing.json similarity index 100% rename from test/fixtures/routing.json rename to test/fixtures/standard/routing.json diff --git a/test/fixtures/scoreboard.json b/test/fixtures/standard/scoreboard.json similarity index 100% rename from test/fixtures/scoreboard.json rename to test/fixtures/standard/scoreboard.json diff --git a/test/fixtures/simpletodos.json b/test/fixtures/standard/simpletodos.json similarity index 100% rename from test/fixtures/simpletodos.json rename to test/fixtures/standard/simpletodos.json diff --git a/test/fixtures/simpletodoshyperscript.json b/test/fixtures/standard/simpletodoshyperscript.json similarity index 100% rename from test/fixtures/simpletodoshyperscript.json rename to test/fixtures/standard/simpletodoshyperscript.json diff --git a/test/fixtures/styledjsx.json b/test/fixtures/standard/styledjsx.json similarity index 100% rename from test/fixtures/styledjsx.json rename to test/fixtures/standard/styledjsx.json diff --git a/test/fixtures/suspensetabs.json b/test/fixtures/standard/suspensetabs.json similarity index 100% rename from test/fixtures/suspensetabs.json rename to test/fixtures/standard/suspensetabs.json diff --git a/test/fixtures/todos.json b/test/fixtures/standard/todos.json similarity index 100% rename from test/fixtures/todos.json rename to test/fixtures/standard/todos.json diff --git a/test/fixtures/with-comment/array_comment.json b/test/fixtures/with-comment/array_comment.json new file mode 100644 index 0000000..8ec6294 --- /dev/null +++ b/test/fixtures/with-comment/array_comment.json @@ -0,0 +1,56 @@ +/** + * spam + */ +// spam +[ + // foo + { + // hoge + "fo\\\\o": "ba\"r", // this is cool + "ba\\r": [ + "baz", + "b\"\"u\"m", + /** aaaa **/ "zam" // this is cool + ], + /* the rest of this document is just fluff + in case you are interested. */ + "somethi\"\"ng": [10, 11, 12, /*aaa */ 23], // a + /** the rest of this document is just fluff + *in case you are interested. */ + "else": 20 + }, // hoge + { + // hoge + "fo\\\\o": "ba\"r", // this is cool + "ba\\r": [ + "baz", + "b\"\"u\"m", + /** aaaa **/ "zam" // this is cool + ], + /* the rest of this document is just fluff + in case you are interested. */ + "somethi\"\"ng": [10, 11, 12, /*aaa */ 23], // a + /** the rest of this document is just fluff + *in case you are interested. */ + "else": 20 + }, // hoge + { + // hoge + "fo\\\\o": "ba\"r", // this is cool + "ba\\r": [ + "baz", + "b\"\"u\"m", + /** aaaa **/ "zam" // this is cool + ], + /* the rest of this document is just fluff + in case you are interested. */ + "somethi\"\"ng": [10, 11, 12, /*aaa */ 23], // a + /** the rest of this document is just fluff + *in case you are interested. */ + "else": 20 + } // hoge +] // foo +/** + * spam + */ +// spam diff --git a/test/fixtures/with-comment/comment.json b/test/fixtures/with-comment/comment.json new file mode 100644 index 0000000..552e100 --- /dev/null +++ b/test/fixtures/with-comment/comment.json @@ -0,0 +1,15 @@ +{ + // hoge + "foo": "bar", // this is cool + "bar": [ + "baz", + "bum", + /** aaaa **/ "zam" // this is cool + ], + /* the rest of this document is just fluff + in case you are interested. */ + "something": 10, + /** the rest of this document is just fluff + *in case you are interested. */ + "else": 20 +} // hoge diff --git a/test/helper.mjs b/test/helper.mjs index 573dbee..0dc2ed4 100644 --- a/test/helper.mjs +++ b/test/helper.mjs @@ -1,8 +1,9 @@ import { readFile, copyFile } from "fs/promises" import { minifyFiles } from "../dist/lib.js" import { performance } from "perf_hooks" +import stripJsonComments from "strip-json-comments" -export async function minifyFixtures(jsonFiles) { +export async function minifyFixtures(jsonFiles, hasComment) { const pathInfo = jsonFiles.map((jsonFile) => { const fixtureName = jsonFile.slice(0, jsonFile.length - 5) const originalFile = `${fixtureName}.json` @@ -12,7 +13,10 @@ export async function minifyFixtures(jsonFiles) { const originalInfo = await Promise.all( pathInfo.map(async ({ originalFile, minifiedFile }) => { await copyFile(originalFile, minifiedFile) - const originalString = await readFile(originalFile, "utf8") + let originalString = await readFile(originalFile, "utf8") + if (hasComment) { + originalString = stripJsonComments(originalString) + } const originalObject = JSON.parse(originalString) return { originalString, originalObject } }) @@ -22,10 +26,10 @@ export async function minifyFixtures(jsonFiles) { const t1 = performance.now() - await minifyFiles(minifiedFiles) + await minifyFiles(minifiedFiles, hasComment) const t2 = performance.now() - console.log("Minifying took:", Math.round(t2 - t1), "ms") + console.log("Minifying took:", ((t2 - t1) / 1000).toFixed(3), "seconds") const resultInfo = await Promise.all( minifiedFiles.map(async (minifiedFile) => { diff --git a/test/index-test.mjs b/test/index-test.mjs index a1fcfe0..1cde872 100644 --- a/test/index-test.mjs +++ b/test/index-test.mjs @@ -1,13 +1,31 @@ import { minifyFixtures } from "./helper.mjs" import { minifyFiles, minifyString } from "../dist/lib.js" -import { jsonFiles } from "./fixtures.mjs" +import { standardFiles, withCommentFiles } from "./fixtures.mjs" import { rm } from "fs/promises" // minify -const { pathInfo, originalInfo, resultInfo } = await minifyFixtures(jsonFiles) +const standard = await minifyFixtures(standardFiles, false) +const withComment = await minifyFixtures(withCommentFiles, true) describe("minijson", () => { - describe("minifyFiles", () => { + describe("minifyFiles standard", () => { + const { pathInfo, originalInfo, resultInfo } = standard + // expects + const fixtureNum = pathInfo.length + for (let iFixture = 0; iFixture !== fixtureNum; ++iFixture) { + it(pathInfo[iFixture].originalFile, () => { + expect(resultInfo[iFixture].minifiedObject).toEqual(originalInfo[iFixture].originalObject) + }) + } + + afterAll(async () => { + await Promise.all(pathInfo.map((file) => rm(file.minifiedFile))) + }) + }) + + describe("minifyFiles with comment", () => { + const { pathInfo, originalInfo, resultInfo } = withComment + // expects const fixtureNum = pathInfo.length for (let iFixture = 0; iFixture !== fixtureNum; ++iFixture) {