Comparing version 0.0.6 to 0.0.7
{ | ||
"name": "tcomb", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "JavaScript types and combinators", | ||
@@ -5,0 +5,0 @@ "main": "tcomb.js", |
@@ -159,3 +159,3 @@ # tcomb | ||
- `message` optional string useful for debugging | ||
- `values...` optional values formatted by `message` (visionmedia/debug style) | ||
- `values...` optional values formatted by `message` ([visionmedia/debug](https://github.com/visionmedia/debug) style) | ||
@@ -162,0 +162,0 @@ Example |
68
tcomb.js
@@ -131,2 +131,8 @@ // https://github.com/gcanti/tcomb | ||
function coerce(Type, values, mut) { | ||
return Type.meta.kind === 'struct' ? | ||
new Type(values, mut) : | ||
Type(values, mut); | ||
} | ||
// -------------------------------------------------------------- | ||
@@ -139,2 +145,3 @@ // primitives | ||
function Primitive(values) { | ||
assert(!(this instanceof Primitive), 'cannot use new with %s', name); | ||
assert(Primitive.is(values), 'bad %s', name); | ||
@@ -146,3 +153,4 @@ return values; | ||
kind: 'primitive', | ||
name: name | ||
name: name, | ||
ctor: false | ||
}; | ||
@@ -204,3 +212,3 @@ | ||
value = values[prop]; | ||
this[prop] = Type.is(value) ? value : new Type(value, mut); | ||
this[prop] = Type.is(value) ? value : coerce(Type, value, mut); | ||
} | ||
@@ -215,3 +223,4 @@ } | ||
props: props, | ||
name: name | ||
name: name, | ||
ctor: true | ||
}; | ||
@@ -250,5 +259,8 @@ | ||
function Union(values, mut) { | ||
assert(Func.is(Union.dispatch), 'in order to use the constructor you must implement %s.dispatch()', name); | ||
assert(Func.is(Union.dispatch), 'unimplemented %s.dispatch()', name); | ||
var Type = Union.dispatch(values); | ||
return new Type(values, mut); | ||
if (this instanceof Union) { | ||
assert(Type.meta.ctor, 'cannot use new with %s', name); | ||
} | ||
return coerce(Type, values, mut); | ||
} | ||
@@ -259,3 +271,4 @@ | ||
types: types, | ||
name: name | ||
name: name, | ||
ctor: types.every(function (type) { return type.meta.ctor; }) | ||
}; | ||
@@ -281,3 +294,4 @@ | ||
function Maybe(values, mut) { | ||
return Nil.is(values) ? null : new Type(values, mut); | ||
assert(!(this instanceof Maybe), 'cannot use new with %s', name); | ||
return Nil.is(values) ? null : coerce(Type, values, mut); | ||
} | ||
@@ -288,3 +302,4 @@ | ||
type: Type, | ||
name: name | ||
name: name, | ||
ctor: false | ||
}; | ||
@@ -309,2 +324,3 @@ | ||
function Enums(x) { | ||
assert(!(this instanceof Enums), 'cannot use new with %s', name); | ||
assert(Enums.is(x), 'bad %s', name); | ||
@@ -317,3 +333,4 @@ return x; | ||
map: map, | ||
name: name | ||
name: name, | ||
ctor: false | ||
}; | ||
@@ -346,3 +363,3 @@ | ||
var value = values[i]; | ||
arr.push(Type.is(value) ? value : new Type(value, mut)); | ||
arr.push(Type.is(value) ? value : coerce(Type, value, mut)); | ||
} | ||
@@ -356,3 +373,4 @@ | ||
types: types, | ||
name: name | ||
name: name, | ||
ctor: true | ||
}; | ||
@@ -369,3 +387,3 @@ | ||
var Type = types[index], | ||
value = Type.is(element) ? element : new Type(element, mut), | ||
value = Type.is(element) ? element : coerce(Type, element, mut), | ||
arr = update(instance, index, value); | ||
@@ -387,3 +405,6 @@ return freeze(arr, mut); | ||
function Subtype(values, mut) { | ||
var x = new Type(values, mut); | ||
if (this instanceof Subtype) { | ||
assert(Subtype.meta.ctor, 'cannot use new with %s', name); | ||
} | ||
var x = coerce(Type, values, mut); | ||
assert(predicate(x), 'bad ' + name); | ||
@@ -397,3 +418,4 @@ return x; | ||
predicate: predicate, | ||
name: name | ||
name: name, | ||
ctor: Type.ctor | ||
}; | ||
@@ -423,3 +445,3 @@ | ||
var value = values[i]; | ||
arr.push(Type.is(value) ? value : new Type(value, mut)); | ||
arr.push(Type.is(value) ? value : coerce(Type, value, mut)); | ||
} | ||
@@ -433,3 +455,4 @@ | ||
type: Type, | ||
name: name | ||
name: name, | ||
ctor: true | ||
}; | ||
@@ -442,3 +465,3 @@ | ||
List.append = function (instance, element, mut) { | ||
var value = Type.is(element) ? element : new Type(element, mut), | ||
var value = Type.is(element) ? element : coerce(Type, element, mut), | ||
arr = append(instance, value); | ||
@@ -449,3 +472,3 @@ return freeze(arr, mut); | ||
List.prepend = function (instance, element, mut) { | ||
var value = Type.is(element) ? element : new Type(element, mut), | ||
var value = Type.is(element) ? element : coerce(Type, element, mut), | ||
arr = prepend(instance, value); | ||
@@ -456,3 +479,3 @@ return freeze(arr, mut); | ||
List.update = function (instance, index, element, mut) { | ||
var value = Type.is(element) ? element : new Type(element, mut), | ||
var value = Type.is(element) ? element : coerce(Type, element, mut), | ||
arr = update(instance, index, value); | ||
@@ -485,8 +508,8 @@ return freeze(arr, mut); | ||
args = Arguments.is(args) ? args : new Arguments(args); | ||
args = Arguments.is(args) ? args : coerce(Arguments, args); | ||
var r = f.apply(this, new Arguments(args)); | ||
var r = f.apply(this, args); | ||
if (Return) { | ||
r = Return.is(r) ? r : new Return(r); | ||
r = Return.is(r) ? r : coerce(Return, r); | ||
} | ||
@@ -530,3 +553,2 @@ | ||
primitive: primitive, | ||
struct: struct, | ||
@@ -533,0 +555,0 @@ enums: enums, |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var assert = require('assert'); | ||
@@ -36,52 +37,6 @@ var t = require('../tcomb'); | ||
// enums | ||
var Direction = enums({ | ||
North: 0, | ||
East: 1, | ||
South: 2, | ||
West: 3 | ||
}); | ||
// union | ||
var Circle = struct({ | ||
center: Point, | ||
radius: Num | ||
}, 'Circle'); | ||
var Rectangle = struct({ | ||
a: Point, | ||
b: Point | ||
}, 'Rectangle'); | ||
var Shape = union([Circle, Rectangle]); | ||
Shape.dispatch = function (values) { | ||
assert(Obj.is(values)); | ||
return values.hasOwnProperty('center') ? | ||
Circle : | ||
Rectangle; | ||
}; | ||
// maybe | ||
var Radio = maybe(Str); | ||
// tuple | ||
var Area = tuple([Num, Num]); | ||
// list | ||
var Path = list(Point); | ||
// subtype | ||
var Positive = subtype(Num, function (n) { | ||
return n >= 0; | ||
}); | ||
// func | ||
var sum = func(tuple([Num, Num]), function (a, b) { | ||
return a + b; | ||
}, Num); | ||
// | ||
// | ||
describe('print', function(){ | ||
@@ -154,3 +109,3 @@ it('should format the message', function() { | ||
describe('Nil', function(){ | ||
describe('#is(x)', function(){ | ||
describe('#is(x)', function () { | ||
it('should return true when x is null or undefined', function() { | ||
@@ -355,2 +310,10 @@ ok(Nil.is(null)); | ||
describe('enums', function(){ | ||
var Direction = enums({ | ||
North: 0, | ||
East: 1, | ||
South: 2, | ||
West: 3 | ||
}); | ||
it('should have a default meaningful meta.name', function() { | ||
@@ -374,2 +337,15 @@ ok(Direction.meta.name === 'enums()'); | ||
describe('union', function(){ | ||
var Circle = struct({ | ||
center: Point, | ||
radius: Num | ||
}, 'Circle'); | ||
var Rectangle = struct({ | ||
a: Point, | ||
b: Point | ||
}, 'Rectangle'); | ||
var Shape = union([Circle, Rectangle]); | ||
it('should have a default meaningful meta.name', function() { | ||
@@ -386,7 +362,26 @@ ok(Shape.meta.name === 'union(Circle, Rectangle)'); | ||
it('should throw when dispatch() is not implemented', function() { | ||
throws(function () { | ||
new Shape({center: {x: 0, y: 0}, radius: 10}); | ||
}, function (err) { | ||
if ( (err instanceof Error) && err.message === 'unimplemented union(Circle, Rectangle).dispatch()' ) { | ||
return true; | ||
} | ||
}); | ||
}); | ||
it('should be used to buils instances', function() { | ||
it('should build instances when dispatch() is implemented', function() { | ||
Shape.dispatch = function (values) { | ||
assert(Obj.is(values)); | ||
return values.hasOwnProperty('center') ? | ||
Circle : | ||
Rectangle; | ||
}; | ||
ok(Circle.is(new Shape({center: {x: 0, y: 0}, radius: 10}))); | ||
}); | ||
it('should have meta.ctor = true if all types are new-ables', function() { | ||
ok(Shape.meta.ctor); | ||
}); | ||
it('should have meta.ctor = true if at least one type is not new-able', function() { | ||
var U = union([Str, Point]); | ||
ko(U.meta.ctor); | ||
}); | ||
}); | ||
@@ -400,2 +395,5 @@ }); | ||
describe('maybe', function(){ | ||
var Radio = maybe(Str); | ||
it('should have a default meaningful meta.name', function() { | ||
@@ -410,3 +408,6 @@ ok(Radio.meta.name === 'maybe(Str)'); | ||
describe('tuple', function(){ | ||
describe('tuple', function () { | ||
var Area = tuple([Num, Num]); | ||
it('should have a default meaningful meta.name', function() { | ||
@@ -432,2 +433,5 @@ ok(Area.meta.name === 'tuple(Num, Num)'); | ||
describe('list', function(){ | ||
var Path = list(Point); | ||
it('should have a default meaningful meta.name', function() { | ||
@@ -443,5 +447,19 @@ ok(Path.meta.name === 'list(Point)'); | ||
describe('subtype', function(){ | ||
// subtype | ||
var Positive = subtype(Num, function (n) { | ||
return n >= 0; | ||
}); | ||
it('should have a default meaningful meta.name', function() { | ||
ok(Positive.meta.name === 'subtype(Num)'); | ||
}); | ||
describe('#is(x)', function(){ | ||
it('should return true when x is a subtype', function() { | ||
ok(Positive.is(1)); | ||
}); | ||
it('should return false when x is not a subtype', function() { | ||
ko(Positive.is(-1)); | ||
}); | ||
}); | ||
}); | ||
@@ -454,2 +472,7 @@ | ||
describe('func', function(){ | ||
var sum = func(tuple([Num, Num]), function (a, b) { | ||
return a + b; | ||
}, Num); | ||
describe('#is(x)', function(){ | ||
@@ -456,0 +479,0 @@ it('should return true when x is the func', function() { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40278
896