Last active
May 28, 2024 13:24
-
-
Save jourdanrodrigues/45b449bd63040afb1d34bce46286bbf8 to your computer and use it in GitHub Desktop.
Parse to camel case to snake case and back
This file contains hidden or 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 parse from 'parse' | |
describe('Parser "keysToCamelCase"', () => { | |
it('should return a new object with camel case keys when receiving an object', () => { | |
const object = { first_name_1: 'a name' } | |
const output = parse(object).keysToCamelCase() | |
expect(output).toEqual({ firstName1: 'a name' }) | |
}) | |
it('should return a new object list with camel case keys when receiving an array', () => { | |
const object = { | |
attr_1: 'value1', | |
attr_number_2: 2, | |
attr_number_3: { | |
attr_number_4: [ | |
{ | |
attr_number_5: 'value3', | |
}, | |
], | |
}, | |
} | |
const objects = ['string in root', object, object] | |
const output = parse(objects).keysToCamelCase() | |
const expectedObject = { | |
attr1: 'value1', | |
attrNumber2: 2, | |
attrNumber3: { | |
attrNumber4: [ | |
{ | |
attrNumber5: 'value3', | |
}, | |
], | |
}, | |
} | |
expect(output).toEqual(['string in root', expectedObject, expectedObject]) | |
}) | |
}) | |
describe('Parser "toCamelCase"', () => { | |
it('should return a camel case string', () => { | |
const output = parse('snake_case_string_1').toCamelCase() | |
expect(output).toEqual('snakeCaseString1') | |
}) | |
}) | |
describe('Parser "toSnakeCase"', () => { | |
it('should return a snake case string', () => { | |
const output = parse('camelCaseString1').toSnakeCase() | |
expect(output).toEqual('camel_case_string_1') | |
}) | |
}) | |
describe('Parser "keysToSnakeCase"', () => { | |
it('should return a new object with snake case keys when receiving an object', () => { | |
const object = { firstName1: 'value1' } | |
const output = parse(object).keysToSnakeCase() | |
expect(output).toEqual({ first_name_1: 'value1' }) | |
}) | |
it('should return a new object list with snake case keys when receiving an array', () => { | |
const object = { | |
attr1: 'value1', | |
attrNumber2: 2, | |
attrNumber3: { | |
attrNumber4: [ | |
{ | |
attrNumber5: 'value3', | |
}, | |
], | |
}, | |
} | |
const objects = ['string in root', object, object] | |
const output = parse(objects).keysToSnakeCase() | |
const expectedObject = { | |
attr_1: 'value1', | |
attr_number_2: 2, | |
attr_number_3: { | |
attr_number_4: [ | |
{ | |
attr_number_5: 'value3', | |
}, | |
], | |
}, | |
} | |
expect(output).toEqual(['string in root', expectedObject, expectedObject]) | |
}) | |
}) |
This file contains hidden or 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
type CaseParser = (string: string) => string | |
type ParserInstance = { | |
toCamelCase (): string, | |
toSnakeCase (): string, | |
keysToCamelCase (): object, | |
keysToSnakeCase (): object, | |
} | |
type Subject = string | number | object | SubjectArray | |
interface SubjectArray extends Array<Subject> { | |
} | |
export default (subject: Subject): ParserInstance => ({ | |
toCamelCase: (): string => parseToCamelCase(subject as string), | |
toSnakeCase: (): string => parseToSnakeCase(subject as string), | |
keysToCamelCase: () => parseKeysToCase(subject as object, parseToCamelCase) as object, | |
keysToSnakeCase: () => parseKeysToCase(subject as object, parseToSnakeCase) as object, | |
}) | |
function parseKeysToCase (subject: Subject, caseParser: CaseParser): Subject { | |
if (subject instanceof Array) { | |
return subject.map(item => parseValueKeysToCase(item, caseParser)) | |
} | |
return Object.entries(subject).reduce( | |
(newObject: object, [key, value]: Array<any>): object => ( | |
Object.assign({}, newObject, { [caseParser(key)]: parseValueKeysToCase(value, caseParser) }) | |
), | |
{}, | |
) | |
} | |
function parseValueKeysToCase (value: any, caseParser: CaseParser): any { | |
return value instanceof Object ? parseKeysToCase(value, caseParser) : value | |
} | |
function parseToCamelCase (string: string): string { | |
return string.replace(/_(.)/g, (_, char) => char.toUpperCase()) | |
} | |
function parseToSnakeCase (string: string): string { | |
return string.replace(/[A-Z\d]/g, char => `_${char.toLowerCase()}`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment