forked from gcanti/tcomb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extend.js
64 lines (58 loc) · 2.33 KB
/
extend.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
var assert = require('./assert');
var isFunction = require('./isFunction');
var isArray = require('./isArray');
var mixin = require('./mixin');
var isStruct = require('./isStruct');
var isInterface = require('./isInterface');
var isObject = require('./isObject');
var refinement = require('./refinement');
var decompose = require('./decompose');
function compose(predicates, unrefinedType, name) {
var result = predicates.reduce(function (type, predicate) {
return refinement(type, predicate);
}, unrefinedType);
if (name) {
result.displayName = name;
result.meta.name = name;
}
return result;
}
function getProps(type) {
return isObject(type) ? type : type.meta.props;
}
function getDefaultProps(type) {
return isObject(type) ? null : type.meta.defaultProps;
}
function pushAll(arr, elements) {
Array.prototype.push.apply(arr, elements);
}
function extend(combinator, mixins, options) {
if (process.env.NODE_ENV !== 'production') {
assert(isFunction(combinator), function () { return 'Invalid argument combinator supplied to extend(combinator, mixins, options), expected a function'; });
assert(isArray(mixins), function () { return 'Invalid argument mixins supplied to extend(combinator, mixins, options), expected an array'; });
}
var props = {};
var prototype = {};
var predicates = [];
var defaultProps = {};
mixins.forEach(function (x, i) {
var decomposition = decompose(x);
var unrefinedType = decomposition.unrefinedType;
if (process.env.NODE_ENV !== 'production') {
assert(isObject(unrefinedType) || isStruct(unrefinedType) || isInterface(unrefinedType), function () { return 'Invalid argument mixins[' + i + '] supplied to extend(combinator, mixins, options), expected an object, struct, interface or a refinement (of struct or interface)'; });
}
pushAll(predicates, decomposition.predicates);
mixin(props, getProps(unrefinedType));
mixin(prototype, unrefinedType.prototype);
mixin(defaultProps, getDefaultProps(unrefinedType), true);
});
options = combinator.getOptions(options);
options.defaultProps = mixin(defaultProps, options.defaultProps, true);
var result = compose(predicates, combinator(props, {
strict: options.strict,
defaultProps: options.defaultProps
}), options.name);
mixin(result.prototype, prototype);
return result;
}
module.exports = extend;