json-with-bigint 3.5.0 → 3.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/__tests__/helpers.cjs +10 -0
- package/__tests__/performance.cjs +14 -4
- package/__tests__/unit.cjs +50 -40
- package/json-with-bigint.cjs +2 -12
- package/json-with-bigint.js +2 -12
- package/json-with-bigint.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const originalParse = JSON.parse;
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
Function to test the V1 (implementation without the JSON.parse's context.source feature support)
|
|
5
|
+
*/
|
|
6
|
+
const imitateJSONParseWithoutContext = (text, reviver) => {
|
|
7
|
+
return originalParse(text, (key, value) => reviver(key, value));
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
module.exports = { originalParse, imitateJSONParseWithoutContext };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// ------ Performance tests ------
|
|
2
|
-
// To test both JSONParseV2() and JSONParse(), switch Node.js version (latest vs something old, like 12.3.0)
|
|
3
2
|
|
|
4
3
|
const { performance } = require("perf_hooks");
|
|
4
|
+
const { imitateJSONParseWithoutContext } = require("./helpers.cjs");
|
|
5
5
|
const { JSONParse, JSONStringify } = require("../json-with-bigint.cjs");
|
|
6
6
|
|
|
7
7
|
const fs = require("fs").promises;
|
|
@@ -124,9 +124,7 @@ const measureExecTime = (fn) => {
|
|
|
124
124
|
console.log("Time: ", endTime - startTime);
|
|
125
125
|
};
|
|
126
126
|
|
|
127
|
-
|
|
128
|
-
const data = await readPerformanceJSON(JSON_LOCAL_FILEPATH, "utf8");
|
|
129
|
-
|
|
127
|
+
const runTests = (data) => {
|
|
130
128
|
measureExecTime(() => {
|
|
131
129
|
console.log("___________");
|
|
132
130
|
console.log("Performance test. One-way");
|
|
@@ -138,6 +136,18 @@ async function main() {
|
|
|
138
136
|
console.log("Performance test. Round-trip");
|
|
139
137
|
JSONStringify(JSONParse(data));
|
|
140
138
|
});
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
async function main() {
|
|
142
|
+
const data = await readPerformanceJSON(JSON_LOCAL_FILEPATH, "utf8");
|
|
143
|
+
|
|
144
|
+
console.log("------ V2 performance tests ------");
|
|
145
|
+
runTests(data);
|
|
146
|
+
|
|
147
|
+
JSON.parse = imitateJSONParseWithoutContext;
|
|
148
|
+
|
|
149
|
+
console.log("\n------ V1 (without context.source) performance tests ------");
|
|
150
|
+
runTests(data);
|
|
141
151
|
}
|
|
142
152
|
|
|
143
153
|
main();
|
package/__tests__/unit.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// ------ Unit tests ------
|
|
2
|
-
// To test both JSONParseV2() and JSONParse(), switch Node.js version (latest vs something old, like 12.3.0)
|
|
3
2
|
|
|
4
3
|
const assert = require("assert");
|
|
4
|
+
const { imitateJSONParseWithoutContext } = require("./helpers.cjs");
|
|
5
5
|
const { JSONStringify, JSONParse } = require("../json-with-bigint.cjs");
|
|
6
6
|
|
|
7
7
|
const test1JSON = `{"zero":9007199254740998,"one":-42,"two":-9007199254740998,"test":["He was\\":[-23432432432434324324324324]",111,9007199254740998,{"test2":-9007199254740998}],"test3":["He was:[-23432432432434324324324324]",111,9007199254740998,{"test2":-9007199254740998,"float":1.9007199254740998,"float2":0.1,"float3":2.9007199254740996,"int":1,"int2":3243243432432434324324324}],"float4":[1.9007199254740998,1111111111111111111111111111111111,0.1,1,54354654654654654654656546546546546]}`;
|
|
@@ -106,42 +106,52 @@ const test7Obj = [
|
|
|
106
106
|
const test8Obj = { uid: BigInt("1308537228663099396") };
|
|
107
107
|
const test8JSON = '{\n "uid": 1308537228663099396\n}';
|
|
108
108
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
109
|
+
const runTests = () => {
|
|
110
|
+
assert.deepStrictEqual(JSONParse(test1JSON), test1Obj);
|
|
111
|
+
console.log("1 test passed");
|
|
112
|
+
assert.deepStrictEqual(JSONStringify(JSONParse(test1JSON)), test1JSON);
|
|
113
|
+
console.log("1 test round-trip passed");
|
|
114
|
+
|
|
115
|
+
assert.deepStrictEqual(JSONParse(test2JSON), test2Obj);
|
|
116
|
+
console.log("2 test passed");
|
|
117
|
+
assert.deepStrictEqual(JSONStringify(JSONParse(test2JSON)), test2TersedJSON);
|
|
118
|
+
console.log("2 test round-trip passed");
|
|
119
|
+
|
|
120
|
+
assert.deepStrictEqual(JSONParse(test3JSON), test3Obj);
|
|
121
|
+
console.log("3 test passed");
|
|
122
|
+
assert.deepStrictEqual(JSONStringify(JSONParse(test3JSON)), test3JSON);
|
|
123
|
+
console.log("3 test round-trip passed");
|
|
124
|
+
|
|
125
|
+
assert.deepStrictEqual(JSONParse(test4JSON), test4Obj);
|
|
126
|
+
console.log("4 test passed");
|
|
127
|
+
assert.deepStrictEqual(JSONStringify(JSONParse(test4JSON)), test4JSON);
|
|
128
|
+
console.log("4 test round-trip passed");
|
|
129
|
+
|
|
130
|
+
assert.deepStrictEqual(JSONParse(test5JSON), test5Obj);
|
|
131
|
+
console.log("5 test passed");
|
|
132
|
+
assert.deepStrictEqual(JSONStringify(JSONParse(test5JSON)), test5JSON);
|
|
133
|
+
console.log("5 test round-trip passed");
|
|
134
|
+
|
|
135
|
+
assert.deepStrictEqual(JSONParse(test6JSON), test6Obj);
|
|
136
|
+
console.log("6 test passed");
|
|
137
|
+
assert.deepStrictEqual(JSONStringify(JSONParse(test6JSON)), test6JSON);
|
|
138
|
+
console.log("6 test round-trip passed");
|
|
139
|
+
|
|
140
|
+
assert.deepStrictEqual(JSONParse(test7JSON), test7Obj);
|
|
141
|
+
console.log("7 test passed");
|
|
142
|
+
assert.deepStrictEqual(JSONStringify(JSONParse(test7JSON)), test7JSON);
|
|
143
|
+
console.log("7 test round-trip passed");
|
|
144
|
+
|
|
145
|
+
assert.deepStrictEqual(JSONStringify(test8Obj, null, 2), test8JSON);
|
|
146
|
+
console.log("8 test passed");
|
|
147
|
+
assert.deepStrictEqual(JSONParse(JSONStringify(test8Obj, null, 2)), test8Obj);
|
|
148
|
+
console.log("8 test round-trip passed");
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
console.log("------ V2 unit tests ------");
|
|
152
|
+
runTests();
|
|
153
|
+
|
|
154
|
+
JSON.parse = imitateJSONParseWithoutContext;
|
|
155
|
+
|
|
156
|
+
console.log("\n------ V1 (without context.source) unit tests ------");
|
|
157
|
+
runTests();
|
package/json-with-bigint.cjs
CHANGED
|
@@ -54,18 +54,8 @@ const JSONStringify = (value, replacer, space) => {
|
|
|
54
54
|
/*
|
|
55
55
|
Function to check if the JSON.parse's context.source feature is supported.
|
|
56
56
|
*/
|
|
57
|
-
const isContextSourceSupported = () =>
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
JSON.parse('{"test": 1}', (key, value, context) => {
|
|
61
|
-
if (key === "test" && context && context.source === "1") {
|
|
62
|
-
supported = true;
|
|
63
|
-
}
|
|
64
|
-
return value;
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
return supported;
|
|
68
|
-
};
|
|
57
|
+
const isContextSourceSupported = () =>
|
|
58
|
+
JSON.parse("1", (_, __, context) => !!context && context.source === "1");
|
|
69
59
|
|
|
70
60
|
/*
|
|
71
61
|
Faster (2x) and simpler function to parse JSON.
|
package/json-with-bigint.js
CHANGED
|
@@ -54,18 +54,8 @@ export const JSONStringify = (value, replacer, space) => {
|
|
|
54
54
|
/*
|
|
55
55
|
Function to check if the JSON.parse's context.source feature is supported.
|
|
56
56
|
*/
|
|
57
|
-
const isContextSourceSupported = () =>
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
JSON.parse('{"test": 1}', (key, value, context) => {
|
|
61
|
-
if (key === "test" && context && context.source === "1") {
|
|
62
|
-
supported = true;
|
|
63
|
-
}
|
|
64
|
-
return value;
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
return supported;
|
|
68
|
-
};
|
|
57
|
+
const isContextSourceSupported = () =>
|
|
58
|
+
JSON.parse("1", (_, __, context) => !!context && context.source === "1");
|
|
69
59
|
|
|
70
60
|
/*
|
|
71
61
|
Faster (2x) and simpler function to parse JSON.
|
package/json-with-bigint.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const noiseValue=/^-?\d+n+$/,originalStringify=JSON.stringify,originalParse=JSON.parse;export const JSONStringify=(n,t
|
|
1
|
+
const noiseValue=/^-?\d+n+$/,originalStringify=JSON.stringify,originalParse=JSON.parse;export const JSONStringify=(n,r,t)=>{if("rawJSON"in JSON)return originalStringify(n,((n,t)=>"bigint"==typeof t?JSON.rawJSON(t.toString()):"function"==typeof r?r(n,t):(Array.isArray(r)&&r.includes(n),t)),t);if(!n)return originalStringify(n,r,t);const e=originalStringify(n,((n,t)=>"string"==typeof t&&Boolean(t.match(noiseValue))||"bigint"==typeof t?t.toString()+"n":"function"==typeof r?r(n,t):(Array.isArray(r)&&r.includes(n),t)),t);return e.replace(/([\[:])?"(-?\d+)n"($|([\\n]|\s)*(\s|[\\n])*[,\}\]])/g,"$1$2$3").replace(/([\[:])?("-?\d+n+)n("$|"([\\n]|\s)*(\s|[\\n])*[,\}\]])/g,"$1$2$3")};const isContextSourceSupported=()=>JSON.parse("1",((n,r,t)=>!!t&&"1"===t.source)),JSONParseV2=(n,r)=>{const t=/^-?\d+$/;return JSON.parse(n,((n,e,i)=>{const o="number"==typeof e&&(e>Number.MAX_SAFE_INTEGER||e<Number.MIN_SAFE_INTEGER),s=t.test(i.source);return o&&s?BigInt(i.source):"function"!=typeof r?e:r(n,e,i)}))};export const JSONParse=(n,r)=>{if(!n)return originalParse(n,r);if(JSON.parse("1",((n,r,t)=>!!t&&"1"===t.source)))return JSONParseV2(n,r);const t=Number.MAX_SAFE_INTEGER.toString(),e=t.length,i=/^"-?\d+n+"$/,o=/^-?\d+n$/,s=n.replace(/"(?:\\.|[^"])*"|-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?/g,((n,r,o,s)=>{const a='"'===n[0];if(a&&Boolean(n.match(i)))return n.substring(0,n.length-1)+'n"';const g=o||s,u=r&&(r.length<e||r.length===e&&r<=t);return a||g||u?n:'"'+n+'n"'}));return originalParse(s,((n,t,e)=>{if("string"==typeof t&&Boolean(t.match(o)))return BigInt(t.substring(0,t.length-1));return"string"==typeof t&&Boolean(t.match(noiseValue))?t.substring(0,t.length-1):"function"!=typeof r?t:r(n,t,e)}))};
|