Skip to content

Instantly share code, notes, and snippets.

@Akryum

Akryum/case.ts

Last active Jun 7, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.