json-with-bigint 3.1.0 → 3.2.1
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 +4 -2
- package/json-with-bigint.cjs +10 -0
- package/json-with-bigint.d.cts +28 -0
- package/json-with-bigint.js +10 -0
- package/json-with-bigint.min.js +1 -1
- package/package.json +5 -1
- /package/__tests__/{performance.js → performance.cjs} +0 -0
- /package/__tests__/{unit.js → unit.cjs} +0 -0
package/README.md
CHANGED
|
@@ -24,7 +24,7 @@ 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,
|
|
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
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.stringify` call in your project with functions from this library and it'll work.
|
|
30
30
|
|
|
@@ -34,11 +34,13 @@ JSONParse(JSONStringify(data)).bigNumber === 9007199254740992n // true
|
|
|
34
34
|
|
|
35
35
|
✔️ Does not contaminate your global space (unlike monkey-patching solution)
|
|
36
36
|
|
|
37
|
+
✔️ Isomorphic (it can run in both the browser and Node.js with the same code)
|
|
38
|
+
|
|
37
39
|
✔️ Can be used in both JavaScript and TypeScript projects (.d.ts file included)
|
|
38
40
|
|
|
39
41
|
✔️ Can be used as both ESM and CommonJS module
|
|
40
42
|
|
|
41
|
-
✔️ Size:
|
|
43
|
+
✔️ Size: 660 bytes (minified and gzipped)
|
|
42
44
|
|
|
43
45
|
✔️ No dependencies
|
|
44
46
|
|
package/json-with-bigint.cjs
CHANGED
|
@@ -5,6 +5,16 @@ const noiseValue = /^-?\d+n+$/; // Noise - strings that match the custom format
|
|
|
5
5
|
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
6
|
*/
|
|
7
7
|
const JSONStringify = (data, space) => {
|
|
8
|
+
if ("rawJSON" in JSON) {
|
|
9
|
+
return JSON.stringify(
|
|
10
|
+
data,
|
|
11
|
+
(_, value) => {
|
|
12
|
+
return typeof value === "bigint" ? JSON.rawJSON(value.toString()) : value
|
|
13
|
+
},
|
|
14
|
+
space
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
8
18
|
if (!data) return JSON.stringify(data);
|
|
9
19
|
|
|
10
20
|
const bigInts = /([\[:])?"(-?\d+)n"($|[,\}\]])/g;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
type JsonObject = {
|
|
2
|
+
[x: string]: Json;
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
type JsonArray = Json[];
|
|
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;
|
package/json-with-bigint.js
CHANGED
|
@@ -5,6 +5,16 @@ const noiseValue = /^-?\d+n+$/; // Noise - strings that match the custom format
|
|
|
5
5
|
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
6
|
*/
|
|
7
7
|
export const JSONStringify = (data, space) => {
|
|
8
|
+
if ("rawJSON" in JSON) {
|
|
9
|
+
return JSON.stringify(
|
|
10
|
+
data,
|
|
11
|
+
(_, value) => {
|
|
12
|
+
return typeof value === "bigint" ? JSON.rawJSON(value.toString()) : value
|
|
13
|
+
},
|
|
14
|
+
space
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
8
18
|
if (!data) return JSON.stringify(data);
|
|
9
19
|
|
|
10
20
|
const bigInts = /([\[:])?"(-?\d+)n"($|[,\}\]])/g;
|
package/json-with-bigint.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const noiseValue=/^-?\d+n+$/;export const JSONStringify=(n,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+"$/,
|
|
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}))};
|
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-with-bigint",
|
|
3
|
-
"version": "3.1
|
|
3
|
+
"version": "3.2.1",
|
|
4
4
|
"description": "JS library that allows you to easily serialize and deserialize data with BigInt values",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
"import": "./json-with-bigint.js",
|
|
8
8
|
"require": "./json-with-bigint.cjs"
|
|
9
9
|
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"performance": "node __tests__/performance.cjs",
|
|
12
|
+
"unit": "node __tests__/unit.cjs"
|
|
13
|
+
},
|
|
10
14
|
"repository": {
|
|
11
15
|
"type": "git",
|
|
12
16
|
"url": "https://github.com/Ivan-Korolenko/json-with-bigint"
|
|
File without changes
|
|
File without changes
|