(
object: TObject,
cb: (value: { key: K | string; value: any }) => { key: string | number | symbol; value: any } | undefined,
recursive = false,
)
| 14 | recursive: false, |
| 15 | ): Record<string, any>; |
| 16 | export 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 | } |
no test coverage detected