Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dalton
committed
Jun 7, 2016
1 parent
45dbe99
commit b75897a
Showing
6 changed files
with
103 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,74 @@ | ||
var util = require('util'); | ||
var util = require('util'); | ||
var Promise = require('./index'); | ||
|
||
exports.pif = pif; | ||
exports.when = when; | ||
exports.unless = unless; | ||
exports.and = and; | ||
exports.pif = pif; | ||
exports.when = when; | ||
exports.unless = unless; | ||
exports.and = and; | ||
exports.propsWithErrors = propsWithErrors; | ||
|
||
function identity(a) { | ||
return a; | ||
return a; | ||
} | ||
|
||
function pif(test, consequent, alternate) { | ||
return function(value) { | ||
return test(value) ? consequent(value) : alternate(value); | ||
}; | ||
return function(value) { | ||
return test(value) ? consequent(value) : alternate(value); | ||
}; | ||
} | ||
|
||
function when(test, f) { | ||
return pif(test, f, identity); | ||
return pif(test, f, identity); | ||
} | ||
|
||
function unless(test, f) { | ||
return pif(test, identity, f); | ||
return pif(test, identity, f); | ||
} | ||
|
||
function and(fn) { | ||
return function(arg) { | ||
if (!arg || !util.isArray(arg) || arg.__ff_and_sig !== true) { | ||
arg = [arg]; | ||
|
||
Object.defineProperty(arg, '__ff_and_sig', { | ||
configurable: false, | ||
writable: false, | ||
enumerable: false, | ||
value: true | ||
}); | ||
} | ||
|
||
return Promise.resolve(fn.apply(this, arg)) | ||
.then(function(result) { | ||
arg.push(result); | ||
|
||
return arg; | ||
}); | ||
}; | ||
return function(arg) { | ||
if (!arg || !util.isArray(arg) || arg.__ff_and_sig !== true) { | ||
arg = [arg]; | ||
|
||
Object.defineProperty(arg, '__ff_and_sig', { | ||
configurable: false, | ||
writable: false, | ||
enumerable: false, | ||
value: true | ||
}); | ||
} | ||
|
||
return Promise.resolve(fn.apply(this, arg)) | ||
.then(function(result) { | ||
arg.push(result); | ||
|
||
return arg; | ||
}); | ||
}; | ||
} | ||
|
||
function propsWithErrors() { | ||
return function(_promises){ | ||
var errors = new Promise.AggregateError(); | ||
var safePromises = {}; | ||
|
||
return Promise.resolve(_promises).then(function(promises){ | ||
for(var key in promises){ | ||
safePromises[key] = Promise.resolve(promises[key]).catch(pushError); | ||
} | ||
|
||
return Promise.props(safePromises).then(function(result){ | ||
if(errors.length > 0){ | ||
throw errors; | ||
} else { | ||
return result; | ||
} | ||
}) | ||
}) | ||
|
||
function pushError(err){ | ||
errors.push(err); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var chai = require('chai'); | ||
var Promise = require('../lib'); | ||
var expect = chai.expect; | ||
|
||
chai.use(require('chai-as-promised')); | ||
|
||
describe('propsWithErrors', function() { | ||
it('should behave like props when no errors', function() { | ||
var promise = Promise.propsWithErrors({ | ||
first: Promise.resolve('1st'), | ||
second: Promise.resolve('2nd') | ||
}); | ||
|
||
return expect(promise).to.eventually.eql({first: '1st', second: '2nd'}); | ||
}); | ||
|
||
it('should throw aggregareError', function() { | ||
var promise = Promise.propsWithErrors({ | ||
pass: Promise.resolve('pass'), | ||
explode: Promise.reject('exploded'), | ||
throws: Promise.reject('threw') | ||
}); | ||
|
||
return expect(promise).to.be.rejectedWith(Promise.AggregateError); | ||
}); | ||
}); | ||
|