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 CHANGED
@@ -44,7 +44,7 @@ JSONParse(JSONStringify(data)).bigNumber === 9007199254740992n // true
44
44
 
45
45
  ✔️ Actively supported
46
46
 
47
- ✔️ Size: 988 bytes (minified and gzipped)
47
+ ✔️ Size: 944 bytes (minified and gzipped)
48
48
 
49
49
  ✔️ No dependencies. Even the dev ones
50
50
 
@@ -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();
@@ -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
- let supported = false;
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.
@@ -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
- let supported = false;
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.
@@ -1 +1 @@
1
- const noiseValue=/^-?\d+n+$/,originalStringify=JSON.stringify,originalParse=JSON.parse;export const JSONStringify=(n,t,r)=>{if("rawJSON"in JSON)return originalStringify(n,((n,r)=>"bigint"==typeof r?JSON.rawJSON(r.toString()):"function"==typeof t?t(n,r):(Array.isArray(t)&&t.includes(n),r)),r);if(!n)return originalStringify(n,t,r);const e=originalStringify(n,((n,r)=>"string"==typeof r&&Boolean(r.match(noiseValue))||"bigint"==typeof r?r.toString()+"n":"function"==typeof t?t(n,r):(Array.isArray(t)&&t.includes(n),r)),r);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=()=>{let n=!1;return JSON.parse('{"test": 1}',((t,r,e)=>("test"===t&&e&&"1"===e.source&&(n=!0),r))),n},JSONParseV2=(n,t)=>{const r=/^-?\d+$/;return JSON.parse(n,((n,e,i)=>{const o="number"==typeof e&&(e>Number.MAX_SAFE_INTEGER||e<Number.MIN_SAFE_INTEGER),s=r.test(i.source);return o&&s?BigInt(i.source):"function"!=typeof t?e:t(n,e,i)}))};export const JSONParse=(n,t)=>{if(!n)return originalParse(n,t);if(isContextSourceSupported())return JSONParseV2(n,t);const r=Number.MAX_SAFE_INTEGER.toString(),e=r.length,i=/^"-?\d+n+"$/,o=/^-?\d+n$/,s=n.replace(/"(?:\\.|[^"])*"|-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?/g,((n,t,o,s)=>{const a='"'===n[0];if(a&&Boolean(n.match(i)))return n.substring(0,n.length-1)+'n"';const u=o||s,g=t&&(t.length<e||t.length===e&&t<=r);return a||u||g?n:'"'+n+'n"'}));return originalParse(s,((n,r,e)=>{if("string"==typeof r&&Boolean(r.match(o)))return BigInt(r.substring(0,r.length-1));return"string"==typeof r&&Boolean(r.match(noiseValue))?r.substring(0,r.length-1):"function"!=typeof t?r:t(n,r,e)}))};
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)}))};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-with-bigint",
3
- "version": "3.5.0",
3
+ "version": "3.5.2",
4
4
  "description": "JS library that allows you to easily serialize and deserialize data with BigInt values",
5
5
  "type": "module",
6
6
  "exports": {