MCPcopy
hub / github.com/directus/directus / get

Function get

packages/utils/shared/get-with-arrays.ts:11–24  ·  view source on GitHub ↗
(object: Record<string, any> | any[], path: string, defaultValue?: unknown)

Source from the content-addressed store, hash-verified

9 * ```
10 */
11export function get(object: Record<string, any> | any[], path: string, defaultValue?: unknown): any {
12 let key = path.split('.')[0]!;
13 const follow = path.split('.').slice(1);
14
15 if (key.includes(':')) key = key.split(':')[0]!;
16
17 const result = Array.isArray(object) ? getArrayResult(object, key) : object?.[key];
18
19 if (result !== undefined && follow.length > 0) {
20 return get(result, follow.join('.'), defaultValue);
21 }
22
23 return result ?? defaultValue;
24}
25
26function getArrayResult(object: unknown[], key: string): unknown[] | undefined {
27 const result = object.map((entry) => entry?.[key as keyof unknown]).filter((entry) => entry);

Callers 5

resolveFnFunction · 0.70
renderMustacheFunction · 0.70
parseDynamicVariableFunction · 0.70
processFilterLevelFunction · 0.70

Calls 3

sliceMethod · 0.80
joinMethod · 0.80
getArrayResultFunction · 0.70

Tested by

no test coverage detected