MCPcopy
hub / github.com/RocketChat/Rocket.Chat / objectMap

Function objectMap

packages/tools/src/objectMap.ts:16–35  ·  view source on GitHub ↗
(
	object: TObject,
	cb: (value: { key: K | string; value: any }) => { key: string | number | symbol; value: any } | undefined,
	recursive = false,
)

Source from the content-addressed store, hash-verified

14 recursive: false,
15): Record<string, any>;
16export function objectMap<TObject extends Record<string, any> = Record<string, any>, K extends keyof TObject | string = keyof TObject>(
17 object: TObject,
18 cb: (value: { key: K | string; value: any }) => { key: string | number | symbol; value: any } | undefined,
19 recursive = false,
20): Record<string, any> {
21 return Object.fromEntries(
22 Object.keys(object)
23 .map((key) => {
24 const value = object[key as K];
25 if (recursive && value && typeof value === 'object' && !Array.isArray(value) && !((value as any) instanceof Date)) {
26 const newValue = objectMap(value, cb as any, true);
27 return cb({ key, value: newValue });
28 }
29
30 return cb({ key, value });
31 })
32 .filter((item) => !!item)
33 .map((item) => [item.key, item.value]),
34 );
35}

Callers 1

objectMap.spec.tsFile · 0.90

Calls 4

cbFunction · 0.85
filterMethod · 0.80
keysMethod · 0.80
mapMethod · 0.45

Tested by

no test coverage detected