Skip to content

Commit

Permalink
Merge pull request #18 from aminya/hasNoSlashOrEvenNumberOfSlashes-
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya authored Jul 10, 2021
2 parents 9a009c0 + 30a6a54 commit 483de4e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 17 deletions.
6 changes: 4 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Minify JSON files **blazing fast**! Supports Comments. Written in D.

60 times faster than jsonminify!
360 times faster than jsonminify!

[![CI](https://github.com/aminya/minijson/actions/workflows/CI.yml/badge.svg)](https://github.com/aminya/minijson/actions/workflows/CI.yml)

Expand Down Expand Up @@ -88,9 +88,11 @@ minifyFiles(["file1.json", "file2.json"], true);

### Benchmarks

On AMD Ryzen 7 4800H:

```
❯ node .\benchmark\native-benchmark.mjs
0.977 seconds
0.163 seconds
❯ node .\benchmark\js-benchmark.mjs
58.818 seconds
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@types/node": "16.0.0",
"eslint-config-atomic": "^1.16.1",
"jasmine": "^3.8.0",
"jasmine-spec-reporter": "^7.0.0",
"jsonminify": "^0.4.1",
"mjs-dirname": "^1.0.0",
"parcel": "^2.0.0-beta.3.1",
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions src/native/lib.d
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,24 @@ string minifyString(in string jsonString, in bool hasComment = false) @trusted
if (noCommentOrNotInComment)
{
auto leftContextSubstr = match.pre()[prevFrom .. $];
if (leftContextSubstr.length != 0)
const noLeftContext = leftContextSubstr.length == 0;
if (!in_string && !noLeftContext)
{
if (!in_string)
{
leftContextSubstr = leftContextSubstr.replaceAll(spaceOrBreakRegex, "");
}
leftContextSubstr = leftContextSubstr.replaceAll(spaceOrBreakRegex, "");
}
if (!noLeftContext) {
result ~= leftContextSubstr;
}

if (matchFrontHit == "\"")
if (matchFrontHit == "\"")
{
if (!in_string || noLeftContext || hasNoSlashOrEvenNumberOfSlashes(leftContextSubstr))
{
if (!in_string || hasNoSlashOrEvenNumberOfSlashes(leftContextSubstr))
{
// start of string with ", or unescaped " character found to end string
in_string = !in_string;
}
--from; // include " character in next catch
rightContext = jsonString[from .. $];
// start of string with ", or unescaped " character found to end string
in_string = !in_string;
}
--from; // include " character in next catch
rightContext = jsonString[from .. $];
}
}
// comments
Expand Down
2 changes: 1 addition & 1 deletion test/helper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function minifyFixtures(jsonFiles, hasComment) {
minifiedObject = JSON.parse(minifiedString)
} catch (e) {
console.error(`The minified file is not valid for: ${minifiedFile}`)
throw e
return { minifiedString, minifiedObject: {} }
}
return { minifiedString, minifiedObject }
})
Expand Down
5 changes: 4 additions & 1 deletion test/index-test.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "./reporter.mjs"
import { minifyFixtures } from "./helper.mjs"
import { minifyFiles, minifyString } from "../dist/lib.js"
import { standardFiles, withCommentFiles } from "./fixtures.mjs"
Expand All @@ -14,7 +15,9 @@ describe("minijson", () => {
const fixtureNum = pathInfo.length
for (let iFixture = 0; iFixture !== fixtureNum; ++iFixture) {
it(pathInfo[iFixture].originalFile, () => {
expect(resultInfo[iFixture].minifiedObject).toEqual(originalInfo[iFixture].originalObject)
const originalObject = originalInfo[iFixture].originalObject
const minifiedObject = resultInfo[iFixture].minifiedObject
expect(minifiedObject).toEqual(originalObject)
})
}

Expand Down
12 changes: 12 additions & 0 deletions test/reporter.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { SpecReporter } from "jasmine-spec-reporter"

jasmine.getEnv().clearReporters() // remove default reporter logs
jasmine.getEnv().addReporter(
new SpecReporter({
spec: {
displaySuccessful: true,
displayPending: true,
displayFailed: true,
},
})
)

0 comments on commit 483de4e

Please sign in to comment.