Skip to content

Instantly share code, notes, and snippets.

@Akryum
Last active June 7, 2019 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Akryum/7e9388d89fb4a5814afc13f43983a14d to your computer and use it in GitHub Desktop.
Save Akryum/7e9388d89fb4a5814afc13f43983a14d to your computer and use it in GitHub Desktop.
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