Comparing version 0.0.3 to 0.0.4
var http = require('http'), | ||
Promise = require('../lib/pacta').Promise; | ||
var get = function (url) { | ||
var get = function (options) { | ||
var promise = new Promise(); | ||
http.get(url, function (res) { | ||
http.get(options, function (res) { | ||
var body = ''; | ||
@@ -22,4 +22,4 @@ | ||
var getJSON = function (url) { | ||
return get(url).map(JSON.parse); | ||
var getJSON = function (options) { | ||
return get(options).map(JSON.parse); | ||
}; | ||
@@ -26,0 +26,0 @@ |
@@ -107,2 +107,10 @@ var events = require('events'); | ||
Promise.prototype.reduce = function () { | ||
var args = [].slice.call(arguments); | ||
return this.map(function (x) { | ||
return x.reduce.apply(x, args); | ||
}); | ||
}; | ||
/* of :: a -> Promise a */ | ||
@@ -109,0 +117,0 @@ Promise.of = function (x) { |
@@ -7,3 +7,3 @@ { | ||
"keywords": ["promises", "monad", "functor"], | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"main": "./lib/pacta.js", | ||
@@ -10,0 +10,0 @@ "dependencies": {}, |
@@ -51,2 +51,5 @@ # pacta [![Build Status](https://travis-ci.org/mudge/pacta.png?branch=master)](https://travis-ci.org/mudge/pacta) | ||
results in `[1, [2, 3]]` rather than `[1, 2, 3]`); | ||
* [`Promise#reduce`](#promisereducef-initialvalue) to | ||
[reduce](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/Reduce) | ||
a list within a promise; | ||
* [`Promise#spread`](#promisespreadf) to map over a promise's value but, | ||
@@ -279,2 +282,18 @@ instead of receiving a single value, spread the promise's value across | ||
### `Promise#reduce(f[, initialValue])` | ||
```javascript | ||
var promise = Promise.of([1, 2, 3]); | ||
promise.reduce(function (acc, e) { | ||
return acc + e; | ||
}, 0); //=> Promise.of(6) | ||
``` | ||
On a promise containing an array, | ||
[reduce](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/Reduce) | ||
its value, returning a promise of the resulting value. This defers to the | ||
underlying signature of `reduce` taking a function `f` and an optional | ||
`initialValue`. | ||
### `Promise#spread(f)` | ||
@@ -281,0 +300,0 @@ |
@@ -357,2 +357,26 @@ var assert = require('assert'), | ||
}); | ||
describe('#reduce', function () { | ||
it('returns a new promise with the result', function (done) { | ||
var p = Promise.of([[1], [2], [3]]); | ||
p.reduce(function (acc, e) { | ||
return acc.concat(e); | ||
}).map(function (x) { | ||
assert.deepEqual([1, 2, 3], x); | ||
done(); | ||
}); | ||
}); | ||
it('takes an optional initial value', function (done) { | ||
var p = Promise.of([1, 2, 3]); | ||
p.reduce(function (acc, e) { | ||
return acc + e; | ||
}, 0).map(function (x) { | ||
assert.equal(6, x); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -359,0 +383,0 @@ |
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
30711
522
326