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