Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

fix: check nullable for snakeToCamel #584

Merged
merged 2 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/api-server/src/base/gw-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ function toConfigEoaScripts(nodeInfo: NodeInfo) {
}

function toApiNodeInfo(nodeInfo: GwNodeInfo): NodeInfo {
// todo: use determinable converting to replace snakeToCamel
return snakeToCamel(nodeInfo, ["code_hash", "hash_type"]);
}

Expand Down
30 changes: 21 additions & 9 deletions packages/api-server/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ export function toSnake(s: string) {
return s.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
}

// convert object key snake_name => camelName
export function isObjectOrArray(obj: any) {
if (
Object.prototype.toString.call(obj) === "[object Object]" ||
Object.prototype.toString.call(obj) === "[object Array]"
) {
return true;
}
return false;
}

// convert object/array key snake_name => camelName
export function snakeToCamel(
t: object,
excludeKeys: string[] = [],
Expand All @@ -38,12 +48,13 @@ export function snakeToCamel(
throw new Error("[snakeToCamel] recursive depth reached max limit.");
}

if (!isObjectOrArray(t)) {
return t;
}

let camel: any = {};
Object.entries(t).map(([key, value]) => {
let newValue =
typeof value === "object"
? snakeToCamel(value, excludeKeys, depthLimit - 1)
: value;
let newValue = snakeToCamel(value, excludeKeys, depthLimit - 1);
const newKey = excludeKeys.includes(key) ? key : toCamel(key);
camel[newKey] = Array.isArray(value) ? Object.values(newValue) : newValue;
});
Expand All @@ -60,12 +71,13 @@ export function camelToSnake(
throw new Error("[camelToSnake] recursive depth reached max limit.");
}

if (!isObjectOrArray(t)) {
return t;
}

let snake: any = {};
Object.entries(t).map(([key, value]) => {
let newValue =
typeof value === "object"
? camelToSnake(value, excludeKeys, depthLimit - 1)
: value;
let newValue = camelToSnake(value, excludeKeys, depthLimit - 1);
const newKey = excludeKeys.includes(key) ? key : toSnake(key);
snake[newKey] = Array.isArray(value) ? Object.values(newValue) : newValue;
});
Expand Down
20 changes: 20 additions & 0 deletions packages/api-server/tests/utils/convention.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ test("snakeToCamel", (t) => {
},
},
hello_array: [{ first_array: 1 }],
hello_nullable: {
null_val: null,
nan_val: NaN,
undefined_val: undefined,
},
},
};
const expectObj = {
Expand All @@ -36,6 +41,11 @@ test("snakeToCamel", (t) => {
},
},
helloArray: [{ firstArray: 1 }],
helloNullable: {
nullVal: null,
nanVal: NaN,
undefinedVal: undefined,
},
},
};
t.deepEqual(snakeToCamel(obj), expectObj);
Expand All @@ -58,6 +68,11 @@ test("camelToSnake", (t) => {
},
},
hello_array: [{ first_array: 1 }],
hello_nullable: {
null_val: null,
nan_val: NaN,
undefined_val: undefined,
},
},
};
const obj = {
Expand All @@ -76,6 +91,11 @@ test("camelToSnake", (t) => {
},
},
helloArray: [{ firstArray: 1 }],
helloNullable: {
nullVal: null,
nanVal: NaN,
undefinedVal: undefined,
},
},
};
t.deepEqual(camelToSnake(obj), expectObj);
Expand Down