array-promise-filter
Advanced tools
Comparing version 0.1.0 to 1.0.0
35
index.js
@@ -0,31 +1,14 @@ | ||
var asyncFilter = require('array-async-filter'); | ||
var $Promise = Promise || require('es6-promise').Promise; | ||
module.exports = function (values, cb) { | ||
var args = arrayify(arguments, 2); | ||
var pending = values.length; | ||
var flagError = new Error('rejected'); | ||
return new Promise(function (resolve) { | ||
var res = []; | ||
values.forEach(function (v, i) { | ||
cb.apply(null, [v].concat(args)) | ||
.then(function (v) { | ||
res[i] = v; | ||
oneDone(); | ||
}, function () { | ||
res[i] = flagError; | ||
oneDone(); | ||
}); | ||
}); | ||
function oneDone() { | ||
if (--pending === 0) { | ||
resolve(res.filter(function (v) { | ||
return v !== flagError; | ||
})); | ||
} | ||
} | ||
return new $Promise(function (resolve, reject) { | ||
asyncFilter(values, cb, function (err, results) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
resolve(results); | ||
}); | ||
}); | ||
}; | ||
function arrayify(o, from, to) { | ||
return Array.prototype.slice.call(o, from, to); | ||
} | ||
{ | ||
"name": "array-promise-filter", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "filter an array through async callback", | ||
@@ -10,3 +10,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "tape test/*.js | faucet" | ||
"test": "eslint . && tap test/*.js" | ||
}, | ||
@@ -29,6 +29,9 @@ "repository": { | ||
"devDependencies": { | ||
"faucet": "0.0.1", | ||
"node-promisify": "^0.1.2", | ||
"tape": "^4.0.0" | ||
"eslint": "^1.5.1", | ||
"tap": "^1.4.1" | ||
}, | ||
"dependencies": { | ||
"array-async-filter": "^2.0.0", | ||
"es6-promise": "^3.0.2" | ||
} | ||
} |
107
README.md
# array-promise-filter | ||
filter an array through async callback | ||
Filter an array through async callback. | ||
The results can be accessed from the returned promise. | ||
## Usage | ||
## res = filter(values, fn) | ||
```javascript | ||
var filter = require('array-promise-filter'); | ||
var promisify = require('node-promisify'); | ||
var gt2 = promisify(function (n, m, cb) { | ||
``` | ||
### values | ||
Type: `Array` | ||
The array to be filtered. | ||
### fn | ||
Type: `function` | ||
`fn` should do one of the following. | ||
#### Respect synchronous signature | ||
`fn(val, index, arr)` | ||
```javascript | ||
var filter = require('..'); | ||
function isUpperCase(n) { | ||
return !n.split().some(function (c) { | ||
return c >= 'a'; | ||
}); | ||
} | ||
filter(['ab', 'CD', 'ef', 'GH'], isUpperCase) | ||
.then(function (res) { | ||
console.log(res); // ['CD', 'GH'] | ||
}) | ||
.catch(function (err) { | ||
console.log(err); | ||
}); | ||
``` | ||
#### Respect asynchronous signature | ||
`fn(val, index, arr, next)` | ||
`next(err, keep)` must be called when `fn` finishes. | ||
```javascript | ||
var filter = require('..'); | ||
function isUpperCase(n, i, arr, next) { | ||
process.nextTick(function () { | ||
try { | ||
next(null, !n.split().some(function (c) { | ||
return c >= 'a'; | ||
})); | ||
} catch (e) { | ||
next(e); | ||
} | ||
}); | ||
} | ||
filter(['ab', 'CD', 'ef', 'GH'], isUpperCase) | ||
.then(function (res) { | ||
console.log(res); // ['CD', 'GH'] | ||
}) | ||
.catch(function (err) { | ||
console.log(err); | ||
}); | ||
``` | ||
#### Return a promise | ||
```javascript | ||
var filter = require('..'); | ||
function isUpperCase(n) { | ||
return new Promise(function (rs, rj) { | ||
process.nextTick(function () { | ||
var err = n <= m ? new Error('not greater') : null; | ||
cb(err, n << 1); | ||
try { | ||
rs(!n.split().some(function (c) { | ||
return c >= 'a'; | ||
})); | ||
} catch (e) { | ||
rj(e); | ||
} | ||
}); | ||
}); | ||
filter([1,2,3,4,5,6], gt2, 3).then(function (res) { | ||
console.log(res); // [8,10,12] | ||
}); | ||
}); | ||
} | ||
filter(['ab', 'CD', 'ef', 'GH'], isUpperCase) | ||
.then(function (res) { | ||
console.log(res); // ['CD', 'GH'] | ||
}) | ||
.catch(function (err) { | ||
console.log(err); | ||
}); | ||
``` | ||
### filter(values, fnReturnPromise, arg1, arg2,...) | ||
### res | ||
* values: *Array* | ||
* fnReturnPromise: some async function which return a promise. The first argument given to this function is an element from `values`, and the rest are from `arg1`, `arg2`,... | ||
Type: `Promise` | ||
You can use tools like [node-promisify](https://www.npmjs.com/package/node-promisify) to make a callback-style async function to a promise-style async function. | ||
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
4705
2
5
0
106
2
12
+ Addedarray-async-filter@^2.0.0
+ Addedes6-promise@^3.0.2
+ Addedarray-async-filter@2.0.1(transitive)
+ Addedes6-promise@3.3.1(transitive)