forked from BinaryThumb/change-case-object
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (54 loc) · 1.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var camelCase = require('camel-case');
var snakeCase = require('snake-case');
var paramCase = require('param-case');
var pascalCase = require('pascal-case');
var changeKeys = function changeKeys(transformer, obj) {
var objectKeys;
if (Array.isArray(obj)) {
return obj.map(function keysMap(key) {
if (typeof key === 'string') {
return transformer(key);
}
return changeKeys(transformer, key);
});
} else if (typeof obj === 'object' && obj !== null) {
objectKeys = Object.keys(obj);
return objectKeys.map(function keysMap(key) {
return transformer(key);
}).reduce(function keysReducer(object, changedKey, index) {
var objValue;
var transformedValue;
objValue = obj[objectKeys[index]];
transformedValue = changeKeys(transformer, objValue);
object[changedKey] = transformedValue;
return object;
}, {});
}
return obj;
};
var changeCaseObject = {};
changeCaseObject.camel = changeCaseObject.camelCase = function camelCaseObject(obj) {
if (typeof obj === 'string') {
return camelCase(obj);
}
return changeKeys(camelCase, obj);
};
changeCaseObject.snake = changeCaseObject.snakeCase = function snakeCaseObject(obj) {
if (typeof obj === 'string') {
return snakeCase(obj);
}
return changeKeys(snakeCase, obj);
};
changeCaseObject.param = changeCaseObject.paramCase = function paramCaseObject(obj) {
if (typeof obj === 'string') {
return paramCase(obj);
}
return changeKeys(paramCase, obj);
};
changeCaseObject.pascal = changeCaseObject.pascalCase = function pascalCaseObject(obj) {
if (typeof obj === 'string') {
return pascalCase(obj);
}
return changeKeys(pascalCase, obj);
};
module.exports = changeCaseObject;