use regex string

This commit is contained in:
Fine 2024-10-18 10:15:06 +08:00
parent 94eee7861a
commit af99c7462f
5 changed files with 6 additions and 34 deletions

View File

@ -387,6 +387,6 @@ const msg = {
hierarchyNodeMetrics: "Metrics for Hierarchy Graph Node", hierarchyNodeMetrics: "Metrics for Hierarchy Graph Node",
hierarchyNodeDashboard: "As dashboard for Hierarchy Graph Node", hierarchyNodeDashboard: "As dashboard for Hierarchy Graph Node",
valueMappings: "Value Mappings", 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; export default msg;

View File

@ -387,6 +387,6 @@ const msg = {
hierarchyNodeMetrics: "Metrics for Hierarchy Graph Node", hierarchyNodeMetrics: "Metrics for Hierarchy Graph Node",
hierarchyNodeDashboard: "As dashboard for Hierarchy Graph Node", hierarchyNodeDashboard: "As dashboard for Hierarchy Graph Node",
valueMappings: "Value Mappings", 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; export default msg;

View File

@ -385,6 +385,6 @@ const msg = {
hierarchyNodeMetrics: "层次图节点的指标", hierarchyNodeMetrics: "层次图节点的指标",
hierarchyNodeDashboard: "作为层次图节点的dashboard", hierarchyNodeDashboard: "作为层次图节点的dashboard",
valueMappings: "值映射", valueMappings: "值映射",
mappingTip: "注意: 映射键如 (-∞, 2] 或者 [4, 10)", mappingTip: "注意: 映射键是一个正则表达式字符串",
}; };
export default msg; export default msg;

View File

@ -65,23 +65,9 @@ limitations under the License. -->
if (valueMappings.value[singleVal.value]) { if (valueMappings.value[singleVal.value]) {
return valueMappings.value[singleVal.value]; return valueMappings.value[singleVal.value];
} }
const regex = /-?\d+(\.\d+)?/g;
const list = Object.keys(valueMappings.value); const list = Object.keys(valueMappings.value);
for (const i of list) { for (const i of list) {
const k = i.replace(/\s+/g, ""); if (new RegExp(i).test(String(singleVal.value))) {
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) {
return valueMappings.value[i] || singleVal.value; return valueMappings.value[i] || singleVal.value;
} }
} }

View File

@ -81,24 +81,10 @@ limitations under the License. -->
if (valueMappings.value[source]) { if (valueMappings.value[source]) {
return valueMappings.value[source]; return valueMappings.value[source];
} }
const regex = /-?\d+(\.\d+)?/g;
const list = Object.keys(valueMappings.value); const list = Object.keys(valueMappings.value);
for (const i of list) { for (const i of list) {
const k = i.replace(/\s+/g, ""); if (new RegExp(i).test(String(source))) {
let withinRange = false; return valueMappings.value[i] || source;
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];
} }
} }
return source; return source;