Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Fix util.js and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Martinez committed Jul 4, 2017
1 parent 5874499 commit 8cb50df
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
14 changes: 7 additions & 7 deletions app/javascript/util/util.js
@@ -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;
}
77 changes: 77 additions & 0 deletions app/javascript/util/util.test.js
@@ -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();
});
});

0 comments on commit 8cb50df

Please sign in to comment.