Last active
June 7, 2019 17:17
-
-
Save Akryum/7e9388d89fb4a5814afc13f43983a14d to your computer and use it in GitHub Desktop.
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
import Case from 'case' | |
const originalToString = Object.prototype.toString | |
function isPlainObject (obj: any): boolean { | |
return originalToString.call(obj) === '[object Object]' | |
} | |
export function toCamel (data: any): any { | |
if (Array.isArray(data)) { | |
return data.map((item) => toCamel(item)) | |
} else if (isPlainObject(data)) { | |
const result: any = {} | |
for (const key of Object.keys(data)) { | |
result[Case.camel(key)] = toCamel(data[key]) | |
} | |
return result | |
} else { | |
return data | |
} | |
} | |
export function toPascal (data: any): any { | |
if (Array.isArray(data)) { | |
return data.map((item) => toPascal(item)) | |
} else if (isPlainObject(data)) { | |
const result: any = {} | |
for (const key of Object.keys(data)) { | |
result[Case.pascal(key)] = toPascal(data[key]) | |
} | |
return result | |
} else { | |
return data | |
} | |
} | |
export function toSnake (data: any): any { | |
if (Array.isArray(data)) { | |
return data.map((item) => toSnake(item)) | |
} else if (isPlainObject(data)) { | |
const result: any = {} | |
for (const key of Object.keys(data)) { | |
result[Case.snake(key)] = toSnake(data[key]) | |
} | |
return result | |
} else { | |
return data | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment