mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +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 '@/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('JSON.parseBigInt', () => {
|
||||
it('should parse bigint in JSON (but not in strings)', () => {
|
||||
expect(`{
|
||||
"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": "操作成功"
|
||||
}
|
||||
`);
|
||||
expect(JSON.stringify(JSON.parseBigInt(bigJson))).toBe(bigJson.replace(/[\r\n]/g, ''));
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,10 @@
|
||||
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 {
|
||||
const strings: string[] = [];
|
||||
// Replace each string with its index
|
||||
@@ -8,29 +13,27 @@ function quoteLargeNumbers(jsonStr: string, minDigits: number): string {
|
||||
strings.push(str);
|
||||
return replacement;
|
||||
});
|
||||
const regex = new RegExp(`(-?\\d{${minDigits},})(?=\\s*[,}\\]])`, 'g');
|
||||
const jsonQuotedBigint = jsonWithoutStrings.replace(regex, '"$1"');
|
||||
const regex = new RegExp(`([+-]?\\d{${minDigits},})(?=\\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)];
|
||||
});
|
||||
}
|
||||
|
||||
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 fallbackToString = options?.fallbackToString ?? true;
|
||||
|
||||
const safeStr = quoteLargeNumbers(jsonStr, minDigits);
|
||||
const bigintRegex = new RegExp(`^¤[+-]?\\d{${minDigits},}¤$`);
|
||||
|
||||
return JSON5.parse(safeStr, (_key, value) => {
|
||||
if (typeof value === 'string') {
|
||||
const bigintRegex = new RegExp(`^-?\\d{${minDigits},}$`);
|
||||
if (bigintRegex.test(value)) {
|
||||
try {
|
||||
if (typeof BigInt !== 'undefined') {
|
||||
return BigInt(value);
|
||||
}
|
||||
else if (fallbackToString) {
|
||||
return value;
|
||||
// take only part xxx between ¤xxx¤
|
||||
return BigInt(value.substring(1, value.length - 1));
|
||||
}
|
||||
}
|
||||
catch {
|
||||
|
||||
Reference in new issue
Block a user