Skip to content

Commit

Permalink
upgrade to bluebird
Browse files Browse the repository at this point in the history
  • Loading branch information
junosuarez committed Dec 6, 2013
1 parent 504cf69 commit cb7d513
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 51 deletions.
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Q = require('q')
var Promise = require('bluebird')
var slice = Array.prototype.slice

function isPromise(p) {
Expand All @@ -7,16 +7,16 @@ function isPromise(p) {

function pcall (p) {
var t = typeof p
if (t === 'boolean') return Q.resolve(p)
if (t === 'boolean') return Promise.cast(p)
if (t === 'function') return pcall(p())
if (isPromise(p)) return p
return Q.resolve(Boolean(p))
return Promise.resolve(Boolean(p))
}

function or () {
var args = arguments;
var term = args[0]
if (!term) return Q.reject(new Error('No terms'));
if (!term) return Promise.reject(new Error('No terms'));
if (args.length === 1) {
return pcall(term).then(Boolean)
}
Expand All @@ -31,7 +31,7 @@ function or () {
function and () {
var args = arguments;
var term = args[0]
if (!term) return Q.reject(new Error('No terms'))
if (!term) return Promise.reject(new Error('No terms'))
if (args.length === 1) {
return pcall(term).then(Boolean)
}
Expand All @@ -47,36 +47,36 @@ function and () {
function some () {
var args = arguments;
var term = args[0]
if (!term) return Q.reject(new Error('No terms'))
if (!term) return Promise.reject(new Error('No terms'))
if (args.length === 1) {
return pcall(term).then(Boolean)
}

var promises = slice.call(arguments).map(pcall)

return Q.all(promises).then(function (results) {
return Promise.all(promises).then(function (results) {
return results.some(Boolean)
})
}

function every () {
var args = arguments;
var term = args[0]
if (!term) return Q.reject(new Error('No terms'))
if (!term) return Promise.reject(new Error('No terms'))
if (args.length === 1) {
return pcall(term).then(Boolean)
}

var promises = slice.call(arguments).map(pcall)

return Q.all(promises).then(function (results) {
return Promise.all(promises).then(function (results) {
return results.every(Boolean)
})
}


function not(term) {
if (!term) return Q.reject(new Error('No term'))
if (!term) return Promise.reject(new Error('No term'))
return pcall(term).then(function (x) { return !x })
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "shortcircuit",
"version": "0.5.2",
"version": "0.5.3",
"description": "asynchronous short circuit evaluation",
"keywords": [
"boolean",
Expand Down Expand Up @@ -33,6 +33,6 @@
"ski": "~1.0.0"
},
"dependencies": {
"q": "~0.9.2"
"bluebird": "~0.11.5-0"
}
}
78 changes: 39 additions & 39 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should()
chai.use(require('chai-interface'))
var sinon = require('sinon')
chai.use(require('sinon-chai'))
var Q = require('q')
var Promise = require('bluebird')
var K = require('ski/k')

describe('shortcircuit-promise', function () {
Expand All @@ -29,27 +29,27 @@ describe('shortcircuit-promise', function () {
})

it('resolves false if any of the terms is false', function (done) {
var t1 = K(Q.resolve(true))
var t2 = K(Q.resolve(false))
var t1 = K(Promise.resolve(true))
var t2 = K(Promise.resolve(false))

and(t1, t2).then(function (val) {
val.should.equal(false)
}).then(done, done)
})

it('resolves true if all of the terms are true', function (done) {
var t1 = K(Q.resolve(true))
var t2 = K(Q.resolve(true))
var t1 = K(Promise.resolve(true))
var t2 = K(Promise.resolve(true))

and(t1, t2).then(function (val) {
val.should.equal(true)
}).then(done, done)
})

it('executes terms in serial', function (done) {
var t1 = sinon.stub().returns(Q.resolve(true))
var t2 = sinon.stub().returns(Q.resolve(true))
var t3 = sinon.stub().returns(Q.resolve(true))
var t1 = sinon.stub().returns(Promise.resolve(true))
var t2 = sinon.stub().returns(Promise.resolve(true))
var t3 = sinon.stub().returns(Promise.resolve(true))

and(t1, t2, t3).then(function () {
t1.should.have.been.called
Expand All @@ -59,9 +59,9 @@ describe('shortcircuit-promise', function () {
})

it('only executes terms necessary', function (done) {
var t1 = sinon.stub().returns(Q.resolve(true))
var t2 = sinon.stub().returns(Q.resolve(false))
var t3 = sinon.stub().returns(Q.resolve(true))
var t1 = sinon.stub().returns(Promise.resolve(true))
var t2 = sinon.stub().returns(Promise.resolve(false))
var t3 = sinon.stub().returns(Promise.resolve(true))

and(t1, t2, t3).then(function () {
t1.should.have.been.called
Expand All @@ -82,27 +82,27 @@ describe('shortcircuit-promise', function () {
})

it('resolves true if any of the terms is true', function (done) {
var t1 = K(Q.resolve(false))
var t2 = K(Q.resolve(true))
var t1 = K(Promise.resolve(false))
var t2 = K(Promise.resolve(true))

or(t1, t2).then(function (val) {
val.should.equal(true)
}).then(done, done)
})

it('resolves false if all of the terms are false', function (done) {
var t1 = K(Q.resolve(false))
var t2 = K(Q.resolve(false))
var t1 = K(Promise.resolve(false))
var t2 = K(Promise.resolve(false))

or(t1, t2).then(function (val) {
val.should.equal(false)
}).then(done, done)
})

it('executes terms in serial', function (done) {
var t1 = sinon.stub().returns(Q.resolve(false))
var t2 = sinon.stub().returns(Q.resolve(false))
var t3 = sinon.stub().returns(Q.resolve(false))
var t1 = sinon.stub().returns(Promise.resolve(false))
var t2 = sinon.stub().returns(Promise.resolve(false))
var t3 = sinon.stub().returns(Promise.resolve(false))

or(t1, t2, t3).then(function () {
t1.should.have.been.called
Expand All @@ -112,9 +112,9 @@ describe('shortcircuit-promise', function () {
})

it('only executes terms necessary', function (done) {
var t1 = sinon.stub().returns(Q.resolve(false))
var t2 = sinon.stub().returns(Q.resolve(true))
var t3 = sinon.stub().returns(Q.resolve(false))
var t1 = sinon.stub().returns(Promise.resolve(false))
var t2 = sinon.stub().returns(Promise.resolve(true))
var t3 = sinon.stub().returns(Promise.resolve(false))

or(t1, t2, t3).then(function () {
t1.should.have.been.called
Expand All @@ -125,8 +125,8 @@ describe('shortcircuit-promise', function () {

it('is rejected if any of the executed terms is rejected', function (done) {
var err = new Error('foo')
var t1 = sinon.stub().returns(Q.resolve(false))
var t2 = sinon.stub().returns(Q.reject(err))
var t1 = sinon.stub().returns(Promise.resolve(false))
var t2 = sinon.stub().returns(Promise.reject(err))

or(t1, t2).then(function () {
done(new Error('should not resolve'))
Expand All @@ -145,11 +145,11 @@ describe('shortcircuit-promise', function () {
})

it('returns true when the term is false', function (done) {
not(Q.resolve(false)).then(function (val) { val.should.equal(true)}).then(done, done)
not(Promise.resolve(false)).then(function (val) { val.should.equal(true)}).then(done, done)
})

it('returns false when the term is true', function (done) {
not(Q.resolve(true)).then(function (val) { val.should.equal(false)}).then(done, done)
not(Promise.resolve(true)).then(function (val) { val.should.equal(false)}).then(done, done)
})
})

Expand All @@ -163,27 +163,27 @@ describe('shortcircuit-promise', function () {
})

it('resolves true if any of the terms is true', function (done) {
var t1 = K(Q.resolve(true))
var t2 = K(Q.resolve(false))
var t1 = K(Promise.resolve(true))
var t2 = K(Promise.resolve(false))

some(t1, t2).then(function (val) {
val.should.equal(true)
}).then(done, done)
})

it('resolves false if all of the terms are false', function (done) {
var t1 = K(Q.resolve(false))
var t2 = K(Q.resolve(false))
var t1 = K(Promise.resolve(false))
var t2 = K(Promise.resolve(false))

some(t1, t2).then(function (val) {
val.should.equal(false)
}).then(done, done)
})

it('executes all terms in parallel', function (done) {
var t1 = sinon.stub().returns(Q.resolve(true))
var t2 = sinon.stub().returns(Q.resolve(false))
var t3 = sinon.stub().returns(Q.resolve(true))
var t1 = sinon.stub().returns(Promise.resolve(true))
var t2 = sinon.stub().returns(Promise.resolve(false))
var t3 = sinon.stub().returns(Promise.resolve(true))

some(t1, t2, t3).then(function () {
t1.should.have.been.called
Expand All @@ -204,27 +204,27 @@ describe('shortcircuit-promise', function () {
})

it('resolves false if any of the terms is false', function (done) {
var t1 = K(Q.resolve(true))
var t2 = K(Q.resolve(false))
var t1 = K(Promise.resolve(true))
var t2 = K(Promise.resolve(false))

every(t1, t2).then(function (val) {
val.should.equal(false)
}).then(done, done)
})

it('resolves true if all of the terms are true', function (done) {
var t1 = K(Q.resolve(true))
var t2 = K(Q.resolve(true))
var t1 = K(Promise.resolve(true))
var t2 = K(Promise.resolve(true))

every(t1, t2).then(function (val) {
val.should.equal(true)
}).then(done, done)
})

it('executes all terms in parallel', function (done) {
var t1 = sinon.stub().returns(Q.resolve(true))
var t2 = sinon.stub().returns(Q.resolve(false))
var t3 = sinon.stub().returns(Q.resolve(true))
var t1 = sinon.stub().returns(Promise.resolve(true))
var t2 = sinon.stub().returns(Promise.resolve(false))
var t3 = sinon.stub().returns(Promise.resolve(true))

every(t1, t2, t3).then(function () {
t1.should.have.been.called
Expand Down

0 comments on commit cb7d513

Please sign in to comment.