From 9d8f3179a3f2a68c2ba7004c9cb9428280ed8e8e Mon Sep 17 00:00:00 2001 From: Axel Vermeil Date: Tue, 2 Dec 2025 20:26:34 +0000 Subject: [PATCH] optimise decamelizeKeys --- package.json | 1 - src/parserRest.ts | 52 +++++++++++++++++++++++++++++++---------------- yarn.lock | 5 ----- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index ddfd06a5..33bcf977 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,6 @@ "google-auth-library": "^9.15.1", "google-gax": "^5.0.1", "long": "^4.0.0", - "map-obj": "^4.0.0", "stream-json": "^1.8.0" }, "devDependencies": { diff --git a/src/parserRest.ts b/src/parserRest.ts index f1401354..989ba829 100644 --- a/src/parserRest.ts +++ b/src/parserRest.ts @@ -1,8 +1,3 @@ -/** - * JSON Rest parsing - */ - -import mapObject from "map-obj"; import { parse } from "circ-json"; import { toSnakeCase } from "./utils"; @@ -25,23 +20,46 @@ export const decamelizeKeys = (input: any) => { return input; } - const makeMapper = (parentPath?: string) => (key: string, value: any) => { - key = cachedDecamelize(key); + return transform(input); - if (isObject(value)) { - const path = parentPath === undefined ? key : `${parentPath}.${key}`; + function transform(value: any, parentPath?: string): any { + if (!isObject(value)) { + return value; + } - // @ts-ignore - value = mapObject(value, makeMapper(path)); - } else { - value = cachedValueParser(key, parentPath, value); + if (Array.isArray(value)) { + const length = value.length; + const result = new Array(length); + + for (let i = 0; i < length; i += 1) { + const item = value[i]; + result[i] = isObject(item) ? transformObject(item, parentPath) : item; + } + + return result; } - return [key, value]; - }; + return transformObject(value, parentPath); + } - // @ts-ignore - return mapObject(input, makeMapper()); + function transformObject(obj: Record, parentPath?: string) { + const output: Record = {}; + + for (const key of Object.keys(obj)) { + const rawValue = obj[key]; + const newKey = cachedDecamelize(key); + + if (isObject(rawValue)) { + const nextParentPath = + parentPath === undefined ? newKey : `${parentPath}.${newKey}`; + output[newKey] = transform(rawValue, nextParentPath); + } else { + output[newKey] = cachedValueParser(newKey, parentPath, rawValue); + } + } + + return output; + } }; const cachedDecamelize = (key: string) => { diff --git a/yarn.lock b/yarn.lock index 1a17021b..a95696dc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3040,11 +3040,6 @@ makeerror@1.0.12: dependencies: tmpl "1.0.5" -map-obj@^4.0.0: - version "4.3.0" - resolved "https://registry.npmjs.org/map-obj/-/map-obj-4.3.0.tgz" - integrity sha512-hdN1wVrZbb29eBGiGjJbeP8JbKjq1urkHJ/LIP/NY48MZ1QVXUsQBV1G1zvYFHn1XE06cwjBsOI2K3Ulnj1YXQ== - math-intrinsics@^1.1.0: version "1.1.0" resolved "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz"