From b3eef455c37af3dd3da999e89b3a1af85bd323b5 Mon Sep 17 00:00:00 2001 From: Stefaan Vandevelde Date: Sun, 14 Jul 2024 20:32:53 +0200 Subject: [PATCH] Extracted inner recursive funtion out of the crush function for better performance --- src/object/crush.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/object/crush.ts b/src/object/crush.ts index 3315fa9f..c0819f48 100644 --- a/src/object/crush.ts +++ b/src/object/crush.ts @@ -21,19 +21,20 @@ type Primitive = | undefined | null; +const crushToPvArray: ( + obj: object, + path: string, +) => Array<{ p: string; v: Primitive }> = (obj: object, path: string) => + Object.entries(obj).flatMap(([key, value]) => + isPrimitive(value) || isDate(value) + ? { p: path === "" ? key : `${path}.${key}`, v: value } + : crushToPvArray(value, path === "" ? key : `${path}.${key}`), + ); + export function crush( value: TValue, ): Record | Record { if (!value) return {}; - const crushToPvArray: ( - obj: object, - path: string, - ) => Array<{ p: string; v: Primitive }> = (obj: object, path: string) => - Object.entries(obj).flatMap(([key, value]) => - isPrimitive(value) || isDate(value) - ? { p: path === "" ? key : `${path}.${key}`, v: value } - : crushToPvArray(value, path === "" ? key : `${path}.${key}`), - ); const result = objectify( crushToPvArray(value, ""),