Skip to content

Commit

Permalink
filter, map, reduce, chain
Browse files Browse the repository at this point in the history
  • Loading branch information
zoubin committed Sep 27, 2015
1 parent 0cd3429 commit 8ecae6c
Show file tree
Hide file tree
Showing 14 changed files with 709 additions and 1 deletion.
82 changes: 82 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
ecmaFeatures:
modules: true

env:
browser: true
node: true
es6: true

rules:
comma-dangle: [2, "always-multiline"]
no-dupe-args: 2
no-dupe-keys: 2
no-duplicate-case: 2
no-empty-character-class: 2
no-ex-assign: 2
no-extra-boolean-cast: 2
no-extra-parens: 2
no-extra-semi: 2
no-func-assign: 2
no-inner-declarations: 2
no-invalid-regexp: 2
no-irregular-whitespace: 2
no-negated-in-lhs: 2
no-obj-calls: 2
no-regex-spaces: 2
no-sparse-arrays: 2
no-unreachable: 2
use-isnan: 2
valid-typeof: 2
no-unexpected-multiline: 2
no-cond-assign: 2
no-constant-condition: 2
no-control-regex: 2
no-debugger: 2
# code style
consistent-return: 2
curly: 2
default-case: 2
dot-notation: 2
dot-location: [2, "property"]
eqeqeq: 2
no-else-return: 2
no-lone-blocks: 2
no-loop-func: 2
no-multi-spaces: 2
no-multi-str: 2
no-proto: 2
no-redeclare: 2
no-return-assign: 2
no-sequences: 2
no-throw-literal: 2
no-unused-expressions: 2
no-void: 2
no-warning-comments: [2, { "terms": ["todo", "fixme", "xxx"], "location": "start" }]
no-with: 2
radix: 2
no-delete-var: 2
no-shadow-restricted-names: 2
no-shadow: 2
no-undef: 2
no-unused-vars: 2
brace-style: [2, "1tbs", { "allowSingleLine": true }]
comma-spacing: 2
comma-style: 2
indent: [2, 2]
key-spacing: 2
max-nested-callbacks: [2, 3]
no-lonely-if: 2
no-mixed-spaces-and-tabs: 2
no-nested-ternary: 2
no-spaced-func: 2
no-trailing-spaces: 2
one-var: [2, "never"]
operator-linebreak: 2
quote-props: [2, "as-needed"]
quotes: [2, "single", "avoid-escape"]
semi: [2, "always"]
space-after-keywords: 2
space-before-blocks: 2
space-infix-ops: 2
space-return-throw-case: 2
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules/
.DS_Store
npm-debug.log
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.gitignore
/test/
/example/
npm-debug.log
138 changes: 137 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,138 @@
# async-array-methods
Async methods to operate on collections
Async methods to operate on collections.

## Usage

```javascript
var methods = require('async-array-methods');
```

Methods:

- [filter](#filter)
- [map](#map)
- [reduce](#reduce)
- [chain](#chain)

## Callbacks

Callbacks can be made asynchronous by returning a promise,
or appending a function argument,
which will be called when the callback finishes.

Otherwise, the callback is treated as synchronous.

## filter

Signature: `filter(arr, fn, done)`

```javascript
filter(
[1, 2, 3, 4],
function (v) {
return new Promise(function (rs) {
process.nextTick(function () {
rs(v % 2);
});
});
},
function (err, results) {
console.log('promise:', results);
}
);
```

## map

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

```javascript
map(
[1, 2, 3, 4],
function (v, i, a, next) {
process.nextTick(function () {
next(null, v << 2);
});
},
function (err, results) {
console.log('async:', results);
}
);

```

## reduce

Signature: `reduce(arr, fn, initial, done)`

```javascript
reduce(
[1, 2, 3, 4],
function (a, b, i, arr, next) {
process.nextTick(function () {
next(null, a + b);
});
},
function (err, results) {
console.log('async:', results);
}
);

```

## chain

Signature: `chain(arr, ...callbacks, done)`

```javascript
chain(
[1, 2, 3, 4],
function (res) {
return res.map(function (r) {
return ++r;
});
},
['filter', odd],
function (res, next) {
process.nextTick(function () {
next(null, res.map(function (r) {
return ++r;
}));
});
},
['map', plusplus],
function (res) {
return new Promise(function (rs) {
process.nextTick(function () {
rs(res.map(function (r) {
return ++r;
}));
});
});
},
['reduce', sum, 10],
function (err, res) {
console.log(err, res);
}
);

function odd(v) {
return v % 2;
}

function plusplus(v, i, a, next) {
process.nextTick(function () {
next(null, ++v);
});
}

function sum(a, b) {
return new Promise(function (rs) {
process.nextTick(function () {
rs(a + b);
});
});
}

```

51 changes: 51 additions & 0 deletions example/chain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var chain = require('..').chain;

chain(
[1, 2, 3, 4],
function (res) {
return res.map(function (r) {
return ++r;
});
},
['filter', odd],
function (res, next) {
process.nextTick(function () {
next(null, res.map(function (r) {
return ++r;
}));
});
},
['map', plusplus],
function (res) {
return new Promise(function (rs) {
process.nextTick(function () {
rs(res.map(function (r) {
return ++r;
}));
});
});
},
['reduce', sum, 10],
function (err, res) {
console.log(err, res);
}
);

function odd(v) {
return v % 2;
}

function plusplus(v, i, a, next) {
process.nextTick(function () {
next(null, ++v);
});
}

function sum(a, b) {
return new Promise(function (rs) {
process.nextTick(function () {
rs(a + b);
});
});
}

38 changes: 38 additions & 0 deletions example/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var filter = require('..').filter;

filter(
[1, 2, 3, 4],
function (v) {
return v % 2;
},
function (err, results) {
console.log('sync:', results);
}
);

filter(
[1, 2, 3, 4],
function (v, i, a, next) {
process.nextTick(function () {
next(null, v % 2);
});
},
function (err, results) {
console.log('async:', results);
}
);

filter(
[1, 2, 3, 4],
function (v) {
return new Promise(function (rs) {
process.nextTick(function () {
rs(v % 2);
});
});
},
function (err, results) {
console.log('promise:', results);
}
);

38 changes: 38 additions & 0 deletions example/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var map = require('..').map;

map(
[1, 2, 3, 4],
function (v) {
return v << 2;
},
function (err, results) {
console.log('sync:', results);
}
);

map(
[1, 2, 3, 4],
function (v, i, a, next) {
process.nextTick(function () {
next(null, v << 2);
});
},
function (err, results) {
console.log('async:', results);
}
);

map(
[1, 2, 3, 4],
function (v) {
return new Promise(function (rs) {
process.nextTick(function () {
rs(v << 2);
});
});
},
function (err, results) {
console.log('promise:', results);
}
);

0 comments on commit 8ecae6c

Please sign in to comment.