Use json-with-bigint via npm
Some checks failed
Build / build (18) (push) Failing after 44s
Build / build (22) (push) Failing after 1m9s
Build / build (20) (push) Failing after 1m4s
Build / build (18) (pull_request) Failing after 43s
Build / build (20) (pull_request) Failing after 1m6s
Build / build (22) (pull_request) Failing after 1m0s
Some checks failed
Build / build (18) (push) Failing after 44s
Build / build (22) (push) Failing after 1m9s
Build / build (20) (push) Failing after 1m4s
Build / build (18) (pull_request) Failing after 43s
Build / build (20) (pull_request) Failing after 1m6s
Build / build (22) (pull_request) Failing after 1m0s
This commit is contained in:
parent
cf070a4f16
commit
9667b6f72c
7
package-lock.json
generated
7
package-lock.json
generated
@ -14,6 +14,7 @@
|
||||
"copyfiles": "^2.4.1",
|
||||
"crc-32": "^1.2.2",
|
||||
"express": "^5",
|
||||
"json-with-bigint": "^3.2.1",
|
||||
"mongoose": "^8.11.0",
|
||||
"morgan": "^1.10.0",
|
||||
"typescript": ">=5.5 <5.6.0",
|
||||
@ -2346,6 +2347,12 @@
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/json-with-bigint": {
|
||||
"version": "3.2.1",
|
||||
"resolved": "https://registry.npmjs.org/json-with-bigint/-/json-with-bigint-3.2.1.tgz",
|
||||
"integrity": "sha512-0f8RHpU1AwBFwIPmtm71W+cFxzlXdiBmzc3JqydsNDSKSAsr0Lso6KXRbz0h2LRwTIRiHAk/UaD+xaAN5f577w==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/json5": {
|
||||
"version": "2.2.3",
|
||||
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz",
|
||||
|
@ -19,6 +19,7 @@
|
||||
"copyfiles": "^2.4.1",
|
||||
"crc-32": "^1.2.2",
|
||||
"express": "^5",
|
||||
"json-with-bigint": "^3.2.1",
|
||||
"mongoose": "^8.11.0",
|
||||
"morgan": "^1.10.0",
|
||||
"typescript": ">=5.5 <5.6.0",
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { JSONParse } from "../json-with-bigint";
|
||||
import { JSONParse } from "json-with-bigint";
|
||||
|
||||
export const getJSONfromString = <T>(str: string): T => {
|
||||
const jsonSubstring = str.substring(0, str.lastIndexOf("}") + 1);
|
||||
|
@ -1,53 +0,0 @@
|
||||
// Based on the json-with-bigint library: https://github.com/Ivan-Korolenko/json-with-bigint/blob/main/json-with-bigint.js
|
||||
// Sadly we can't use it directly: https://github.com/Ivan-Korolenko/json-with-bigint/issues/15
|
||||
|
||||
/* eslint-disable */
|
||||
const noiseValue = /^-?\d+n+$/; // Noise - strings that match the custom format before being converted to it
|
||||
export const JSONParse = (json: any) => {
|
||||
if (!json) return JSON.parse(json);
|
||||
|
||||
const MAX_INT = Number.MAX_SAFE_INTEGER.toString();
|
||||
const MAX_DIGITS = MAX_INT.length;
|
||||
const stringsOrLargeNumbers =
|
||||
/"(?:\\.|[^"])*"|-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?/g;
|
||||
const noiseValueWithQuotes = /^"-?\d+n+"$/; // Noise - strings that match the custom format before being converted to it
|
||||
const customFormat = /^-?\d+n$/;
|
||||
|
||||
// Find and mark big numbers with "n"
|
||||
const serializedData = json.replace(
|
||||
stringsOrLargeNumbers,
|
||||
(text: any, digits: any, fractional: any, exponential: any) => {
|
||||
const isString = text[0] === '"';
|
||||
const isNoise = isString && Boolean(text.match(noiseValueWithQuotes));
|
||||
|
||||
if (isNoise) return text.substring(0, text.length - 1) + 'n"'; // Mark noise values with additional "n" to offset the deletion of one "n" during the processing
|
||||
|
||||
const isFractionalOrExponential = fractional || exponential;
|
||||
const isLessThanMaxSafeInt =
|
||||
digits &&
|
||||
(digits.length < MAX_DIGITS ||
|
||||
(digits.length === MAX_DIGITS && digits <= MAX_INT)); // With a fixed number of digits, we can correctly use lexicographical comparison to do a numeric comparison
|
||||
|
||||
if (isString || isFractionalOrExponential || isLessThanMaxSafeInt)
|
||||
return text;
|
||||
|
||||
return '"' + text + 'n"';
|
||||
}
|
||||
);
|
||||
|
||||
// Convert marked big numbers to BigInt
|
||||
return JSON.parse(serializedData, (_, value) => {
|
||||
const isCustomFormatBigInt =
|
||||
typeof value === "string" && Boolean(value.match(customFormat));
|
||||
|
||||
if (isCustomFormatBigInt)
|
||||
return BigInt(value.substring(0, value.length - 1));
|
||||
|
||||
const isNoiseValue =
|
||||
typeof value === "string" && Boolean(value.match(noiseValue));
|
||||
|
||||
if (isNoiseValue) return value.substring(0, value.length - 1); // Remove one "n" off the end of the noisy string
|
||||
|
||||
return value;
|
||||
});
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user