From 0a7bfb9b55a436fc70ac1fe3f87ba00037682ef3 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 3814a2f4..89c48552 100644 --- a/src/object/crush.ts +++ b/src/object/crush.ts @@ -18,19 +18,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 const 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, ''),