-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
221 lines (194 loc) · 6.24 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'use strict';
const plugins = {
$param(value, params, config) {
if (typeof value === 'string') {
return get(params, value);
}
const result = get(params, value[0]);
return result === undefined ? config._resolve(value[1], params, true) : result;
},
$template(value, params) {
if (!(typeof value === 'string')) {
value = value.join('');
}
return value.replace(/\$\{([^}]+)\}/g, (_, path) => get(params, path.trim()));
},
$guard(value, params, config) {
const item = find(value, (item) => item[0] === '$default' || get(params, item[0]));
return item && config._resolve(item[1], params, true);
},
$switch(value, params, config) {
const test = get(params, value[0]);
const item = find(value[1], (item) => {
const _case = item[0];
if (_case === test) {
return true;
}
if (Array.isArray(_case) && _case.indexOf(test) !== -1) {
return true;
}
return _case === '$default';
});
return item && config._resolve(item[1], params, true);
},
$function(value, params, config) {
if (typeof value === 'string') {
return get(config._functions, value)(params);
}
const mergedParams = Object.assign({}, params, value[1]);
return get(config._functions, value[0])(mergedParams);
}
};
const keywords = Object.keys(plugins);
class Config {
/**
* @param {object} data - Config data.
* @param {object} [options] - Options.
* @param {object} [options.functions] - Functions.
*/
constructor(data, options) {
this._data = data;
this._functions = get(options, 'functions');
}
/**
* Computes config value.
*
* @param {string|string[]} path - The path of the property to get.
* @param {object} [params] - Dynamic params.
* @returns {*} Config value.
*/
get(path, params) {
if (typeof path === 'string') {
path = path.split('.');
}
const length = path.length;
let value = this._data;
let index = 0;
// eslint-disable-next-line no-eq-null
while (value != null) {
const isLast = index === length;
if (params) {
value = this._resolve(value, params, isLast);
}
if (isLast) {
break;
}
const result = value[path[index++]];
value = result === undefined ? value.$default : result;
}
return value;
}
/**
* Resolves config value.
*
* @param {*} value - Config value.
* @param {object} params - Dynamic params.
* @param {boolean} [deep=false] - Recursively resolve nested properties.
* @returns {*} Config value.
*/
_resolve(value, params, deep) {
if (typeof value === 'object' && value !== null) {
if (Array.isArray(value)) {
if (deep) {
return value.map((value) => this._resolve(value, params, true));
}
} else {
const keyword = find(keywords, (keyword) => value[keyword]);
if (keyword) {
return plugins[keyword](value[keyword], params, this);
}
if (deep) {
return Object.keys(value).reduce((result, key) => {
result[key] = this._resolve(value[key], params, true);
return result;
}, {});
}
}
}
return value;
}
/**
* Binds config to params.
*
* @param {object} params - Dynamic params.
* @returns {object} Bound config.
*/
bind(params) {
return Object.keys(this._data).reduce((result, key) => {
return this._assign(params, result, this._data[key], key);
}, {});
}
/**
* Binds array/object item.
*
* @param {object} params - Dynamic params.
* @param {array|object} result - Target array/object.
* @param {*} value - Array/object item value.
* @param {number|string} key - Index/key.
* @returns {*} Target array/object.
*/
_assign(params, result, value, key) {
if (typeof value === 'object' && value !== null) {
if (Array.isArray(value)) {
value = value.reduce(this._assign.bind(this, params), []);
} else {
const keyword = find(keywords, (keyword) => value[keyword]);
if (keyword) {
// convert dynamic key into getter
return setGetter(result, key, () => {
setGetter(result, key, () => {
throw new Error(`Circular reference found in '${key}'`);
});
const resolved = plugins[keyword](value[keyword], params, this);
return setValue(result, key, resolved);
});
}
value = Object.keys(value).reduce((result, key) => {
return this._assign(params, result, value[key], key);
}, {});
}
}
result[key] = value;
return result;
}
}
// shim for IE
function find(array, callback) {
let result;
array.some((item) => {
if (callback(item)) {
result = item;
return true;
}
return false;
});
return result;
}
function get(object, path) {
path = path.split('.');
const length = path.length;
let index = 0;
// eslint-disable-next-line no-eq-null
while (object != null && index < length) {
object = object[path[index++]];
}
return object;
}
function setGetter(object, key, getter) {
return Object.defineProperty(object, key, {
configurable: true,
enumerable: true,
get: getter,
set: (value) => setValue(object, key, value)
});
}
function setValue(object, key, value) {
Object.defineProperty(object, key, {
configurable: true,
enumerable: true,
writable: true,
value
});
return value;
}
module.exports = Config;