-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
127 lines (104 loc) · 2.75 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
125
126
127
'use strict';
var mixin = require('mixin-deep');
var defaults = require('defaults-deep');
var defaultOptions = { overwrite: true };
function regulate(anyValues, anyPromotions, anyEliminations, anyImmutables, optionalOptions) {
var assign, eliminations, immutables, options, promotions;
if (anyEliminations && !isList(anyEliminations)) {
return regulate(anyValues, anyPromotions, [], [], anyEliminations);
} else if (anyImmutables && !isList(anyImmutables)) {
return regulate(anyValues, anyPromotions, anyEliminations, [], anyImmutables);
}
promotions = _hashify(anyPromotions);
eliminations = _hashify(anyEliminations);
immutables = _hashify(anyImmutables);
options = defaults(_options(), defaultOptions);
assign = options.overwrite ? mixin : defaults;
return _regulate(anyValues);
function _options() {
if (isPlainObject(optionalOptions)) {
return optionalOptions;
}
return {};
}
function _regulate(values) {
return _array(values) || _object(values, {}) || values;
}
function _array(values) {
if (Array.isArray(values)) {
return values.map(_regulate);
}
}
function _object(values, accumulator) {
var keys, result;
if (isPlainObject(values)) {
// process normal properies before promotion properties to ensure overwrite option.
keys = sort(Object.keys(values), promotions, eliminations);
result = keys.n.reduce(_normal, accumulator);
result = keys.p.reduce(_promote, result);
return result;
}
function _normal(ret, key) {
if (immutables[key]) {
if (!(key in ret)) {
ret[key] = values[key];
}
} else {
_assign(ret, key, _regulate(values[key]));
}
return ret;
}
function _promote(ret, key) {
var value;
value = values[key];
return _object(value, ret) || _assign(ret, key, _array(value) || value);
}
}
function _assign(ret, key, value) {
var tmp = {};
tmp[key] = value;
return assign(ret, tmp);
}
}
function _hashify(value) {
var result;
result = {};
if (Array.isArray(value)) {
result = value.reduce(function (ret, key) {
ret[key] = true;
return ret;
}, result);
} else if (isString(value)) {
result[value] = true;
}
return result;
}
function isList(value) {
return typeof value === 'string' || Array.isArray(value);
}
function isString(value) {
return typeof value === 'string';
}
function isPlainObject(value) {
if (value === null || typeof value !== 'object') {
return false;
}
return Object.getPrototypeOf(value) === Object.prototype;
}
function sort(keys, promotions, eliminations) {
var i, n, key, ret;
ret = {
p: [],
n: []
};
for (i = 0, n = keys.length; i < n; ++i) {
key = keys[i];
if (promotions[key]) {
ret.p.push(key);
} else if (!eliminations[key]) {
ret.n.push(key);
}
}
return ret;
}
module.exports = regulate;