Skip to content

Commit

Permalink
Style tweaks and test case fix
Browse files Browse the repository at this point in the history
  • Loading branch information
paldepind committed Oct 1, 2016
1 parent e34eb86 commit eef0da2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
20 changes: 10 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('union type', function() {
var Age = Type({Age: [Number]});
assert.throws(function() {
Age.Age('12');
}, /bad value/);
}, /wrong value/);
});
it('throws on too many arguments', function() {
var Foo = Type({Foo: [Number, Number]});
Expand All @@ -64,23 +64,23 @@ describe('union type', function() {
var Exists = Type({Exists: [Boolean]});
assert.throws(function() {
Exists.Exists('12');
}, /bad value/);
}, /wrong value/);
});
});
it('array of types', function() {
var Point = Type({Point: [Number, Number]});
var Shape = Type({Shape: [Type.ListOf(Point)]}).Shape;
assert.throws(function(){
Shape([1,Point.Point(1,2),3]);
assert.throws(function() {
Shape([1, Point.Point(1,2), 3]);
}, /wrong value 1 passed to location first in List/);
assert.throws(function(){
Shape([Point.Point(1,2), Point.Point('3',1)]);
}, /wrong value 3 passed to location first in Point/);
Shape([Point.Point(1,2), Point.Point(1,2)]);
assert.throws(function() {
Shape([Point.Point(1, 2), Point.Point('3', 1)]);
}, /wrong value/);
Shape([Point.Point(1, 2), Point.Point(1, 2)]);
Shape([]);
assert.throws(function(){
assert.throws(function() {
Shape("not a List")
}, /wrong value not a List passed to location first in List/);
}, /wrong value/);
});
it('nest types', function() {
var Point = Type({Point: [isNumber, isNumber]});
Expand Down
14 changes: 7 additions & 7 deletions union-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var validate = function(group, validators, name, args) {
(validator.prototype === undefined || !validator.prototype.isPrototypeOf(v)) &&
(typeof validator !== 'function' || !validator(v))) {
var strVal = typeof v === 'string' ? "'" + v + "'" : v; // put the value in quotes if it's a string
throw new TypeError('bad value ' + strVal + ' passed as ' + numToStr[i] + ' argument to constructor ' + name);
throw new TypeError('wrong value ' + strVal + ' passed as ' + numToStr[i] + ' argument to constructor ' + name);
}
}
};
Expand Down Expand Up @@ -127,8 +127,8 @@ function Type(desc) {

obj.prototype = {};
obj.prototype[Symbol ? Symbol.iterator : '@@iterator'] = createIterator;
obj.prototype.case = function (cases) { return obj.case(cases, this); }
obj.prototype.caseOn = function (cases) { return obj.caseOn(cases, this); }
obj.prototype.case = function (cases) { return obj.case(cases, this); };
obj.prototype.caseOn = function (cases) { return obj.caseOn(cases, this); };

for (key in desc) {
res = constructor(obj, key, desc[key]);
Expand All @@ -143,17 +143,17 @@ Type.ListOf = function (T) {
var innerType = Type({T: [T]}).T;
var validate = List.case({
List: function (array) {
try{
try {
for(var n = 0; n < array.length; n++) {
innerType(array[n])
innerType(array[n]);
}
} catch (e) {
throw TypeError('wrong value '+array[n]+' passed to location '+numToStr[n]+' in List')
throw new TypeError('wrong value '+ array[n] + ' passed to location ' + numToStr[n] + ' in List');
}
return true;
}
});
return compose(validate, List.List);
}
};

module.exports = Type;

0 comments on commit eef0da2

Please sign in to comment.