mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-22 05:51:44 +03:00
fix: handle quoted big numbers + fix unit tests
This commit is contained in:
1 parent
be41dd6175
commit
1eb6ec6e8a
2 files changed
+81
-140
No files matched your search
+69
-131
@@ -1,139 +1,77 @@
|
|||||||
import { describe, expect, it } from 'vitest';
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import '@/utils/json5-bigint';
|
||||||
|
|
||||||
|
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":"操作成功"
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
describe('json5-bigint utils', () => {
|
describe('json5-bigint utils', () => {
|
||||||
describe('JSON.parseBigInt', () => {
|
describe('JSON.parseBigInt', () => {
|
||||||
it('should parse bigint in JSON (but not in strings)', () => {
|
it('should parse bigint in JSON (but not in strings)', () => {
|
||||||
expect(`{
|
expect(JSON.stringify(JSON.parseBigInt(bigJson))).toBe(bigJson.replace(/[\r\n]/g, ''));
|
||||||
"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.50,
|
|
||||||
"monthlyWater": 9.00,
|
|
||||||
"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": "操作成功"
|
|
||||||
}
|
|
||||||
`).toBe(`{
|
|
||||||
"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.50,
|
|
||||||
"monthlyWater": 9.00,
|
|
||||||
"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": "操作成功"
|
|
||||||
}
|
|
||||||
`);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -1,5 +1,10 @@
|
|||||||
import JSON5 from 'json5';
|
import JSON5 from 'json5';
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-extend-native
|
||||||
|
BigInt.prototype.toJSON = function () {
|
||||||
|
return JSON.rawJSON ? JSON.rawJSON(this.toString()) : this.toString();
|
||||||
|
};
|
||||||
|
|
||||||
function quoteLargeNumbers(jsonStr: string, minDigits: number): string {
|
function quoteLargeNumbers(jsonStr: string, minDigits: number): string {
|
||||||
const strings: string[] = [];
|
const strings: string[] = [];
|
||||||
// Replace each string with its index
|
// Replace each string with its index
|
||||||
@@ -8,29 +13,27 @@ function quoteLargeNumbers(jsonStr: string, minDigits: number): string {
|
|||||||
strings.push(str);
|
strings.push(str);
|
||||||
return replacement;
|
return replacement;
|
||||||
});
|
});
|
||||||
const regex = new RegExp(`(-?\\d{${minDigits},})(?=\\s*[,}\\]])`, 'g');
|
const regex = new RegExp(`([+-]?\\d{${minDigits},})(?=\\s*[,}\\]])`, 'g');
|
||||||
const jsonQuotedBigint = jsonWithoutStrings.replace(regex, '"$1"');
|
// 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 jsonQuotedBigint.replace(/"@(\d+)@"/g, (_, ix) => {
|
||||||
return strings[Number(ix)];
|
return strings[Number(ix)];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
JSON.parseBigInt = function (jsonStr: string, options?: { minDigits?: number; fallbackToString?: boolean }): unknown {
|
JSON.parseBigInt = function (jsonStr: string, options?: { minDigits?: number }): unknown {
|
||||||
const minDigits = options?.minDigits ?? 15;
|
const minDigits = options?.minDigits ?? 15;
|
||||||
const fallbackToString = options?.fallbackToString ?? true;
|
|
||||||
|
|
||||||
const safeStr = quoteLargeNumbers(jsonStr, minDigits);
|
const safeStr = quoteLargeNumbers(jsonStr, minDigits);
|
||||||
|
const bigintRegex = new RegExp(`^¤[+-]?\\d{${minDigits},}¤$`);
|
||||||
|
|
||||||
return JSON5.parse(safeStr, (_key, value) => {
|
return JSON5.parse(safeStr, (_key, value) => {
|
||||||
if (typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
const bigintRegex = new RegExp(`^-?\\d{${minDigits},}$`);
|
|
||||||
if (bigintRegex.test(value)) {
|
if (bigintRegex.test(value)) {
|
||||||
try {
|
try {
|
||||||
if (typeof BigInt !== 'undefined') {
|
if (typeof BigInt !== 'undefined') {
|
||||||
return BigInt(value);
|
// take only part xxx between ¤xxx¤
|
||||||
}
|
return BigInt(value.substring(1, value.length - 1));
|
||||||
else if (fallbackToString) {
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
|
|||||||
Reference in new issue
Block a user