json-with-bigint 2.2.3 → 2.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.
@@ -1,3 +1,9 @@
1
+ type JsonObject = {
2
+ [x: string]: Json;
3
+ };
4
+
5
+ type JsonArray = Json[];
6
+
1
7
  export type Json =
2
8
  | null
3
9
  | undefined
@@ -9,12 +15,14 @@ export type Json =
9
15
  | {}
10
16
  | JsonArray;
11
17
 
12
- interface JsonObject {
13
- [x: string]: Json;
14
- }
15
-
16
- interface JsonArray extends Array<Json> {}
18
+ export function JSONStringify(
19
+ data: Exclude<Json, undefined>,
20
+ space?: string | number
21
+ ): string;
17
22
 
18
- export function JSONStringify(data: Json, space?: string | number): string;
23
+ export function JSONStringify(
24
+ data: undefined,
25
+ space?: string | number
26
+ ): undefined;
19
27
 
20
28
  export function JSONParse<T extends Json = Json>(serializedData: string): T;
@@ -3,6 +3,8 @@
3
3
  Converts BigInt values to custom format (strings with digits and "n" at the end) and then converts them to proper big integers in JSON string
4
4
  */
5
5
  export const JSONStringify = (data, space) => {
6
+ if (!data) return JSON.stringify(data);
7
+
6
8
  const bigInts = /([\[:])?"(-?\d+)n"([,\}\]])/g;
7
9
  const preliminaryJSON = JSON.stringify(
8
10
  data,
@@ -26,6 +28,8 @@ export const JSONStringify = (data, space) => {
26
28
  - After the match there is , OR } without " after it OR ] without " after it
27
29
  */
28
30
  export const JSONParse = (json) => {
31
+ if (!json) return JSON.parse(json);
32
+
29
33
  const numbersBiggerThanMaxInt =
30
34
  /(?<=[^\\]":[\[]?|[^\\]":\[.*[^\.\d*])(-?\d{17,}|-?(?:[9](?:[1-9]07199254740991|0[1-9]7199254740991|00[8-9]199254740991|007[2-9]99254740991|007199[3-9]54740991|0071992[6-9]4740991|00719925[5-9]740991|007199254[8-9]40991|0071992547[5-9]0991|00719925474[1-9]991|00719925474099[2-9])))(?=,|\}[^"]?|\][^"])/g;
31
35
  const serializedData = json.replace(numbersBiggerThanMaxInt, `"$1n"`);
@@ -1 +1 @@
1
- export const JSONStringify=(t,n)=>JSON.stringify(t,((t,n)=>"bigint"==typeof n?n.toString()+"n":n),n).replace(/([\[:])?"(-?\d+)n"([,\}\]])/g,"$1$2$3");export const JSONParse=t=>{const n=t.replace(/(?<=[^\\]":[\[]?|[^\\]":\[.*[^\.\d*])(-?\d{17,}|-?(?:[9](?:[1-9]07199254740991|0[1-9]7199254740991|00[8-9]199254740991|007[2-9]99254740991|007199[3-9]54740991|0071992[6-9]4740991|00719925[5-9]740991|007199254[8-9]40991|0071992547[5-9]0991|00719925474[1-9]991|00719925474099[2-9])))(?=,|\}[^"]?|\][^"])/g,'"$1n"');return JSON.parse(n,((t,n)=>"string"==typeof n&&Boolean(n.match(/^-?\d+n$/))?BigInt(n.substring(0,n.length-1)):n))};
1
+ export const JSONStringify=(t,n)=>{if(!t)return JSON.stringify(t);return JSON.stringify(t,((t,n)=>"bigint"==typeof n?n.toString()+"n":n),n).replace(/([\[:])?"(-?\d+)n"([,\}\]])/g,"$1$2$3")};export const JSONParse=t=>{if(!t)return JSON.parse(t);const n=t.replace(/(?<=[^\\]":[\[]?|[^\\]":\[.*[^\.\d*])(-?\d{17,}|-?(?:[9](?:[1-9]07199254740991|0[1-9]7199254740991|00[8-9]199254740991|007[2-9]99254740991|007199[3-9]54740991|0071992[6-9]4740991|00719925[5-9]740991|007199254[8-9]40991|0071992547[5-9]0991|00719925474[1-9]991|00719925474099[2-9])))(?=,|\}[^"]?|\][^"])/g,'"$1n"');return JSON.parse(n,((t,n)=>"string"==typeof n&&Boolean(n.match(/^-?\d+n$/))?BigInt(n.substring(0,n.length-1)):n))};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-with-bigint",
3
- "version": "2.2.3",
3
+ "version": "2.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": "./json-with-bigint.js",