* feat: support to parse association variable * chore: change comment * feat(operators): support array as value in string operator * refactor: add a special marker * feat: date support to-multi field variables * feat: support for notIn * refactor: rename * test: fix test error * Revert "test: fix test error" This reverts commit 3d139698f6295678a39b77b08c88427f9fafe247. * Revert "refactor: rename" This reverts commit 2e16225c038e18ee25f1136d510cb4746bd9932f. * Revert "feat: support for notIn" This reverts commit 2087e5c4da1429b260890d5136c714ad541955a9. * Revert "feat: date support to-multi field variables" This reverts commit 0d7b2db0512ba7b632a2ab61f37cf83c0d06b9a1. * Revert "refactor: add a special marker" This reverts commit 7ba2e4bc00d79f60a2e90340df65b2938985750a. * Revert "feat(operators): support array as value in string operator" This reverts commit 0897cd19e9863248ef9e2af6c91ee14e805778a4. * refactor: improve code * test: fix can not import style file * feat: only eq and ne oprators support mutil relation fields * test: add example.test.ts * refactor: remove jsonata * Revert "test: add example.test.ts" This reverts commit 0ad2ea458cd8a964891490d2021a5b000f391395. * test: fix error * fix: fix error * refactor: remove async * chore: rebase * test: fix error * test: fix errors
38 lines
846 B
TypeScript
38 lines
846 B
TypeScript
export const getValuesByPath = (obj: object, path: string, defaultValue?: any) => {
|
|
if (!obj) {
|
|
return defaultValue;
|
|
}
|
|
const keys = path.split('.');
|
|
let result: any[] = [];
|
|
let currentValue = obj;
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
const key = keys[i];
|
|
|
|
if (Array.isArray(currentValue)) {
|
|
for (const element of currentValue) {
|
|
const value = getValuesByPath(element, keys.slice(i).join('.'), defaultValue);
|
|
result = result.concat(value);
|
|
}
|
|
break;
|
|
}
|
|
|
|
if (currentValue?.[key] === undefined) {
|
|
break;
|
|
}
|
|
currentValue = currentValue[key];
|
|
|
|
if (i === keys.length - 1) {
|
|
result.push(currentValue);
|
|
}
|
|
}
|
|
|
|
result = result.filter(Boolean);
|
|
|
|
if (result.length === 0) {
|
|
return defaultValue;
|
|
}
|
|
|
|
return result.length === 1 ? result[0] : result;
|
|
};
|