Skip to content

Commit

Permalink
reactor: refactor some code
Browse files Browse the repository at this point in the history
  • Loading branch information
slievrly committed Feb 11, 2025
1 parent a7c1edd commit 68dbca8
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions saga/seata-saga-statemachine-designer/src/render/PathMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,28 @@

// helpers //////////////////////

// copied and adjusted from https://github.com/adobe-webplatform/Snap.svg/blob/master/src/svg.js
// Regular expression to match tokens in the format {tokenName}
const tokenRegex = /\{([^{}]+)\}/g;
const objNotationRegex = /(?:(?:^|\.)(.+?)(?=\[|\.|$|\()|\[('|")(.+?)\2\])(\(\))?/g; // matches .xxxxx or ["xxxxx"] to run over object properties
// Regular expression to match object property access using dot notation or bracket notation
// It captures properties accessed either directly (e.g., `obj.prop`)
// or indirectly (e.g., `obj['prop']` or `obj[prop]`), as well as function calls (e.g., `obj.method()`)
const objNotationRegex = /(?:(?:^|\.)(.+?)(?=\[|\.|$|\()|\[('|")(.+?)\2\])(\(\))?/g;

function replacer(all, key, obj) {
let res = obj;
key.replace(objNotationRegex, (_, name, quote, quotedName, isFunc) => {
name = name || quotedName;
if (res) {
if (name in res) {
res = res[name];
}
if (typeof res === 'function' && isFunc) {
res = res();
let result = obj;
key.replace(objNotationRegex, (_, name, quote, quotedName, isFunction) => {
const propertyName = name || quotedName;
if (result && propertyName in result) {
result = result[propertyName];

// If the result is a function and is marked as a function, call it to get a "real" value
if (typeof result === 'function' && isFunction) {
result = result();
}
}
});
res = `${res == null || res === obj ? all : res}`;

return res;
//Return the final alternative result, or all if result has not changed
return result == null || result === obj ? all : result;
}

function format(str, obj) {
Expand Down

0 comments on commit 68dbca8

Please sign in to comment.