Skip to content

Commit 7a7ae06

Browse files
Merge pull request #528 from Opteo/faster-decamelize
Optimise decamelizeKeys
2 parents 5639e60 + 9d8f317 commit 7a7ae06

3 files changed

Lines changed: 35 additions & 23 deletions

File tree

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"google-auth-library": "^9.15.1",
2626
"google-gax": "^5.0.1",
2727
"long": "^4.0.0",
28-
"map-obj": "^4.0.0",
2928
"stream-json": "^1.8.0"
3029
},
3130
"devDependencies": {

src/parserRest.ts

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/**
2-
* JSON Rest parsing
3-
*/
4-
5-
import mapObject from "map-obj";
61
import { parse } from "circ-json";
72

83
import { toSnakeCase } from "./utils";
@@ -25,23 +20,46 @@ export const decamelizeKeys = (input: any) => {
2520
return input;
2621
}
2722

28-
const makeMapper = (parentPath?: string) => (key: string, value: any) => {
29-
key = cachedDecamelize(key);
23+
return transform(input);
3024

31-
if (isObject(value)) {
32-
const path = parentPath === undefined ? key : `${parentPath}.${key}`;
25+
function transform(value: any, parentPath?: string): any {
26+
if (!isObject(value)) {
27+
return value;
28+
}
3329

34-
// @ts-ignore
35-
value = mapObject(value, makeMapper(path));
36-
} else {
37-
value = cachedValueParser(key, parentPath, value);
30+
if (Array.isArray(value)) {
31+
const length = value.length;
32+
const result = new Array(length);
33+
34+
for (let i = 0; i < length; i += 1) {
35+
const item = value[i];
36+
result[i] = isObject(item) ? transformObject(item, parentPath) : item;
37+
}
38+
39+
return result;
3840
}
3941

40-
return [key, value];
41-
};
42+
return transformObject(value, parentPath);
43+
}
4244

43-
// @ts-ignore
44-
return mapObject(input, makeMapper());
45+
function transformObject(obj: Record<string, any>, parentPath?: string) {
46+
const output: Record<string, any> = {};
47+
48+
for (const key of Object.keys(obj)) {
49+
const rawValue = obj[key];
50+
const newKey = cachedDecamelize(key);
51+
52+
if (isObject(rawValue)) {
53+
const nextParentPath =
54+
parentPath === undefined ? newKey : `${parentPath}.${newKey}`;
55+
output[newKey] = transform(rawValue, nextParentPath);
56+
} else {
57+
output[newKey] = cachedValueParser(newKey, parentPath, rawValue);
58+
}
59+
}
60+
61+
return output;
62+
}
4563
};
4664

4765
const cachedDecamelize = (key: string) => {

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3040,11 +3040,6 @@ makeerror@1.0.12:
30403040
dependencies:
30413041
tmpl "1.0.5"
30423042

3043-
map-obj@^4.0.0:
3044-
version "4.3.0"
3045-
resolved "https://registry.npmjs.org/map-obj/-/map-obj-4.3.0.tgz"
3046-
integrity sha512-hdN1wVrZbb29eBGiGjJbeP8JbKjq1urkHJ/LIP/NY48MZ1QVXUsQBV1G1zvYFHn1XE06cwjBsOI2K3Ulnj1YXQ==
3047-
30483043
math-intrinsics@^1.1.0:
30493044
version "1.1.0"
30503045
resolved "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz"

0 commit comments

Comments
 (0)