json-with-bigint 3.3.3 → 3.4.4
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 +2 -2
- package/__tests__/unit.cjs +15 -7
- package/json-with-bigint.cjs +1 -1
- package/json-with-bigint.d.cts +2 -27
- package/json-with-bigint.d.ts +2 -27
- package/json-with-bigint.js +1 -1
- package/json-with-bigint.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,9 +24,9 @@ JSONParse(JSONStringify(data)).bigNumber === 9007199254740992n // true
|
|
|
24
24
|
|
|
25
25
|
✔️ No need to change your JSON or the way you want to work with your data
|
|
26
26
|
|
|
27
|
-
✔️ You don't have to memorize this library's API, you already know it. Just skip the dot, and that's it (JSONParse()
|
|
27
|
+
✔️ You don't have to memorize this library's API, you already know it. Just skip the dot, and that's it (`JSONParse()`, `JSONStringify()`)
|
|
28
28
|
|
|
29
|
-
✔️ Parses and stringifies all other values other than big numbers the same way as native JSON methods in JS do. You can just replace every `JSON.parse` and `JSON.
|
|
29
|
+
✔️ Parses and stringifies all other values other than big numbers the same way as native JSON methods in JS do. Signatures match too. You can just replace every `JSON.parse()` and `JSON.strinfigy()` in your project with `JSONParse()` and `JSONStringify()`, and it will work
|
|
30
30
|
|
|
31
31
|
✔️ Correctly parses float numbers and negative numbers
|
|
32
32
|
|
package/__tests__/unit.cjs
CHANGED
|
@@ -101,37 +101,45 @@ const test7Obj = [
|
|
|
101
101
|
"90071992547409981111.5n",
|
|
102
102
|
];
|
|
103
103
|
|
|
104
|
+
const test8Obj = { uid: BigInt("1308537228663099396") };
|
|
105
|
+
const test8JSON = '{\n "uid": 1308537228663099396\n}';
|
|
106
|
+
|
|
104
107
|
assert.deepStrictEqual(JSONParse(test1JSON), test1Obj);
|
|
105
|
-
console.log("1 test
|
|
108
|
+
console.log("1 test passed");
|
|
106
109
|
assert.deepStrictEqual(JSONStringify(JSONParse(test1JSON)), test1JSON);
|
|
107
110
|
console.log("1 test round-trip passed");
|
|
108
111
|
|
|
109
112
|
assert.deepStrictEqual(JSONParse(test2JSON), test2Obj);
|
|
110
|
-
console.log("2 test
|
|
113
|
+
console.log("2 test passed");
|
|
111
114
|
assert.deepStrictEqual(JSONStringify(JSONParse(test2JSON)), test2TersedJSON);
|
|
112
115
|
console.log("2 test round-trip passed");
|
|
113
116
|
|
|
114
117
|
assert.deepStrictEqual(JSONParse(test3JSON), test3Obj);
|
|
115
|
-
console.log("3 test
|
|
118
|
+
console.log("3 test passed");
|
|
116
119
|
assert.deepStrictEqual(JSONStringify(JSONParse(test3JSON)), test3JSON);
|
|
117
120
|
console.log("3 test round-trip passed");
|
|
118
121
|
|
|
119
122
|
assert.deepStrictEqual(JSONParse(test4JSON), test4Obj);
|
|
120
|
-
console.log("4 test
|
|
123
|
+
console.log("4 test passed");
|
|
121
124
|
assert.deepStrictEqual(JSONStringify(JSONParse(test4JSON)), test4JSON);
|
|
122
125
|
console.log("4 test round-trip passed");
|
|
123
126
|
|
|
124
127
|
assert.deepStrictEqual(JSONParse(test5JSON), test5Obj);
|
|
125
|
-
console.log("5 test
|
|
128
|
+
console.log("5 test passed");
|
|
126
129
|
assert.deepStrictEqual(JSONStringify(JSONParse(test5JSON)), test5JSON);
|
|
127
130
|
console.log("5 test round-trip passed");
|
|
128
131
|
|
|
129
132
|
assert.deepStrictEqual(JSONParse(test6JSON), test6Obj);
|
|
130
|
-
console.log("6 test
|
|
133
|
+
console.log("6 test passed");
|
|
131
134
|
assert.deepStrictEqual(JSONStringify(JSONParse(test6JSON)), test6JSON);
|
|
132
135
|
console.log("6 test round-trip passed");
|
|
133
136
|
|
|
134
137
|
assert.deepStrictEqual(JSONParse(test7JSON), test7Obj);
|
|
135
|
-
console.log("7 test
|
|
138
|
+
console.log("7 test passed");
|
|
136
139
|
assert.deepStrictEqual(JSONStringify(JSONParse(test7JSON)), test7JSON);
|
|
137
140
|
console.log("7 test round-trip passed");
|
|
141
|
+
|
|
142
|
+
assert.deepStrictEqual(JSONStringify(test8Obj, null, 2), test8JSON);
|
|
143
|
+
console.log("8 test passed");
|
|
144
|
+
assert.deepStrictEqual(JSONParse(JSONStringify(test8Obj, null, 2)), test8Obj);
|
|
145
|
+
console.log("8 test round-trip passed");
|
package/json-with-bigint.cjs
CHANGED
|
@@ -26,7 +26,7 @@ const JSONStringify = (value, replacer, space) => {
|
|
|
26
26
|
if (!value) return originalStringify(value, replacer, space);
|
|
27
27
|
|
|
28
28
|
const bigInts = /([\[:])?"(-?\d+)n"($|([\\n]|\s)*(\s|[\\n])*[,\}\]])/g;
|
|
29
|
-
const noise = /([\[:])?("-?\d+n+)n("$|"[,\}\]])/g;
|
|
29
|
+
const noise = /([\[:])?("-?\d+n+)n("$|"([\\n]|\s)*(\s|[\\n])*[,\}\]])/g;
|
|
30
30
|
const convertedToCustomJSON = originalStringify(
|
|
31
31
|
value,
|
|
32
32
|
(key, value) => {
|
package/json-with-bigint.d.cts
CHANGED
|
@@ -1,28 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
[x: string]: Json;
|
|
3
|
-
};
|
|
1
|
+
export const JSONStringify: typeof JSON.stringify;
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export type Json =
|
|
8
|
-
| null
|
|
9
|
-
| undefined
|
|
10
|
-
| string
|
|
11
|
-
| number
|
|
12
|
-
| bigint
|
|
13
|
-
| boolean
|
|
14
|
-
| JsonObject
|
|
15
|
-
| {}
|
|
16
|
-
| JsonArray;
|
|
17
|
-
|
|
18
|
-
export function JSONStringify(
|
|
19
|
-
data: Exclude<Json, undefined>,
|
|
20
|
-
space?: string | number
|
|
21
|
-
): string;
|
|
22
|
-
|
|
23
|
-
export function JSONStringify(
|
|
24
|
-
data: undefined,
|
|
25
|
-
space?: string | number
|
|
26
|
-
): undefined;
|
|
27
|
-
|
|
28
|
-
export function JSONParse<T extends Json = Json>(serializedData: string): T;
|
|
3
|
+
export const JSONParse: typeof JSON.parse;
|
package/json-with-bigint.d.ts
CHANGED
|
@@ -1,28 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
[x: string]: Json;
|
|
3
|
-
};
|
|
1
|
+
export const JSONStringify: typeof JSON.stringify;
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export type Json =
|
|
8
|
-
| null
|
|
9
|
-
| undefined
|
|
10
|
-
| string
|
|
11
|
-
| number
|
|
12
|
-
| bigint
|
|
13
|
-
| boolean
|
|
14
|
-
| JsonObject
|
|
15
|
-
| {}
|
|
16
|
-
| JsonArray;
|
|
17
|
-
|
|
18
|
-
export function JSONStringify(
|
|
19
|
-
data: Exclude<Json, undefined>,
|
|
20
|
-
space?: string | number
|
|
21
|
-
): string;
|
|
22
|
-
|
|
23
|
-
export function JSONStringify(
|
|
24
|
-
data: undefined,
|
|
25
|
-
space?: string | number
|
|
26
|
-
): undefined;
|
|
27
|
-
|
|
28
|
-
export function JSONParse<T extends Json = Json>(serializedData: string): T;
|
|
3
|
+
export const JSONParse: typeof JSON.parse;
|
package/json-with-bigint.js
CHANGED
|
@@ -26,7 +26,7 @@ export const JSONStringify = (value, replacer, space) => {
|
|
|
26
26
|
if (!value) return originalStringify(value, replacer, space);
|
|
27
27
|
|
|
28
28
|
const bigInts = /([\[:])?"(-?\d+)n"($|([\\n]|\s)*(\s|[\\n])*[,\}\]])/g;
|
|
29
|
-
const noise = /([\[:])?("-?\d+n+)n("$|"[,\}\]])/g;
|
|
29
|
+
const noise = /([\[:])?("-?\d+n+)n("$|"([\\n]|\s)*(\s|[\\n])*[,\}\]])/g;
|
|
30
30
|
const convertedToCustomJSON = originalStringify(
|
|
31
31
|
value,
|
|
32
32
|
(key, value) => {
|
package/json-with-bigint.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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
|
|
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("$|"([\\n]|\s)*(\s|[\\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 s='"'===n[0];if(s&&Boolean(n.match(e)))return n.substring(0,n.length-1)+'n"';const a=o||g,l=r&&(r.length<i||r.length===i&&r<=t);return s||a||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)}))};
|