json-with-bigint 3.5.1 → 3.5.3

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.
@@ -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
- async function main() {
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();
@@ -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
- assert.deepStrictEqual(JSONParse(test1JSON), test1Obj);
110
- console.log("1 test passed");
111
- assert.deepStrictEqual(JSONStringify(JSONParse(test1JSON)), test1JSON);
112
- console.log("1 test round-trip passed");
113
-
114
- assert.deepStrictEqual(JSONParse(test2JSON), test2Obj);
115
- console.log("2 test passed");
116
- assert.deepStrictEqual(JSONStringify(JSONParse(test2JSON)), test2TersedJSON);
117
- console.log("2 test round-trip passed");
118
-
119
- assert.deepStrictEqual(JSONParse(test3JSON), test3Obj);
120
- console.log("3 test passed");
121
- assert.deepStrictEqual(JSONStringify(JSONParse(test3JSON)), test3JSON);
122
- console.log("3 test round-trip passed");
123
-
124
- assert.deepStrictEqual(JSONParse(test4JSON), test4Obj);
125
- console.log("4 test passed");
126
- assert.deepStrictEqual(JSONStringify(JSONParse(test4JSON)), test4JSON);
127
- console.log("4 test round-trip passed");
128
-
129
- assert.deepStrictEqual(JSONParse(test5JSON), test5Obj);
130
- console.log("5 test passed");
131
- assert.deepStrictEqual(JSONStringify(JSONParse(test5JSON)), test5JSON);
132
- console.log("5 test round-trip passed");
133
-
134
- assert.deepStrictEqual(JSONParse(test6JSON), test6Obj);
135
- console.log("6 test passed");
136
- assert.deepStrictEqual(JSONStringify(JSONParse(test6JSON)), test6JSON);
137
- console.log("6 test round-trip passed");
138
-
139
- assert.deepStrictEqual(JSONParse(test7JSON), test7Obj);
140
- console.log("7 test passed");
141
- assert.deepStrictEqual(JSONStringify(JSONParse(test7JSON)), test7JSON);
142
- console.log("7 test round-trip passed");
143
-
144
- assert.deepStrictEqual(JSONStringify(test8Obj, null, 2), test8JSON);
145
- console.log("8 test passed");
146
- assert.deepStrictEqual(JSONParse(JSONStringify(test8Obj, null, 2)), test8Obj);
147
- console.log("8 test round-trip passed");
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/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "json-with-bigint",
3
- "version": "3.5.1",
3
+ "version": "3.5.3",
4
4
  "description": "JS library that allows you to easily serialize and deserialize data with BigInt values",
5
5
  "type": "module",
6
+ "types": "./json-with-bigint.d.ts",
6
7
  "exports": {
7
8
  "import": "./json-with-bigint.js",
8
9
  "require": "./json-with-bigint.cjs"