Skip to content

Commit

Permalink
Support series
Browse files Browse the repository at this point in the history
  • Loading branch information
zoubin committed Dec 10, 2015
1 parent 78f163f commit af80f59
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 3 deletions.
42 changes: 41 additions & 1 deletion README.md
Expand Up @@ -23,6 +23,7 @@ All methods return a promise.

- [filter](#filter)
- [map](#map)
- [series](#series)
- [forEach](#foreach)
- [reduce](#reduce)
- [AsyncArray](#asyncarray)
Expand Down Expand Up @@ -60,7 +61,7 @@ filter(

Signature: `map(arr, fn, context)`

`fn` is called with elements in parallel.
`fn` is called with each element in parallel.

```javascript
map(
Expand All @@ -79,6 +80,38 @@ map(

```

### series
This method works like `map`,
except that `fn` is called with each element in sequence rather than in parallel.

Signature: `map(arr, fn, context)`


```javascript
var n = 1
series(
[1, 2, 3, 4],
function (v, i, arr, next) {
var delay = rand()
console.log('i:', i, 'delay:', delay)
setTimeout(function() {
next(null, v << n++)
}, delay)
}
)
// i: 0 delay: 10
// i: 1 delay: 50
// i: 2 delay: 0
// i: 3 delay: 80
// [2, 8, 24, 64]
.then(log)

function rand() {
return Math.floor(Math.random() * 10) * 10
}

```

### forEach

`fn` is called with elements in sequence.
Expand Down Expand Up @@ -125,6 +158,13 @@ reduce(
### AsyncArray
Signature: `AsyncArray(arr)`

Methods:
* `map`
* `series`
* `filter`
* `forEach`
* `reduce`

```javascript
var origin = AsyncArray([1, 2, 3, 4, 5, 6])
var odd = origin.filter(isOdd)
Expand Down
21 changes: 21 additions & 0 deletions example/series.js
@@ -0,0 +1,21 @@
var series = require('..').series

var log = console.log.bind(console)

var n = 1
series(
[1, 2, 3, 4],
function (v, i, arr, next) {
var delay = rand()
console.log('i:', i, 'delay:', delay)
setTimeout(function() {
next(null, v << n++)
}, delay)
}
)
// [2, 8, 24, 64]
.then(log)

function rand() {
return Math.floor(Math.random() * 10) * 10
}
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -3,5 +3,6 @@ exports.forEach = require('./lib/forEach')
exports.map = require('./lib/map')
exports.filter = require('./lib/filter')
exports.reduce = require('./lib/reduce')
exports.series = require('./lib/series')
exports.Array = require('./lib/async-array')

3 changes: 2 additions & 1 deletion lib/async-array.js
@@ -1,5 +1,6 @@
var asyncMethods = {
map: require('./map'),
series: require('./series'),
filter: require('./filter'),
reduce: require('./reduce'),
forEach: require('./forEach'),
Expand All @@ -12,7 +13,7 @@ function AsyncArray(arr) {
this.result = Promise.resolve(arr || [])
}

['map', 'filter', 'forEach', 'reduce']
['map', 'filter', 'forEach', 'reduce', 'series']
.forEach(function (fnName) {
AsyncArray.prototype[fnName] = function () {
var args = slice(arguments)
Expand Down
18 changes: 18 additions & 0 deletions lib/series.js
@@ -0,0 +1,18 @@
var Runner = require('callback-sequence').Runner
var runner = Runner({
run: { stream: false },
})

module.exports = function (arr, fn, ctx) {
if (arguments.length < 3) {
ctx = arr
}

return runner.series.apply(runner, arr.map(function (v, i) {
return fn.bind(ctx, v, i, arr)
})).then(function (results) {
return [].concat.apply([], results)
})
}


2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -28,7 +28,7 @@
},
"homepage": "https://github.com/zoubin/async-array-methods#readme",
"dependencies": {
"callback-sequence": "^3.1.0"
"callback-sequence": "^3.2.0"
},
"devDependencies": {
"eslint": "^1.5.1",
Expand Down
14 changes: 14 additions & 0 deletions test/array.js
Expand Up @@ -22,6 +22,20 @@ test('map', function(t) {
})
})

test('series', function(t) {
var origin = AsyncArray([1, 3, 5])
var delays = [10, 0, 20]
return origin.series(function (v, i, arr, next) {
setTimeout(function() {
next(null, v + 1)
}, delays[i])
})
.then(function (res) {
t.same(res, [2, 4, 6])
})

})

test('reduce', function(t) {
var origin = AsyncArray([1, 3, 5])
return origin.reduce(function (a, b) {
Expand Down
34 changes: 34 additions & 0 deletions test/series.js
@@ -0,0 +1,34 @@
var test = require('tap').test
var series = require('../lib/series')

test('results', function(t) {
t.plan(2)

var res = []
series([30, 20, 10], function (v, i, arr, next) {
setTimeout(function() {
res.push(v)
next(null, i)
}, v)
}).then(function (results) {
t.same(results, [0, 1, 2])
t.same(res, [30, 20, 10])
})
})

test('context', function(t) {
t.plan(2)

var res = []
series([30, 20, 10], function (v, i, arr, next) {
var self = this
setTimeout(function() {
res.push(v)
next(null, i + self.n)
}, v)
}, { n: 1 }).then(function (results) {
t.same(results, [1, 2, 3])
t.same(res, [30, 20, 10])
})
})

0 comments on commit af80f59

Please sign in to comment.