-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d4a5e51
commit d0e6363
Showing
2 changed files
with
15 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,19 @@ | ||
export function snakeToCamel(key: string) { | ||
if (!key) return '' | ||
|
||
let keyArr = key.split('_') | ||
for (let i = 0; i < keyArr.length; i++) { | ||
if (i !== 0) { | ||
keyArr[i] = keyArr[i][0].toUpperCase() + keyArr[i].substr(1) | ||
} | ||
} | ||
return keyArr.join('') | ||
export const snakeToCamel = (str: string): string => { | ||
return str.replace(/([-_][a-z])/gi, c => c.toUpperCase().replace(/[-_]/g, '')) | ||
} | ||
|
||
type Json = Record<string, unknown> | ||
|
||
export function deepCamelizeKeys(param: Json): Json { | ||
Object.keys(param).map(key => { | ||
let item = param[key] | ||
if (item instanceof Object) { | ||
deepCamelizeKeys(item as Json) | ||
} | ||
if (snakeToCamel(key) !== key) { | ||
param[snakeToCamel(key)] = param[key] | ||
delete param[key] | ||
} | ||
}) | ||
return param | ||
export const deepCamelizeKeys = (item: unknown): unknown => { | ||
if (Array.isArray(item)) { | ||
return item.map((el: unknown) => deepCamelizeKeys(el)) | ||
} else if (typeof item === 'function' || item !== Object(item)) { | ||
return item | ||
} | ||
return Object.fromEntries( | ||
Object.entries(item as Record<string, unknown>).map(([key, value]: [string, unknown]) => [ | ||
key.replace(/([-_][a-z])/gi, c => c.toUpperCase().replace(/[-_]/g, '')), | ||
deepCamelizeKeys(value), | ||
]) | ||
) | ||
} | ||
|
||
export default { deepCamelizeKeys, snakeToCamel } |
d0e6363
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Packaging for test is done in 9377482374