From 39ced0881b25440196998a0db4159673ca815d05 Mon Sep 17 00:00:00 2001 From: YuShifan <894402575bt@gmail.com> Date: Thu, 26 Sep 2024 17:43:38 +0800 Subject: [PATCH] refactor(rule): change to use json big int utils from shared utils --- package.json | 5 +- src/common/jsonUtils.ts | 53 ----------------- src/shims-vue.d.ts | 1 - src/views/RuleEngine/components/SQLTest.vue | 4 +- yarn.lock | 65 ++++++++++++++++++--- 5 files changed, 61 insertions(+), 67 deletions(-) delete mode 100644 src/common/jsonUtils.ts diff --git a/package.json b/package.json index 32f38fbf3..63e75d79f 100644 --- a/package.json +++ b/package.json @@ -18,8 +18,8 @@ }, "dependencies": { "@element-plus/icons-vue": "^1.1.0", - "@emqx/shared-ui-i18n": "~0.0.12", - "@emqx/shared-ui-utils": "^0.0.11", + "@emqx/shared-ui-i18n": "~0.0.25", + "@emqx/shared-ui-utils": "^0.0.14", "@highlightjs/vue-plugin": "^2.1.0", "@vue-flow/core": "^1.20.1", "axios": "^1.4.0", @@ -31,7 +31,6 @@ "highlight.js": "10.7.3", "hocon-parser": "^1.0.1", "js-base64": "^3.7.2", - "json-bigint": "^1.0.0", "json-to-avro": "^1.1.1", "lodash": "^4.17.21", "marked": "^4.0.9", diff --git a/src/common/jsonUtils.ts b/src/common/jsonUtils.ts deleted file mode 100644 index 59a56ec9d..000000000 --- a/src/common/jsonUtils.ts +++ /dev/null @@ -1,53 +0,0 @@ -import JSONBig from 'json-bigint' - -// JSONBig with default configuration, suitable for handling both integer and floating-point big numbers -const jsonBigNumber = JSONBig - -// JSONBig configured to use native BigInt, optimized for integer-only big numbers -const jsonBigInt = JSONBig({ - useNativeBigInt: true, -}) - -/** - * Parse JSON string with enhanced support for big numbers. - * @param value The string to parse as JSON - * @param reviver A function that transforms the results - * @returns Parsed JSON object - */ -const jsonParse = (value: string, reviver?: (this: any, key: string, value: any) => any): any => { - try { - // Attempt to parse using native BigInt for integer-only JSON - return jsonBigInt.parse(value, reviver) - } catch { - try { - // If that fails, try parsing with BigNumber for floating-point numbers - return jsonBigNumber.parse(value, reviver) - } catch { - // If both custom parsers fail, fall back to native JSON.parse - return JSON.parse(value, reviver) - } - } -} - -/** - * Stringify JSON with enhanced support for big numbers. - * @param value The value to convert to a JSON string - * @param replacer A function that alters the behavior of the stringification process - * @param space Adds indentation, white space, and line break characters to the return-value JSON text - * @returns JSON string - */ -const jsonStringify = ( - value: Record, - replacer?: (this: any, key: string, value: any) => any | (number | string)[] | null, - space?: string | number, -): string => { - try { - // Attempt to stringify using native BigInt for integer-only JSON - return jsonBigInt.stringify(value, replacer as any, space) - } catch { - // If that fails, use BigNumber library (note: integers will be in scientific notation) - return jsonBigNumber.stringify(value, replacer as any, space) - } -} - -export { jsonParse, jsonStringify } diff --git a/src/shims-vue.d.ts b/src/shims-vue.d.ts index 0e64b744f..6f1c9022a 100644 --- a/src/shims-vue.d.ts +++ b/src/shims-vue.d.ts @@ -12,5 +12,4 @@ declare module '@/common/highlight/sql' declare module 'js-sql-parser' declare module 'highlight.js/lib/core' declare module 'highlight.js/lib/languages/sql' -declare module 'json-bigint' declare module 'hocon-parser' diff --git a/src/views/RuleEngine/components/SQLTest.vue b/src/views/RuleEngine/components/SQLTest.vue index c560aca3a..558070ec7 100644 --- a/src/views/RuleEngine/components/SQLTest.vue +++ b/src/views/RuleEngine/components/SQLTest.vue @@ -114,7 +114,7 @@ import { PropType, defineExpose, defineProps, onUnmounted, ref, watch } from 'vu import FromSelect from '../components/FromSelect.vue' import RuleTest from './RuleTest.vue' import TestSQLContextForm from './TestSQLContextForm.vue' -import { jsonParse, jsonStringify } from '@/common/jsonUtils' +import { jsonBigIntParse, jsonBigIntStringify } from '@emqx/shared-ui-utils' const { tl, t } = useI18nTl('RuleEngine') @@ -155,7 +155,7 @@ const submitTestSQL = async () => { try { res = await testsql({ context: getMockContext(), sql: ruleSql.value }) if (res) { - resultData.value = jsonStringify(jsonParse(res)) + resultData.value = jsonBigIntStringify(jsonBigIntParse(res)) ElMessage.success(tl('testPassed')) } } catch (error) { diff --git a/yarn.lock b/yarn.lock index da40a6d46..44a5db4f2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1195,15 +1195,19 @@ resolved "https://registry.yarnpkg.com/@element-plus/icons-vue/-/icons-vue-2.0.9.tgz#b7777c57534522e387303d194451d50ff549d49a" integrity sha512-okdrwiVeKBmW41Hkl0eMrXDjzJwhQMuKiBOu17rOszqM+LS/yBYpNQNV5Jvoh06Wc+89fMmb/uhzf8NZuDuUaQ== -"@emqx/shared-ui-i18n@~0.0.12": - version "0.0.20" - resolved "https://registry.yarnpkg.com/@emqx/shared-ui-i18n/-/shared-ui-i18n-0.0.20.tgz#5a7c1b4f65f5a2e7fe9345e47052da5b364ecc0a" - integrity sha512-ft3EE3034VBr27mlcjHVHHbdkTj4ewbk14UbmXDXeZqUQD6I7DbI1kyjIQgDvcyPq4L1ywOXiQKIbktkbNYvog== +"@emqx/shared-ui-i18n@~0.0.25": + version "0.0.25" + resolved "https://registry.yarnpkg.com/@emqx/shared-ui-i18n/-/shared-ui-i18n-0.0.25.tgz#bf5dc58fa8adc85489f60bc8c70df0f9741c780e" + integrity sha512-oVfaVglfHSq5aUmMB0BrwMaqE+7cJgBaL9aZdSbLhSwRci6yEmuF+TGreLtOGFeTb1n8aTj8BpG/sClJtm5kTQ== -"@emqx/shared-ui-utils@^0.0.11": - version "0.0.11" - resolved "https://registry.yarnpkg.com/@emqx/shared-ui-utils/-/shared-ui-utils-0.0.11.tgz#4d301e736e32f8d8d26dacc981c359a6f34224f8" - integrity sha512-/TH3KMqs+7FK1IuDlUPXPaKGZMrT6Zhu/CtOJMREiBo28dsdSVkVClOh3OSWtb315XTLo+Xglpq7h5DI1L2GGw== +"@emqx/shared-ui-utils@^0.0.14": + version "0.0.14" + resolved "https://registry.yarnpkg.com/@emqx/shared-ui-utils/-/shared-ui-utils-0.0.14.tgz#1778dfb0d22fac40c8d159c03d5db800f644e983" + integrity sha512-65TsDNoEyb6obdupU4zuFSxjTa7hXxS8n+BdJ5CvpBuZCPne4zMNV3djuSEVS3LqlwV8RrIzKzN7QZar1G+0bQ== + dependencies: + json-bigint "^1.0.0" + papaparse "^5.4.1" + vue-i18n "^9.10.2" "@esbuild/android-arm@0.15.18": version "0.15.18" @@ -1295,6 +1299,14 @@ cssnano-preset-default "^4.0.0" postcss "^7.0.0" +"@intlify/core-base@9.14.1": + version "9.14.1" + resolved "https://registry.yarnpkg.com/@intlify/core-base/-/core-base-9.14.1.tgz#405b43dca3a38ab70dbb06a818d0a5b8a835fcc9" + integrity sha512-rG5/hlNW6Qfve41go37szEf0mVLcfhYuOu83JcY0jZKasnwsrcZYYWDzebCcuO5I/6Sy1JFWo9p+nvkQS1Dy+w== + dependencies: + "@intlify/message-compiler" "9.14.1" + "@intlify/shared" "9.14.1" + "@intlify/core-base@9.2.0-beta.22": version "9.2.0-beta.22" resolved "https://registry.yarnpkg.com/@intlify/core-base/-/core-base-9.2.0-beta.22.tgz#0a6a284770371a9b27feeace9c8448c1c5df0ae3" @@ -1312,6 +1324,14 @@ dependencies: "@intlify/shared" "9.2.0-beta.22" +"@intlify/message-compiler@9.14.1": + version "9.14.1" + resolved "https://registry.yarnpkg.com/@intlify/message-compiler/-/message-compiler-9.14.1.tgz#e361133e2849453c55cb3feb856898ff563e70d1" + integrity sha512-MY8hwukJBnXvGAncVKlHsqKDQ5ZcQx4peqEmI8wBUTXn4pezrtTGYXNoz81cLyEEHB+L/zlKWVBSh5TiX4gYoQ== + dependencies: + "@intlify/shared" "9.14.1" + source-map-js "^1.0.2" + "@intlify/message-compiler@9.2.0-beta.22": version "9.2.0-beta.22" resolved "https://registry.yarnpkg.com/@intlify/message-compiler/-/message-compiler-9.2.0-beta.22.tgz#02548ec10cff6466052116345a98d072bca4f943" @@ -1320,6 +1340,11 @@ "@intlify/shared" "9.2.0-beta.22" source-map "0.6.1" +"@intlify/shared@9.14.1": + version "9.14.1" + resolved "https://registry.yarnpkg.com/@intlify/shared/-/shared-9.14.1.tgz#11c3d6103615d8b0203224826d2955987c058b72" + integrity sha512-XjHu6PEQup9MnP1x0W9y0nXXfq9jFftAYSfV11hryjtH4XqXP8HrzMvXI+ZVifF+jZLszaTzIhvukllplxTQTg== + "@intlify/shared@9.2.0-beta.22": version "9.2.0-beta.22" resolved "https://registry.yarnpkg.com/@intlify/shared/-/shared-9.2.0-beta.22.tgz#26144144cd93b3100f20b370e9f1bc7b5e955b6a" @@ -2780,6 +2805,11 @@ resolved "https://registry.yarnpkg.com/@vue/devtools-api/-/devtools-api-6.0.0-beta.20.1.tgz#5b499647e929c35baf2a66a399578f9aa4601142" integrity sha512-R2rfiRY+kZugzWh9ZyITaovx+jpU4vgivAEAiz80kvh3yviiTU3CBuGuyWpSwGz9/C7TkSWVM/FtQRGlZ16n8Q== +"@vue/devtools-api@^6.5.0": + version "6.6.4" + resolved "https://registry.yarnpkg.com/@vue/devtools-api/-/devtools-api-6.6.4.tgz#cbe97fe0162b365edc1dba80e173f90492535343" + integrity sha512-sGhTPMuXqZ1rVOk32RylztWkfXTRhuS7vgAKv0zjqk8gbsHkJ7xfFf+jbySxt7tWObEJwyKaHMikV/WGDiQm8g== + "@vue/eslint-config-prettier@^6.0.0": version "6.0.0" resolved "https://registry.yarnpkg.com/@vue/eslint-config-prettier/-/eslint-config-prettier-6.0.0.tgz#ad5912b308f4ae468458e02a2b05db0b9d246700" @@ -9873,6 +9903,11 @@ pako@~1.0.5: resolved "https://registry.yarnpkg.com/pako/-/pako-1.0.11.tgz#6c9599d340d54dfd3946380252a35705a6b992bf" integrity sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw== +papaparse@^5.4.1: + version "5.4.1" + resolved "https://registry.yarnpkg.com/papaparse/-/papaparse-5.4.1.tgz#f45c0f871853578bd3a30f92d96fdcfb6ebea127" + integrity sha512-HipMsgJkZu8br23pW15uvo6sib6wne/4woLZPlFf3rpDyMe9ywEXUsuD7+6K9PRkJlVT51j/sCOYDKGGS3ZJrw== + parallel-transform@^1.1.0: version "1.2.0" resolved "https://registry.yarnpkg.com/parallel-transform/-/parallel-transform-1.2.0.tgz#9049ca37d6cb2182c3b1d2c720be94d14a5814fc" @@ -11608,6 +11643,11 @@ source-map-js@^0.6.2: resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-0.6.2.tgz#0bb5de631b41cfbda6cfba8bd05a80efdfd2385e" integrity sha512-/3GptzWzu0+0MBQFrDKzw/DvvMTUORvgY6k6jd/VS6iCR4RDTKWH6v6WPwQoUO8667uQEf9Oe38DxAYWY5F/Ug== +source-map-js@^1.0.2: + version "1.2.1" + resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.2.1.tgz#1ce5650fddd87abc099eda37dcff024c2667ae46" + integrity sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA== + source-map-resolve@^0.5.0: version "0.5.3" resolved "https://registry.yarnpkg.com/source-map-resolve/-/source-map-resolve-0.5.3.tgz#190866bece7553e1f8f267a2ee82c606b5509a1a" @@ -12764,6 +12804,15 @@ vue-hot-reload-api@^2.3.0: resolved "https://registry.yarnpkg.com/vue-hot-reload-api/-/vue-hot-reload-api-2.3.4.tgz#532955cc1eb208a3d990b3a9f9a70574657e08f2" integrity sha512-BXq3jwIagosjgNVae6tkHzzIk6a8MHFtzAdwhnV5VlvPTFxDCvIttgSiHWjdGoTJvXtmRu5HacExfdarRcFhog== +vue-i18n@^9.10.2: + version "9.14.1" + resolved "https://registry.yarnpkg.com/vue-i18n/-/vue-i18n-9.14.1.tgz#5bd2d7659e1a1dcda28df66e78aa786d1792ef31" + integrity sha512-xjxV0LYc1xQ8TbAVfIyZiOSS8qoU1R0YwV7V5I8I6Fd64+zvsTsdPgtylPsie3Vdt9wekeYhr+smKDeaK6RBuA== + dependencies: + "@intlify/core-base" "9.14.1" + "@intlify/shared" "9.14.1" + "@vue/devtools-api" "^6.5.0" + vue-i18n@^9.2.0-beta.19: version "9.2.0-beta.22" resolved "https://registry.yarnpkg.com/vue-i18n/-/vue-i18n-9.2.0-beta.22.tgz#f0a90fa578e9aaf40adb9b6a37d0b2986a0e8912"