Commit d80bd59b authored by Nicolas Biri's avatar Nicolas Biri

Add an type

parent 3d3703f8
......@@ -35,8 +35,11 @@ module.exports =
, Date: _.isDate
, Array: _.isArray
, Object: _.isObject
, InstanceOf: function(cls) {
return function InstanceOf(x) {return x.constructor === cls}
}
, Function: _.isFunction
, Range: function Range(min, max) {
, Range: function(min, max) {
const self = x => x >= min && x <= max
Object.assign(self, {typeName: 'Range', min, max})
return self
......
......@@ -67,6 +67,31 @@ describe('Range', done => {
})
describe('InstanceOf', done => {
it('accepts valid instance', done => {
function Foo(x) {this.x = x}
const r = new JSMF.InstanceOf(Foo)
r(new Foo(2)).should.be.true()
done()
})
it('refuses the class itself instance', done => {
function Foo(x) {this.x = x}
const r = new JSMF.InstanceOf(Foo)
r(Foo).should.be.false()
done()
})
it('refuses duck typing', done => {
function Foo(x) {this.x = x}
const r = new JSMF.InstanceOf(Foo)
r({x: 12}).should.be.false()
done()
})
})
describe('normalizeType', done => {
it ('transform Number to JSMF.Number', done => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment