json-with-bigint 3.2.1 → 3.2.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
@@ -40,7 +40,7 @@ JSONParse(JSONStringify(data)).bigNumber === 9007199254740992n // true
40
40
 
41
41
  ✔️ Can be used as both ESM and CommonJS module
42
42
 
43
- ✔️ Size: 660 bytes (minified and gzipped)
43
+ ✔️ Size: 684 bytes (minified and gzipped)
44
44
 
45
45
  ✔️ No dependencies
46
46
 
@@ -1,4 +1,6 @@
1
1
  const noiseValue = /^-?\d+n+$/; // Noise - strings that match the custom format before being converted to it
2
+ const originalStringify = JSON.stringify;
3
+ const originalParse = JSON.parse;
2
4
 
3
5
  /*
4
6
  Function to serialize data to a JSON string.
@@ -6,7 +8,7 @@ const noiseValue = /^-?\d+n+$/; // Noise - strings that match the custom format
6
8
  */
7
9
  const JSONStringify = (data, space) => {
8
10
  if ("rawJSON" in JSON) {
9
- return JSON.stringify(
11
+ return originalStringify(
10
12
  data,
11
13
  (_, value) => {
12
14
  return typeof value === "bigint" ? JSON.rawJSON(value.toString()) : value
@@ -15,11 +17,11 @@ const JSONStringify = (data, space) => {
15
17
  );
16
18
  }
17
19
 
18
- if (!data) return JSON.stringify(data);
20
+ if (!data) return originalStringify(data);
19
21
 
20
22
  const bigInts = /([\[:])?"(-?\d+)n"($|[,\}\]])/g;
21
23
  const noise = /([\[:])?("-?\d+n+)n("$|"[,\}\]])/g;
22
- const convertedToCustomJSON = JSON.stringify(
24
+ const convertedToCustomJSON = originalStringify(
23
25
  data,
24
26
  (_, value) => {
25
27
  const isNoise =
@@ -43,7 +45,7 @@ const JSONStringify = (data, space) => {
43
45
  Other types of values are not affected and parsed as native JSON.parse() would parse them.
44
46
  */
45
47
  const JSONParse = (json) => {
46
- if (!json) return JSON.parse(json);
48
+ if (!json) return originalParse(json);
47
49
 
48
50
  const MAX_INT = Number.MAX_SAFE_INTEGER.toString();
49
51
  const MAX_DIGITS = MAX_INT.length;
@@ -75,7 +77,7 @@ const JSONParse = (json) => {
75
77
  );
76
78
 
77
79
  // Convert marked big numbers to BigInt
78
- return JSON.parse(serializedData, (_, value) => {
80
+ return originalParse(serializedData, (_, value) => {
79
81
  const isCustomFormatBigInt =
80
82
  typeof value === "string" && Boolean(value.match(customFormat));
81
83
 
@@ -1,4 +1,6 @@
1
1
  const noiseValue = /^-?\d+n+$/; // Noise - strings that match the custom format before being converted to it
2
+ const originalStringify = JSON.stringify;
3
+ const originalParse = JSON.parse;
2
4
 
3
5
  /*
4
6
  Function to serialize data to a JSON string.
@@ -6,7 +8,7 @@ const noiseValue = /^-?\d+n+$/; // Noise - strings that match the custom format
6
8
  */
7
9
  export const JSONStringify = (data, space) => {
8
10
  if ("rawJSON" in JSON) {
9
- return JSON.stringify(
11
+ return originalStringify(
10
12
  data,
11
13
  (_, value) => {
12
14
  return typeof value === "bigint" ? JSON.rawJSON(value.toString()) : value
@@ -15,11 +17,11 @@ export const JSONStringify = (data, space) => {
15
17
  );
16
18
  }
17
19
 
18
- if (!data) return JSON.stringify(data);
20
+ if (!data) return originalStringify(data);
19
21
 
20
22
  const bigInts = /([\[:])?"(-?\d+)n"($|[,\}\]])/g;
21
23
  const noise = /([\[:])?("-?\d+n+)n("$|"[,\}\]])/g;
22
- const convertedToCustomJSON = JSON.stringify(
24
+ const convertedToCustomJSON = originalStringify(
23
25
  data,
24
26
  (_, value) => {
25
27
  const isNoise =
@@ -43,7 +45,7 @@ export const JSONStringify = (data, space) => {
43
45
  Other types of values are not affected and parsed as native JSON.parse() would parse them.
44
46
  */
45
47
  export const JSONParse = (json) => {
46
- if (!json) return JSON.parse(json);
48
+ if (!json) return originalParse(json);
47
49
 
48
50
  const MAX_INT = Number.MAX_SAFE_INTEGER.toString();
49
51
  const MAX_DIGITS = MAX_INT.length;
@@ -75,7 +77,7 @@ export const JSONParse = (json) => {
75
77
  );
76
78
 
77
79
  // Convert marked big numbers to BigInt
78
- return JSON.parse(serializedData, (_, value) => {
80
+ return originalParse(serializedData, (_, value) => {
79
81
  const isCustomFormatBigInt =
80
82
  typeof value === "string" && Boolean(value.match(customFormat));
81
83
 
@@ -1 +1 @@
1
- const noiseValue=/^-?\d+n+$/;export const JSONStringify=(n,t)=>{if("rawJSON"in JSON)return JSON.stringify(n,((n,t)=>"bigint"==typeof t?JSON.rawJSON(t.toString()):t),t);if(!n)return JSON.stringify(n);return JSON.stringify(n,((n,t)=>"string"==typeof t&&Boolean(t.match(noiseValue))||"bigint"==typeof t?t.toString()+"n":t),t).replace(/([\[:])?"(-?\d+)n"($|[,\}\]])/g,"$1$2$3").replace(/([\[:])?("-?\d+n+)n("$|"[,\}\]])/g,"$1$2$3")};export const JSONParse=n=>{if(!n)return JSON.parse(n);const t=Number.MAX_SAFE_INTEGER.toString(),e=t.length,r=/^"-?\d+n+"$/,i=/^-?\d+n$/,o=n.replace(/"(?:\\.|[^"])*"|-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?/g,((n,i,o,g)=>{const s='"'===n[0];if(s&&Boolean(n.match(r)))return n.substring(0,n.length-1)+'n"';const a=o||g,l=i&&(i.length<e||i.length===e&&i<=t);return s||a||l?n:'"'+n+'n"'}));return JSON.parse(o,((n,t)=>{if("string"==typeof t&&Boolean(t.match(i)))return BigInt(t.substring(0,t.length-1));return"string"==typeof t&&Boolean(t.match(noiseValue))?t.substring(0,t.length-1):t}))};
1
+ const noiseValue=/^-?\d+n+$/,originalStringify=JSON.stringify,originalParse=JSON.parse;export const JSONStringify=(n,r)=>{if("rawJSON"in JSON)return originalStringify(n,((n,r)=>"bigint"==typeof r?JSON.rawJSON(r.toString()):r),r);if(!n)return originalStringify(n);return originalStringify(n,((n,r)=>"string"==typeof r&&Boolean(r.match(noiseValue))||"bigint"==typeof r?r.toString()+"n":r),r).replace(/([\[:])?"(-?\d+)n"($|[,\}\]])/g,"$1$2$3").replace(/([\[:])?("-?\d+n+)n("$|"[,\}\]])/g,"$1$2$3")};export const JSONParse=n=>{if(!n)return originalParse(n);const r=Number.MAX_SAFE_INTEGER.toString(),t=r.length,i=/^"-?\d+n+"$/,e=/^-?\d+n$/,g=n.replace(/"(?:\\.|[^"])*"|-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?/g,((n,e,g,o)=>{const a='"'===n[0];if(a&&Boolean(n.match(i)))return n.substring(0,n.length-1)+'n"';const s=g||o,l=e&&(e.length<t||e.length===t&&e<=r);return a||s||l?n:'"'+n+'n"'}));return originalParse(g,((n,r)=>{if("string"==typeof r&&Boolean(r.match(e)))return BigInt(r.substring(0,r.length-1));return"string"==typeof r&&Boolean(r.match(noiseValue))?r.substring(0,r.length-1):r}))};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-with-bigint",
3
- "version": "3.2.1",
3
+ "version": "3.2.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": {