-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
124 lines (109 loc) · 3.25 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'use strict';
var changeCase = require('change-case');
var transformationNames = require('./transformations');
module.exports.options = {
recursive: false,
arrayRecursive: false,
throwOnDuplicate: false,
locale: null
};
function setDefaults(object, defaults) {
object = object || {};
for (var i in defaults) {
if (defaults.hasOwnProperty(i) && typeof object[i] === 'undefined') {
object[i] = defaults[i];
}
}
return object;
}
function isObject(value) {
if (!value) {
return false;
}
return typeof value === 'object' || typeof value === 'function';
}
function isArray(value) {
return (Array.isArray && Array.isArray(value)) ||
Object.prototype.toString.call(value) === '[object Array]';
}
function computeNewValue(value, f, options, forceRecurse) {
var valueIsArray = isArray(value);
if (valueIsArray && options.arrayRecursive) {
return transformArray(value, f, options);
} else if (isObject(value) && !valueIsArray && (options.recursive || forceRecurse)) {
return transformObjectKeys(value, f, options);
} else {
return value;
}
}
function transformArray(array, f, options) {
options = setDefaults(options, module.exports.options);
if (!isArray(array)) {
throw new Error('transformArray expects an array');
}
var result = [];
for (var i = 0; i < array.length; i++) {
var value = array[i];
var newValue = computeNewValue(value, f, options, true);
result.push(newValue);
}
return result;
}
function transformObjectKeys(object, f, options) {
options = setDefaults(options, module.exports.options);
var result = {};
for (var key in object) {
if (object.hasOwnProperty(key)) {
var value = object[key];
var newKey = key
if (!(options.exclude && options.exclude.indexOf(key) >= 0)) {
newKey = f(key, options.locale);
}
if (result.hasOwnProperty(newKey) && options.throwOnDuplicate) {
throw new Error('duplicated key ' + newKey);
}
result[newKey] = computeNewValue(value, f, options, false);
}
}
return result;
}
function makeObjectTransformation(f) {
return function (object, options) {
if (!object || !object.hasOwnProperty) {
return object;
}
return transformObjectKeys(object, f, options);
};
}
function makeArrayTransformation(f) {
return function (array, options) {
return transformArray(array, f, options);
};
}
// creates functions that accept any kind of data structure
function makeArbitraryDataTransformation(f) {
return function (data, options) {
if (isArray(data)) {
return transformArray(data, f, options);
} else if (isObject(data)) {
return transformObjectKeys(data, f, options);
} else {
return data;
}
};
}
function exportTransformation(name) {
var f = changeCase[name];
module.exports[name + 'Keys'] = makeObjectTransformation(f);
module.exports[name + 'Array'] = makeArrayTransformation(f);
module.exports['to' + changeCase.ucFirst(name)] = makeArbitraryDataTransformation(f);
}
// reexport all functions exported by `changeCase`
for (var i in changeCase) {
if (changeCase.hasOwnProperty(i)) {
module.exports[i] = changeCase[i];
}
}
for (var i = 0; i < transformationNames.length; i++) {
exportTransformation(transformationNames[i]);
}