From 9667b6f72c457cc20ec72aa24e4d22410e1b671a Mon Sep 17 00:00:00 2001 From: Sainan Date: Wed, 19 Mar 2025 09:11:28 +0100 Subject: [PATCH] Use json-with-bigint via npm --- package-lock.json | 7 +++++ package.json | 1 + src/helpers/stringHelpers.ts | 2 +- src/json-with-bigint.ts | 53 ------------------------------------ 4 files changed, 9 insertions(+), 54 deletions(-) delete mode 100644 src/json-with-bigint.ts diff --git a/package-lock.json b/package-lock.json index 9dacff87..0a867c58 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index c4e00181..f1ba56ab 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/helpers/stringHelpers.ts b/src/helpers/stringHelpers.ts index cd16fb4e..bba75fca 100644 --- a/src/helpers/stringHelpers.ts +++ b/src/helpers/stringHelpers.ts @@ -1,4 +1,4 @@ -import { JSONParse } from "../json-with-bigint"; +import { JSONParse } from "json-with-bigint"; export const getJSONfromString = (str: string): T => { const jsonSubstring = str.substring(0, str.lastIndexOf("}") + 1); diff --git a/src/json-with-bigint.ts b/src/json-with-bigint.ts deleted file mode 100644 index 456de87a..00000000 --- a/src/json-with-bigint.ts +++ /dev/null @@ -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; - }); -};