Skip to content

Commit 3c6a9e7

Browse files
committed
Case converter part of package
1 parent 5ec00aa commit 3c6a9e7

File tree

4 files changed

+45
-83
lines changed

4 files changed

+45
-83
lines changed

package-lock.json

Lines changed: 11 additions & 79 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
"homepage": "https://github.com/joedelia/ajax2#readme",
1919
"dependencies": {
2020
"babel-runtime": "^6.26.0",
21-
"camelcase-keys": "^7.0.0",
22-
"snakecase-keys": "^4.0.2",
21+
"lodash": "^4.17.21",
2322
"whatwg-fetch": "^2.0.4"
2423
},
2524
"devDependencies": {

src/case-convert.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import camelCaseConverter from 'lodash/camelCase';
2+
import snakeCaseConverter from 'lodash/snakeCase';
3+
4+
export function toCamelCase(obj) {
5+
return deepMapKeys(obj, camelCaseConverter);
6+
}
7+
export function toSnakeCase(obj) {
8+
return deepMapKeys(obj, snakeCaseConverter);
9+
}
10+
11+
function deepMapKeys(obj, mod) {
12+
if ((obj === null || typeof obj !== 'object') && !Array.isArray(obj))
13+
throw new Error('You must provide initial argument as an object');
14+
15+
if (!mod || typeof mod !== 'function')
16+
throw new Error('You must provide a modifier by which your keys will be changed');
17+
18+
function innerRoutine(obj, mod) {
19+
if (Array.isArray(obj))
20+
return obj.map(val => innerRoutine(val, mod));
21+
22+
if (obj !== null && typeof obj === 'object') {
23+
return Object.keys(obj).reduce((accum, key) => {
24+
accum[mod(key)] = innerRoutine(obj[key], mod);
25+
return accum;
26+
}, { });
27+
}
28+
return obj;
29+
}
30+
31+
return innerRoutine(obj, mod);
32+
}

src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'whatwg-fetch'; // eslint-disable-line import/no-unassigned-import
2-
import toCamelCase from 'camelcase-keys';
3-
import toSnakeCase from 'snakecase-keys';
2+
import {toCamelCase, toSnakeCase} from './case-convert';
43
import EventDispatcher from './events';
54

65
let defaultConvertRequest = null,

0 commit comments

Comments
 (0)