forked from gcanti/tcomb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intersection.js
61 lines (47 loc) · 1.83 KB
/
intersection.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
var assert = require('./assert');
var isTypeName = require('./isTypeName');
var isFunction = require('./isFunction');
var isArray = require('./isArray');
var forbidNewOperator = require('./isIdentity');
var is = require('./is');
var getTypeName = require('./getTypeName');
var isIdentity = require('./isIdentity');
function getDefaultName(types) {
return types.map(getTypeName).join(' & ');
}
function intersection(types, name) {
if (process.env.NODE_ENV !== 'production') {
assert(isArray(types) && types.every(isFunction) && types.length >= 2, function () { return 'Invalid argument types ' + assert.stringify(types) + ' supplied to intersection(types, [name]) combinator (expected an array of at least 2 types)'; });
assert(isTypeName(name), function () { return 'Invalid argument name ' + assert.stringify(name) + ' supplied to intersection(types, [name]) combinator (expected a string)'; });
}
var displayName = name || getDefaultName(types);
var identity = types.every(isIdentity);
function Intersection(value, path) {
if (process.env.NODE_ENV !== 'production') {
if (identity) {
forbidNewOperator(this, Intersection);
}
path = path || [displayName];
assert(Intersection.is(value), function () { return 'Invalid value ' + assert.stringify(value) + ' supplied to ' + path.join('/'); });
}
return value;
}
Intersection.meta = {
kind: 'intersection',
types: types,
name: name,
identity: identity
};
Intersection.displayName = displayName;
Intersection.is = function (x) {
return types.every(function (type) {
return is(x, type);
});
};
Intersection.update = function (instance, patch) {
return Intersection(assert.update(instance, patch));
};
return Intersection;
}
intersection.getDefaultName = getDefaultName;
module.exports = intersection;