Skip to content

Commit

Permalink
add predicates is[Something] for union type
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouyang committed Nov 16, 2016
1 parent 6c3c3c9 commit 3eb4315
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ describe('union type', function() {
assert.equal(p[1], undefined);
});
});
describe('predicate', function() {
it('can use predicate to check type', function() {
var Maybe = Type({Just: [T], Nothing: []});
var OtherMaybe = Type({Just: [T]});
var just1 = Maybe.Just(1)
assert.equal(Maybe.isJust(just1), true);
assert.equal(Maybe.isNothing(just1), false);
assert.equal(Maybe.isNothing(just1), false);
assert.equal(OtherMaybe.isJust(just1), false);
})
});
describe('type methods', function() {
it('can add instance methods', function() {
var Maybe = Type({Just: [T], Nothing: []});
Expand Down
3 changes: 3 additions & 0 deletions union-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ function constructor(group, name, fields) {
return val;
}
group[name] = curryN(keys.length, construct);
group['is' + name] = function(value) {
return group.prototype.isPrototypeOf(value) && name === value._name
}
if (keys !== undefined) {
group[name+'Of'] = function(obj) {
return construct.apply(undefined, extractValues(keys, obj));
Expand Down

0 comments on commit 3eb4315

Please sign in to comment.