json-with-bigint 3.2.1 → 3.3.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: 756 bytes (minified and gzipped)
44
44
 
45
45
  ✔️ No dependencies
46
46
 
@@ -1,33 +1,47 @@
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
- /*
4
- Function to serialize data to a JSON string.
5
+ /*
6
+ Function to serialize value to a JSON string.
5
7
  Converts BigInt values to a custom format (strings with digits and "n" at the end) and then converts them to proper big integers in a JSON string.
6
8
  */
7
- const JSONStringify = (data, space) => {
9
+ const JSONStringify = (value, replacer, space) => {
8
10
  if ("rawJSON" in JSON) {
9
- return JSON.stringify(
10
- data,
11
- (_, value) => {
12
- return typeof value === "bigint" ? JSON.rawJSON(value.toString()) : value
11
+ return originalStringify(
12
+ value,
13
+ (key, value) => {
14
+ if (typeof value === "bigint") return JSON.rawJSON(value.toString());
15
+
16
+ if (typeof replacer === "function") return replacer(key, value);
17
+
18
+ if (Array.isArray(replacer) && replacer.includes(key)) return value;
19
+
20
+ return value;
13
21
  },
14
22
  space
15
23
  );
16
24
  }
17
25
 
18
- if (!data) return JSON.stringify(data);
26
+ if (!value) return originalStringify(value, replacer, space);
19
27
 
20
28
  const bigInts = /([\[:])?"(-?\d+)n"($|[,\}\]])/g;
21
29
  const noise = /([\[:])?("-?\d+n+)n("$|"[,\}\]])/g;
22
- const convertedToCustomJSON = JSON.stringify(
23
- data,
24
- (_, value) => {
30
+ const convertedToCustomJSON = originalStringify(
31
+ value,
32
+ (key, value) => {
25
33
  const isNoise =
26
34
  typeof value === "string" && Boolean(value.match(noiseValue));
27
35
 
28
36
  if (isNoise) return value.toString() + "n"; // Mark noise values with additional "n" to offset the deletion of one "n" during the processing
29
37
 
30
- return typeof value === "bigint" ? value.toString() + "n" : value;
38
+ if (typeof value === "bigint") return value.toString() + "n";
39
+
40
+ if (typeof replacer === "function") return replacer(key, value);
41
+
42
+ if (Array.isArray(replacer) && replacer.includes(key)) return value;
43
+
44
+ return value;
31
45
  },
32
46
  space
33
47
  );
@@ -37,13 +51,13 @@ const JSONStringify = (data, space) => {
37
51
  return denoisedJSON;
38
52
  };
39
53
 
40
- /*
54
+ /*
41
55
  Function to parse JSON.
42
56
  If JSON has number values greater than Number.MAX_SAFE_INTEGER, we convert those values to a custom format, then parse them to BigInt values.
43
57
  Other types of values are not affected and parsed as native JSON.parse() would parse them.
44
58
  */
45
- const JSONParse = (json) => {
46
- if (!json) return JSON.parse(json);
59
+ const JSONParse = (text, reviver) => {
60
+ if (!text) return originalParse(text, reviver);
47
61
 
48
62
  const MAX_INT = Number.MAX_SAFE_INTEGER.toString();
49
63
  const MAX_DIGITS = MAX_INT.length;
@@ -53,7 +67,7 @@ const JSONParse = (json) => {
53
67
  const customFormat = /^-?\d+n$/;
54
68
 
55
69
  // Find and mark big numbers with "n"
56
- const serializedData = json.replace(
70
+ const serializedData = text.replace(
57
71
  stringsOrLargeNumbers,
58
72
  (text, digits, fractional, exponential) => {
59
73
  const isString = text[0] === '"';
@@ -75,7 +89,7 @@ const JSONParse = (json) => {
75
89
  );
76
90
 
77
91
  // Convert marked big numbers to BigInt
78
- return JSON.parse(serializedData, (_, value) => {
92
+ return originalParse(serializedData, (key, value, context) => {
79
93
  const isCustomFormatBigInt =
80
94
  typeof value === "string" && Boolean(value.match(customFormat));
81
95
 
@@ -87,7 +101,9 @@ const JSONParse = (json) => {
87
101
 
88
102
  if (isNoiseValue) return value.substring(0, value.length - 1); // Remove one "n" off the end of the noisy string
89
103
 
90
- return value;
104
+ if (typeof reviver !== "function") return value;
105
+
106
+ return reviver(key, value, context);
91
107
  });
92
108
  };
93
109
 
@@ -1,33 +1,47 @@
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
- /*
4
- Function to serialize data to a JSON string.
5
+ /*
6
+ Function to serialize value to a JSON string.
5
7
  Converts BigInt values to a custom format (strings with digits and "n" at the end) and then converts them to proper big integers in a JSON string.
6
8
  */
7
- export const JSONStringify = (data, space) => {
9
+ export const JSONStringify = (value, replacer, space) => {
8
10
  if ("rawJSON" in JSON) {
9
- return JSON.stringify(
10
- data,
11
- (_, value) => {
12
- return typeof value === "bigint" ? JSON.rawJSON(value.toString()) : value
11
+ return originalStringify(
12
+ value,
13
+ (key, value) => {
14
+ if (typeof value === "bigint") return JSON.rawJSON(value.toString())
15
+
16
+ if (typeof replacer === "function") return replacer(key, value)
17
+
18
+ if (Array.isArray(replacer) && replacer.includes(key)) return value
19
+
20
+ return value
13
21
  },
14
22
  space
15
23
  );
16
24
  }
17
25
 
18
- if (!data) return JSON.stringify(data);
26
+ if (!value) return originalStringify(value, replacer, space);
19
27
 
20
28
  const bigInts = /([\[:])?"(-?\d+)n"($|[,\}\]])/g;
21
29
  const noise = /([\[:])?("-?\d+n+)n("$|"[,\}\]])/g;
22
- const convertedToCustomJSON = JSON.stringify(
23
- data,
24
- (_, value) => {
30
+ const convertedToCustomJSON = originalStringify(
31
+ value,
32
+ (key, value) => {
25
33
  const isNoise =
26
34
  typeof value === "string" && Boolean(value.match(noiseValue));
27
35
 
28
36
  if (isNoise) return value.toString() + "n"; // Mark noise values with additional "n" to offset the deletion of one "n" during the processing
29
37
 
30
- return typeof value === "bigint" ? value.toString() + "n" : value;
38
+ if (typeof value === "bigint") return value.toString() + "n";
39
+
40
+ if (typeof replacer === "function") return replacer(key, value);
41
+
42
+ if (Array.isArray(replacer) && replacer.includes(key)) return value;
43
+
44
+ return value;
31
45
  },
32
46
  space
33
47
  );
@@ -37,13 +51,13 @@ export const JSONStringify = (data, space) => {
37
51
  return denoisedJSON;
38
52
  };
39
53
 
40
- /*
54
+ /*
41
55
  Function to parse JSON.
42
56
  If JSON has number values greater than Number.MAX_SAFE_INTEGER, we convert those values to a custom format, then parse them to BigInt values.
43
57
  Other types of values are not affected and parsed as native JSON.parse() would parse them.
44
58
  */
45
- export const JSONParse = (json) => {
46
- if (!json) return JSON.parse(json);
59
+ export const JSONParse = (text, reviver) => {
60
+ if (!text) return originalParse(text, reviver);
47
61
 
48
62
  const MAX_INT = Number.MAX_SAFE_INTEGER.toString();
49
63
  const MAX_DIGITS = MAX_INT.length;
@@ -53,7 +67,7 @@ export const JSONParse = (json) => {
53
67
  const customFormat = /^-?\d+n$/;
54
68
 
55
69
  // Find and mark big numbers with "n"
56
- const serializedData = json.replace(
70
+ const serializedData = text.replace(
57
71
  stringsOrLargeNumbers,
58
72
  (text, digits, fractional, exponential) => {
59
73
  const isString = text[0] === '"';
@@ -75,7 +89,7 @@ export const JSONParse = (json) => {
75
89
  );
76
90
 
77
91
  // Convert marked big numbers to BigInt
78
- return JSON.parse(serializedData, (_, value) => {
92
+ return originalParse(serializedData, (key, value, context) => {
79
93
  const isCustomFormatBigInt =
80
94
  typeof value === "string" && Boolean(value.match(customFormat));
81
95
 
@@ -87,6 +101,8 @@ export const JSONParse = (json) => {
87
101
 
88
102
  if (isNoiseValue) return value.substring(0, value.length - 1); // Remove one "n" off the end of the noisy string
89
103
 
90
- return value;
104
+ if (typeof reviver !== "function") return value;
105
+
106
+ return reviver(key, value, context);
91
107
  });
92
108
  };
@@ -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,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 i=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 i.replace(/([\[:])?"(-?\d+)n"($|[,\}\]])/g,"$1$2$3").replace(/([\[:])?("-?\d+n+)n("$|"[,\}\]])/g,"$1$2$3")};export const JSONParse=(n,r)=>{if(!n)return originalParse(n,r);const t=Number.MAX_SAFE_INTEGER.toString(),i=t.length,e=/^"-?\d+n+"$/,o=/^-?\d+n$/,g=n.replace(/"(?:\\.|[^"])*"|-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?/g,((n,r,o,g)=>{const a='"'===n[0];if(a&&Boolean(n.match(e)))return n.substring(0,n.length-1)+'n"';const s=o||g,l=r&&(r.length<i||r.length===i&&r<=t);return a||s||l?n:'"'+n+'n"'}));return originalParse(g,((n,t,i)=>{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,i)}))};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-with-bigint",
3
- "version": "3.2.1",
3
+ "version": "3.3.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": {