comment-json
Advanced tools
+3
-3
| { | ||
| "name": "comment-json", | ||
| "version": "4.3.0", | ||
| "version": "4.4.0", | ||
| "description": "Parse and stringify JSON with comments. It will retain comments even after saved!", | ||
@@ -57,7 +57,7 @@ "main": "src/index.js", | ||
| "cross-env": "^10.0.0", | ||
| "eslint": "^8.8.0", | ||
| "eslint": "^8.57.1", | ||
| "eslint-plugin-import": "^2.25.4", | ||
| "nyc": "^15.1.0", | ||
| "test-fixture": "^2.4.1", | ||
| "typescript": "^4.5.5" | ||
| "typescript": "^5.9.2" | ||
| }, | ||
@@ -64,0 +64,0 @@ "dependencies": { |
+32
-0
@@ -129,2 +129,3 @@ [](https://github.com/kaelzhang/node-comment-json/actions/workflows/nodejs.yml) | ||
| - **reviver?** `Function() | null` Default to `null`. It acts the same as the second parameter of [`JSON.parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse). If a function, prescribes how the value originally produced by parsing is transformed, before being returned. | ||
| - `comment-json` also passes the 3rd parameter `context` to the function `reviver`, as described in https://github.com/tc39/proposal-json-parse-with-source, which will be useful to parse a JSON string with `BigInt` values. | ||
| - **remove_comments?** `boolean = false` If true, the comments won't be maintained, which is often used when we want to get a clean object. | ||
@@ -595,2 +596,33 @@ | ||
| ## Dealing with `BigInt`s | ||
| > Advanced Section | ||
| `comment-json` implements the TC39 proposal [proposal-json-parse-with-source](https://github.com/tc39/proposal-json-parse-with-source) | ||
| ```js | ||
| const {parse, stringify} = require('comment-json') | ||
| const parsed = parse( | ||
| `{"foo": 9007199254740993}`, | ||
| // | ||
| (key, value, {source}) => | ||
| /^[0-9]+$/.test(source) ? BigInt(source) : value | ||
| ) | ||
| console.log(parsed) | ||
| // { | ||
| // "foo": 9007199254740993n | ||
| // } | ||
| stringify(parsed, (key, val) => | ||
| typeof value === 'bigint' | ||
| // Pay attention that | ||
| // JSON.rawJSON is supported in node >= 21 | ||
| ? JSON.rawJSON(String(val)).rawJSON | ||
| : value | ||
| ) | ||
| // {"foo":9007199254740993} | ||
| ``` | ||
| ## License | ||
@@ -597,0 +629,0 @@ |
+26
-10
@@ -86,5 +86,5 @@ // JSON formatting | ||
| const transform = (k, v) => reviver | ||
| ? reviver(k, v) | ||
| : v | ||
| const transform = (k, {value, context = {}}) => reviver | ||
| ? reviver(k, value, context) | ||
| : value | ||
@@ -374,3 +374,5 @@ const unexpected = () => { | ||
| next() | ||
| return parse_object() | ||
| return { | ||
| value: parse_object() | ||
| } | ||
| } | ||
@@ -380,3 +382,5 @@ | ||
| next() | ||
| return parse_array() | ||
| return { | ||
| value: parse_array() | ||
| } | ||
| } | ||
@@ -394,2 +398,3 @@ | ||
| let v | ||
| let source | ||
@@ -403,4 +408,13 @@ switch (tt) { | ||
| next() | ||
| return JSON.parse(negative + v) | ||
| source = negative + v | ||
| return { | ||
| value: JSON.parse(source), | ||
| context: { | ||
| source | ||
| } | ||
| } | ||
| default: | ||
| // => unexpected token | ||
| return {} | ||
| } | ||
@@ -431,3 +445,3 @@ } | ||
| let result = walk() | ||
| const final = walk() | ||
@@ -440,2 +454,7 @@ parse_comments(PREFIX_AFTER_ALL) | ||
| // reviver | ||
| let result = transform('', final) | ||
| // We should run reviver before the checks below, | ||
| // otherwise the comment info will be lost | ||
| if (!no_comments && result !== null) { | ||
@@ -456,5 +475,2 @@ if (!isObject(result)) { | ||
| // reviver | ||
| result = transform('', result) | ||
| free() | ||
@@ -461,0 +477,0 @@ |
47925
2.59%1150
1.32%633
5.32%