-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathConfig.js
287 lines (250 loc) · 7.31 KB
/
Config.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
'use strict';
import validators from './validators';
/**
* Sugar api that can be used as an alternative for manually building `State`
* configuration in the expected format. For example, instead of having
* something like this:
*
* ```js
* MyClass.STATE = {
* foo: {
* required: true,
* validator: validators.number,
* value: 13
* }
* };
* ```
*
* You could instead do:
*
* ```js
* MyClass.STATE = {
* foo: Config.required().number().value(13)
* };
* ```
*/
const Config = {
/**
* An object that contains a validator function.
* @typedef {!Object} ConfigWithValidator
*/
/**
* Function that creates `State` object with an `any` validator.
* @return {ConfigWithValidator} `State` configuration object.
*/
any: setPrimitiveValidators('any'),
/**
* Function that creates `State` object with an `array` validator.
* @return {ConfigWithValidator} `State` configuration object.
*/
array: setPrimitiveValidators('array'),
/**
* Function that creates `State` object with an `arrayOf` validator.
* @param {ConfigWithValidator} stateConfig `State` configuration object
* @return {ConfigWithValidator} `State` configuration object.
*/
arrayOf: setNestedValidators('arrayOf'),
/**
* Function that creates `State` object with a `bool` validator.
* @return {ConfigWithValidator} `State` configuration object.
*/
bool: setPrimitiveValidators('bool'),
/**
* Function that creates `State` object with a `func` validator.
* @return {ConfigWithValidator} `State` configuration object.
*/
func: setPrimitiveValidators('func'),
/**
* Creates `State` configuration object with a `rangeOf` validator.
* @param {!Number} min The minimum value allowed.
* @param {!Number} max The maximum value allowed.
* @return {ConfigWithValidator} `State` configuration object.
*/
inRange(min, max) {
return this.validator(validators.inRange(min, max));
},
/**
* Function that creates `State` object with an `instanceOf` validator.
* @return {ConfigWithValidator} `State` configuration object.
*/
instanceOf: setExplicitValueValidators('instanceOf'),
/**
* Function that creates `State` object with a `number` validator.
* @return {ConfigWithValidator} `State` configuration object.
*/
number: setPrimitiveValidators('number'),
/**
* Function that creates `State` object with an `object` validator.
* @return {ConfigWithValidator} `State` configuration object.
*/
object: setPrimitiveValidators('object'),
/**
* Function that creates `State` object with an `objectOf` validator.
* @param {ConfigWithValidator} stateConfig `State` configuration object
* @return {ConfigWithValidator} `State` configuration object.
*/
objectOf: setNestedValidators('objectOf'),
/**
* Function that creates `State` object with an `oneOf` validator.
* @param {!Array} values `State` configuration object
* @return {ConfigWithValidator} `State` configuration object.
*/
oneOf: setExplicitValueValidators('oneOf'),
/**
* Creates `State` configuration object with an `oneOfType` validator.
* @param {ConfigWithValidator[]} validatorArray Array of `State` configuration objects.
* @return {ConfigWithValidator} `State` configuration object.
*/
oneOfType(validatorArray) {
validatorArray = validatorArray.map(
configObj => configObj.config.validator
);
return this.validator(validators.oneOfType(validatorArray));
},
/**
* Creates `State` configuration object with a `shapeOf` validator.
* @param {!Object.<string, ConfigWithValidator>} shapeObj Values being `State` configuration objects.
* @return {ConfigWithValidator} `State` configuration object.
*/
shapeOf(shapeObj) {
shapeObj = destructShapeOfConfigs(shapeObj);
return this.validator(validators.shapeOf(shapeObj));
},
/**
* Function that creates `State` object with an `string` validator.
* @return {ConfigWithValidator} `State` configuration object.
*/
string: setPrimitiveValidators('string'),
/**
* Adds the `internal` flag to the `State` configuration.
* @param {boolean} internal Flag to set "internal" to. True by default.
* @return {!Object} `State` configuration object.
*/
internal(internal = true) {
return mergeConfig(this, {
internal,
});
},
/**
* Adds the `required` flag to the `State` configuration.
* @param {boolean} required Flag to set "required" to. True by default.
* @return {!Object} `State` configuration object.
*/
required(required = true) {
return mergeConfig(this, {
required,
});
},
/**
* Adds a setter to the `State` configuration.
* @param {!function()} setter
* @return {!Object} `State` configuration object.
*/
setter(setter) {
return mergeConfig(this, {
setter,
});
},
/**
* Adds a validator to the `State` configuration.
* @param {!function()} validator
* @return {!Object} `State` configuration object.
*/
validator(validator) {
return mergeConfig(this, {
validator,
});
},
/**
* Adds a default value to the `State` configuration.
* @param {*} value
* @return {!Object} `State` configuration object.
*/
value(value) {
return mergeConfig(this, {
value,
});
},
/**
* Adds a valueFn that will return a default value for the `State` configuration.
* @param {!function()} valueFn
* @return {!Object} `State` configuration object.
*/
valueFn(valueFn) {
return mergeConfig(this, {
valueFn,
});
},
/**
* Adds the `writeOnce` flag to the `State` configuration.
* @param {boolean} writeOnce Flag to set "writeOnce" to. False by default.
* @return {!Object} `State` configuration object.
*/
writeOnce(writeOnce = false) {
return mergeConfig(this, {
writeOnce,
});
},
};
/**
* Recursively sets validators for shapeOf.
* @param {!Object} shape The shape of specific types.
* @return {!Object} Shape object with validators as values.
*/
function destructShapeOfConfigs(shape) {
const keys = Object.keys(shape);
const retShape = {};
keys.forEach(key => {
const value = shape[key];
retShape[key] =
value.config && value.config.validator
? value.config.validator
: destructShapeOfConfigs(value);
});
return retShape;
}
/**
* Merges the given config object into the one that has been built so far.
* @param {!Object} context The object calling this function.
* @param {!Object} config The object to merge to the built config.
* @return {!Object} The final object containing the built config.
*/
function mergeConfig(context, config) {
let obj = context;
const objConfig = obj.config || {};
obj = Object.create(Config);
obj.config = {};
Object.assign(obj.config, objConfig, config);
return obj;
}
/**
* Calls validators with provided argument.
* @param {string} name The name of the validator.
* @return {function()}
*/
function setExplicitValueValidators(name) {
return function(arg) {
return this.validator(validators[name](arg));
};
}
/**
* Calls validators with a single nested config.
* @param {string} name The name of the validator.
* @return {!function()}
*/
function setNestedValidators(name) {
return function(arg) {
return this.validator(validators[name](arg.config.validator));
};
}
/**
* Adds primitive type validators to the config object.
* @param {string} name The name of the validator.
* @return {!function()}
*/
function setPrimitiveValidators(name) {
return function() {
return this.validator(validators[name]);
};
}
export default Config;