From af99c7462f5303d0536fe879da7145dc0ac136ef Mon Sep 17 00:00:00 2001 From: Fine Date: Fri, 18 Oct 2024 10:15:06 +0800 Subject: [PATCH] use regex string --- src/locales/lang/en.ts | 2 +- src/locales/lang/es.ts | 2 +- src/locales/lang/zh.ts | 2 +- src/views/dashboard/graphs/Card.vue | 16 +--------------- src/views/dashboard/graphs/Table.vue | 18 ++---------------- 5 files changed, 6 insertions(+), 34 deletions(-) diff --git a/src/locales/lang/en.ts b/src/locales/lang/en.ts index 5ccbf10f..1abdbd67 100644 --- a/src/locales/lang/en.ts +++ b/src/locales/lang/en.ts @@ -387,6 +387,6 @@ const msg = { hierarchyNodeMetrics: "Metrics for Hierarchy Graph Node", hierarchyNodeDashboard: "As dashboard for Hierarchy Graph Node", valueMappings: "Value Mappings", - mappingTip: "Notice: The mapping key is like (-∞, 2] or [4, 10)", + mappingTip: "Notice: The mapping key is a Regex string", }; export default msg; diff --git a/src/locales/lang/es.ts b/src/locales/lang/es.ts index dac733d8..7c3ee5b8 100644 --- a/src/locales/lang/es.ts +++ b/src/locales/lang/es.ts @@ -387,6 +387,6 @@ const msg = { hierarchyNodeMetrics: "Metrics for Hierarchy Graph Node", hierarchyNodeDashboard: "As dashboard for Hierarchy Graph Node", valueMappings: "Value Mappings", - mappingTip: "Notice: The mapping key is like (-∞, 2] or [4, 10)", + mappingTip: "Notice: The mapping key is a Regex string", }; export default msg; diff --git a/src/locales/lang/zh.ts b/src/locales/lang/zh.ts index 46eb585d..23825661 100644 --- a/src/locales/lang/zh.ts +++ b/src/locales/lang/zh.ts @@ -385,6 +385,6 @@ const msg = { hierarchyNodeMetrics: "层次图节点的指标", hierarchyNodeDashboard: "作为层次图节点的dashboard", valueMappings: "值映射", - mappingTip: "注意: 映射键如 (-∞, 2] 或者 [4, 10)", + mappingTip: "注意: 映射键是一个正则表达式字符串", }; export default msg; diff --git a/src/views/dashboard/graphs/Card.vue b/src/views/dashboard/graphs/Card.vue index 7ee9cffb..ff040ee6 100644 --- a/src/views/dashboard/graphs/Card.vue +++ b/src/views/dashboard/graphs/Card.vue @@ -65,23 +65,9 @@ limitations under the License. --> if (valueMappings.value[singleVal.value]) { return valueMappings.value[singleVal.value]; } - const regex = /-?\d+(\.\d+)?/g; const list = Object.keys(valueMappings.value); for (const i of list) { - const k = i.replace(/\s+/g, ""); - let withinRange = false; - const ranges = k.match(regex)?.map(Number) || []; - if (k.startsWith("[")) { - withinRange = Number(singleVal.value) >= ranges[0]; - } else { - withinRange = k.startsWith("(-∞") || Number(singleVal.value) > ranges[0]; - } - if (k.endsWith("]")) { - withinRange = withinRange && Number(singleVal.value) <= (ranges[1] || ranges[0]); - } else { - withinRange = withinRange && (k.endsWith("∞)") || Number(singleVal.value) < (ranges[1] || ranges[0])); - } - if (withinRange) { + if (new RegExp(i).test(String(singleVal.value))) { return valueMappings.value[i] || singleVal.value; } } diff --git a/src/views/dashboard/graphs/Table.vue b/src/views/dashboard/graphs/Table.vue index 8b3387f3..ffc62d3d 100644 --- a/src/views/dashboard/graphs/Table.vue +++ b/src/views/dashboard/graphs/Table.vue @@ -81,24 +81,10 @@ limitations under the License. --> if (valueMappings.value[source]) { return valueMappings.value[source]; } - const regex = /-?\d+(\.\d+)?/g; const list = Object.keys(valueMappings.value); for (const i of list) { - const k = i.replace(/\s+/g, ""); - let withinRange = false; - const ranges = k.match(regex)?.map(Number) || []; - if (k.startsWith("[")) { - withinRange = Number(source) >= ranges[0]; - } else { - withinRange = k.startsWith("(-∞") || Number(source) > ranges[0]; - } - if (k.endsWith("]")) { - withinRange = withinRange && Number(source) <= (ranges[1] || ranges[0]); - } else { - withinRange = withinRange && (k.endsWith("∞)") || Number(source) < (ranges[1] || ranges[0])); - } - if (withinRange) { - return valueMappings.value[i]; + if (new RegExp(i).test(String(source))) { + return valueMappings.value[i] || source; } } return source;