nocobase/packages/core/utils/src/getValuesByPath.ts
Zeke Zhang 837f4f4158
refactor(variable): support default value setting (#4583)
* refactor(variable): support default value setting

* chore: add e2e test for URL search params variable

* fix: resolve field linkage errors

* chore: fix unit test

* test: association select data scope linkage

---------

Co-authored-by: katherinehhh <katherine_15995@163.com>
2024-06-07 10:23:28 +08:00

53 lines
1.2 KiB
TypeScript

/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
export const getValuesByPath = (obj: object, path: string, defaultValue?: any) => {
if (!obj) {
return defaultValue;
}
const keys = path.split('.');
let result: any[] = [];
let currentValue = obj;
let shouldReturnArray = false;
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (Array.isArray(currentValue)) {
shouldReturnArray = true;
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((item) => item !== undefined);
if (result.length === 0) {
return defaultValue;
}
if (shouldReturnArray) {
return result.filter((item) => item !== null);
}
return result[0];
};