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 +1 -1
- package/json-with-bigint.cjs +7 -5
- package/json-with-bigint.js +7 -5
- package/json-with-bigint.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
package/json-with-bigint.cjs
CHANGED
|
@@ -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
|
|
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
|
|
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 =
|
|
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
|
|
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
|
|
80
|
+
return originalParse(serializedData, (_, value) => {
|
|
79
81
|
const isCustomFormatBigInt =
|
|
80
82
|
typeof value === "string" && Boolean(value.match(customFormat));
|
|
81
83
|
|
package/json-with-bigint.js
CHANGED
|
@@ -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
|
|
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
|
|
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 =
|
|
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
|
|
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
|
|
80
|
+
return originalParse(serializedData, (_, value) => {
|
|
79
81
|
const isCustomFormatBigInt =
|
|
80
82
|
typeof value === "string" && Boolean(value.match(customFormat));
|
|
81
83
|
|
package/json-with-bigint.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const noiseValue=/^-?\d+n
|
|
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}))};
|