This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
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
Vincent Martinez
committed
Jul 4, 2017
1 parent
5874499
commit 8cb50df
Showing
2 changed files
with
84 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { camelCase, isPlainObject, forEach, mapKeys, mapValues } from 'lodash'; | ||
// @flow | ||
import { camelCase, mapKeys, mapValues } from 'lodash'; | ||
|
||
export function camelizeKeys<T>(obj: T): T { | ||
if (isPlainObject(obj)) { | ||
let aux = mapKeys(obj, (_, key: string) => camelCase(key)); | ||
return mapValues(aux, camelizeKeys); | ||
} else if (Array.isArray(obj)) { | ||
return obj.map((val: mixed) => camelizeKeys(val)); | ||
export function camelizeKeys<T>(obj: T): Object | T { | ||
if (Array.isArray(obj)) return obj.map(v => camelizeKeys(v)); | ||
if (typeof obj === 'object') { | ||
const camelCased = mapKeys(obj, (v, k: string): string => camelCase(k)); | ||
return mapValues(camelCased, (v: Object) => camelizeKeys(v)); | ||
} | ||
return obj; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// @flow | ||
import { camelizeKeys } from './util'; | ||
import { mapValues, mapKeys } from 'lodash'; | ||
|
||
const fixture: Object = { | ||
camelCaseKey: 'lorem', | ||
snake_case_key: 'ipsum', | ||
'kebab-case-key': 'dolor', | ||
PascalCaseKey: 'sit', | ||
'Strangely-formatted_KeyName': 'Strangely-formatted_KeyValue', | ||
array_with_objects: [ | ||
{ snake_case: 'snake_case' }, | ||
{ 'kebab-case': 'kebab-case' }, | ||
], | ||
camel_case_subgroup: { | ||
number_key: 1, | ||
letter_key: 'a', | ||
array_key: [0, 1, 2], | ||
object_key: { | ||
'kebab-case-key': 'value', | ||
}, | ||
}, | ||
}; | ||
|
||
it('keeps camelCase keys intact', () => { | ||
expect(camelizeKeys(fixture)).toHaveProperty('camelCaseKey'); | ||
expect(camelizeKeys(fixture)).toHaveProperty('arrayWithObjects'); | ||
}); | ||
|
||
it('converts snake_case keys to camelCase', () => { | ||
expect(camelizeKeys(fixture)).toHaveProperty('snakeCaseKey', 'ipsum'); | ||
}); | ||
|
||
it('converts kebab-case keys to camelCase', () => { | ||
expect(camelizeKeys(fixture)).toHaveProperty('kebabCaseKey', 'dolor'); | ||
}); | ||
|
||
it('converts PascalCase keys to camelCase', () => { | ||
expect(camelizeKeys(fixture)).toHaveProperty('pascalCaseKey', 'sit'); | ||
}); | ||
|
||
it('converts Stragely-formatted_KeyNames to camelCase', () => { | ||
expect(camelizeKeys(fixture)).toHaveProperty( | ||
'strangelyFormattedKeyName', | ||
'Strangely-formatted_KeyValue' | ||
); | ||
}); | ||
|
||
it('converts array items', () => { | ||
const camelizedArray = camelizeKeys(fixture).arrayWithObjects; | ||
expect(camelizedArray[0]).toHaveProperty('snakeCase', 'snake_case'); | ||
expect(camelizedArray[1]).toHaveProperty('kebabCase', 'kebab-case'); | ||
}); | ||
|
||
it('converts nested keys', () => { | ||
expect(camelizeKeys(fixture).camelCaseSubgroup).toHaveProperty('numberKey'); | ||
}); | ||
|
||
it('converts deeply nested keys', () => { | ||
const { camelCaseSubgroup } = camelizeKeys(fixture); | ||
expect(camelCaseSubgroup.objectKey).toHaveProperty('kebabCaseKey'); | ||
}); | ||
|
||
describe('values', () => { | ||
it('keeps strings intact', () => { | ||
expect(camelizeKeys(fixture).camelCaseKey).toBe('lorem'); | ||
}); | ||
|
||
it('keeps numbers intact', () => { | ||
const { camelCaseSubgroup } = camelizeKeys(fixture); | ||
expect(camelCaseSubgroup.numberKey).toBe(1); | ||
}); | ||
|
||
it('keeps arrays intact', () => { | ||
expect(Array.isArray(camelizeKeys(fixture).arrayWithObjects)).toBeTruthy(); | ||
}); | ||
}); |