diff --git a/package.json b/package.json index b951f558..4ad30da5 100644 --- a/package.json +++ b/package.json @@ -140,6 +140,7 @@ "date-fns": "^4.0.0", "date-fns-tz": "^3.2.0", "date-holidays": "^3.23.21", + "decimal.js": "^10.6.0", "decomposerize": "^1.4.4", "detect-csv": "^1.1.0", "diceware-generator": "^3.0.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ed476e93..8ecebeca 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -309,6 +309,9 @@ importers: date-holidays: specifier: ^3.23.21 version: 3.23.21 + decimal.js: + specifier: ^10.6.0 + version: 10.6.0 decomposerize: specifier: ^1.4.4 version: 1.4.4 @@ -6043,8 +6046,8 @@ packages: resolution: {integrity: sha512-z2S+W9X73hAUUki+N+9Za2lBlun89zigOyGrsax+KUQ6wKW4ZoWpEYBkGhQjwAjjDCkWxhY0VKEhk8wzY7F5cA==} engines: {node: '>=0.10.0'} - decimal.js@10.4.3: - resolution: {integrity: sha512-VBBaLc1MgL5XpzgIP7ny5Z6Nx3UrRkIViUkPUdtl9aya5amy3De1gsUUSB1g3+3sExYNjCAsAznmukyxCb1GRA==} + decimal.js@10.6.0: + resolution: {integrity: sha512-YpgQiITW3JXGntzdUmyUR1V812Hn8T1YVXhCu+wO3OpS4eU9l4YdD3qjyiKdV6mvV29zapkMeD390UVEf2lkUg==} decomposerize@1.4.4: resolution: {integrity: sha512-XEG0Jq2Cyhuqg8sc/LQIsIBElMUEQ+rqk88kC3qa0PkPUpfxWLcaHbQTji8otrUQDHiH/fhSG40yaIAxi7fQOQ==} @@ -17363,7 +17366,7 @@ snapshots: decamelize@1.2.0: {} - decimal.js@10.4.3: {} + decimal.js@10.6.0: {} decomposerize@1.4.4: dependencies: @@ -19376,7 +19379,7 @@ snapshots: abab: 2.0.6 cssstyle: 3.0.0 data-urls: 4.0.0 - decimal.js: 10.4.3 + decimal.js: 10.6.0 domexception: 4.0.0 form-data: 4.0.0 html-encoding-sniffer: 3.0.0 @@ -19897,7 +19900,7 @@ snapshots: dependencies: '@babel/runtime': 7.27.1 complex.js: 2.4.2 - decimal.js: 10.4.3 + decimal.js: 10.6.0 escape-latex: 1.2.0 fraction.js: 5.2.1 javascript-natural-sort: 0.7.1 @@ -19909,7 +19912,7 @@ snapshots: dependencies: '@babel/runtime': 7.27.1 complex.js: 2.4.2 - decimal.js: 10.4.3 + decimal.js: 10.6.0 escape-latex: 1.2.0 fraction.js: 5.2.1 javascript-natural-sort: 0.7.1 @@ -23473,7 +23476,7 @@ snapshots: chokidar: 3.6.0 crypto-js: 4.2.0 dayjs: 1.11.13 - decimal.js: 10.4.3 + decimal.js: 10.6.0 diff: 5.2.0 eventemitter3: 5.0.1 fuse.js: 6.6.2 diff --git a/src/main.ts b/src/main.ts index 72b82778..dde703ff 100644 --- a/src/main.ts +++ b/src/main.ts @@ -12,6 +12,7 @@ import shadow from 'vue-shadow-dom'; import { hideSplashScreen } from 'vite-plugin-splash-screen/runtime'; import { plausible } from './plugins/plausible.plugin'; import '@/utils/json5-bigint'; +import '@/utils/json5-bignum'; import 'virtual:uno.css'; @@ -31,11 +32,6 @@ window.addEventListener('vite:preloadError', (event: Event) => { installAbortSignalPolyfill(); -// eslint-disable-next-line no-extend-native -BigInt.prototype.toJSON = function () { - return JSON.rawJSON ? JSON.rawJSON(this.toString()) : this.toString(); -}; - library.add(fas); registerSW(); diff --git a/src/shims.d.ts b/src/shims.d.ts index decb0592..8c9a621d 100644 --- a/src/shims.d.ts +++ b/src/shims.d.ts @@ -44,7 +44,8 @@ interface BigInt { } interface JSON { - parseBigInt: (jsonStr: string, options?: { minDigits?: number; fallbackToString?: boolean; }) => any; + parseBigInt: (jsonStr: string, options?: { minDigits?: number; }) => any; + parseBigNum: (jsonStr: string) => any; rawJSON(value: string): any; } diff --git a/src/utils/json5-bignum.test.ts b/src/utils/json5-bignum.test.ts new file mode 100644 index 00000000..e1e5d240 --- /dev/null +++ b/src/utils/json5-bignum.test.ts @@ -0,0 +1,80 @@ +import { describe, expect, it } from 'vitest'; +import '@/utils/json5-bignum'; + +const bigJson = `{ +"code":200, +"success":true, +"data":{ +"vacantNum":121, +"arrearsNum":95, +"rentingNum":239, +"houseNum":95, +"communityDongFloorHouseVoList":{ +"records":[{ +"id":1911328104527409155, +"personId":null, +"communityId":1911239335774236673, +"communityName":"临江产业邻里中心", +"dongNumber":"3栋1单元", +"houseName":"818", +"houseAlias":"818", +"tenants":null, +"withPeople":null, +"manager":null, +"maintenance":null, +"managerList":null, +"maintenanceList":null, +"deviceId":"1929809743871979521", +"productKey":"68240ae56b00c735478abb1c", +"deviceModel":"G1N9", +"checkIn":false, +"houseStatus":1, +"createTime":null, +"lastTime":"2026-08-17", +"startTime":null, +"endTime":null, +"houseCode":"1911239335774236673,1911328104154116098,1911328104527409155", +"houseCodeName":"临江产业邻里中心3栋1单元818", +"online":1, +"devSignal":"0", +"electricityStatus":"1", +"electricity":"100%", +"monthlyElectricity":233.5, +"monthlyWater":9, +"occupancy":null, +"houseType":1, +"houseMoney":-166.16, +"waterCount":null, +"electricCount":null, +"isCollection":null, +"deviceSn":"62ba4bf0bb5e", +"vacantNum":0, +"rentingNum":0, +"occupancyNum":2, +"vacantDays":"0" +}], +"total":95, +"size":10, +"current":3, +"orders":[], +"optimizeCountSql":true, +"searchCount":true, +"countId":null, +"maxLimit":null, +"pages":10 +} +}, +"msg":"操作成功", +"num_value":12345678901234567890.123456789, +"num_small":42, +"num_other":1.77777777777777777777777777, +"num_arr":[1.234567890123456789,"hello"] +}`; + +describe('json5-bignum utils', () => { + describe('JSON.parseBigNum', () => { + it('should parse big numbers in JSON (but not in strings)', () => { + expect(JSON.stringify(JSON.parseBigNum(bigJson))).toBe(bigJson.replace(/[\r\n]/g, '')); + }); + }); +}); diff --git a/src/utils/json5-bignum.ts b/src/utils/json5-bignum.ts new file mode 100644 index 00000000..6f1b0aa3 --- /dev/null +++ b/src/utils/json5-bignum.ts @@ -0,0 +1,51 @@ +import JSON5 from 'json5'; +import Decimal from 'decimal.js'; + +// strangely, Decimal.js serialize as strings by default +Decimal.prototype.toJSON = function () { + return JSON.rawJSON ? JSON.rawJSON(this.toString()) : this.toString(); +}; + +function quoteBigNumbers(jsonStr: string): string { + const strings: string[] = []; + // Replace each string with its index + const jsonWithoutStrings = jsonStr.replace(/"(?:\\.|[^"\\])*"/g, (str) => { + const replacement = `"@${strings.length}@"`; + strings.push(str); + return replacement; + }); + const regex = /([+-]?\d*\.?\d+(?:[eE][+-]?\d+)?)(?=\s*[,}\]])/g; + // ensure to flag real numbers with ¤xxx¤ to not parse stringified numbers + const jsonQuotedBigint = jsonWithoutStrings.replace(regex, '"¤$1¤"'); + return jsonQuotedBigint.replace(/"@(\d+)@"/g, (_, ix) => { + return strings[Number(ix)]; + }); +} + +const bignumRegex = /^¤[+-]?\d*\.?\d+(?:[eE][+-]?\d+)?¤$/; +JSON.parseBigNum = function (jsonStr: string): unknown { + const safeStr = quoteBigNumbers(jsonStr); + + return JSON5.parse(safeStr, (_key, value) => { + if (typeof value === 'string') { + if (bignumRegex.test(value)) { + try { + // take only part xxx between ¤xxx¤ + const number_string = value.substring(1, value.length - 1); + const full_precision = new Decimal(number_string); + const json_number = Number(number_string); + if (json_number.toString() === full_precision.toString()) { + return json_number; + } + else { + return full_precision; + } + } + catch { + return value; + } + } + } + return value; + }); +};