async-array-reduce
Advanced tools
Comparing version 0.2.1 to 1.0.0
41
index.js
/*! | ||
* async-array-reduce <https://github.com/jonschlinkert/async-array-reduce> | ||
* | ||
* Copyright (c) 2015, Jon Schlinkert. | ||
* Licensed under the MIT License. | ||
* Copyright (c) 2015-2017, Jon Schlinkert. | ||
* Released under the MIT License. | ||
*/ | ||
@@ -11,17 +11,25 @@ | ||
module.exports = function reduce(arr, memo, iteratee, cb) { | ||
if (typeof cb !== 'function') { | ||
if (typeof memo === 'function' && typeof iteratee === 'function') { | ||
cb = iteratee; | ||
iteratee = memo; | ||
memo = []; | ||
} else { | ||
throw new TypeError('expected callback to be a function'); | ||
} | ||
} | ||
if (!Array.isArray(arr)) { | ||
throw new TypeError('expected an array'); | ||
cb(new TypeError('expected an array')); | ||
return; | ||
} | ||
if (typeof iteratee !== 'function') { | ||
throw new TypeError('expected iteratee to be a function'); | ||
cb(new TypeError('expected iteratee to be a function')); | ||
return; | ||
} | ||
if (typeof cb !== 'function') { | ||
throw new TypeError('expected callback to be a function'); | ||
} | ||
var callback = once(cb); | ||
(function next(i, acc) { | ||
if (i === arr.length) { | ||
callback(null, acc); | ||
cb(null, acc); | ||
return; | ||
@@ -32,3 +40,3 @@ } | ||
if (err) { | ||
callback(err); | ||
cb(err); | ||
return; | ||
@@ -40,12 +48,1 @@ } | ||
}; | ||
function once(fn) { | ||
var value; | ||
return function() { | ||
if (!fn.called) { | ||
fn.called = true; | ||
value = fn.apply(this, arguments); | ||
} | ||
return value; | ||
}; | ||
} |
{ | ||
"name": "async-array-reduce", | ||
"description": "Async reduce.", | ||
"version": "0.2.1", | ||
"description": "Async array reduce. Asynchronously applies a function against an accumulator and each element in an array (from left to right, in series) to reduce it to a single value.", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/jonschlinkert/async-array-reduce", | ||
"author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"contributors": [ | ||
"Brian Woodward (https://twitter.com/doowb)", | ||
"Jon Schlinkert (http://twitter.com/jonschlinkert)" | ||
], | ||
"repository": "jonschlinkert/async-array-reduce", | ||
@@ -13,4 +17,3 @@ "bugs": { | ||
"files": [ | ||
"index.js", | ||
"LICENSE" | ||
"index.js" | ||
], | ||
@@ -17,0 +20,0 @@ "main": "index.js", |
@@ -1,5 +0,7 @@ | ||
# async-array-reduce [![NPM version](https://img.shields.io/npm/v/async-array-reduce.svg?style=flat)](https://www.npmjs.com/package/async-array-reduce) [![NPM downloads](https://img.shields.io/npm/dm/async-array-reduce.svg?style=flat)](https://npmjs.org/package/async-array-reduce) [![Build Status](https://img.shields.io/travis/jonschlinkert/async-array-reduce.svg?style=flat)](https://travis-ci.org/jonschlinkert/async-array-reduce) | ||
# async-array-reduce [![NPM version](https://img.shields.io/npm/v/async-array-reduce.svg?style=flat)](https://www.npmjs.com/package/async-array-reduce) [![NPM monthly downloads](https://img.shields.io/npm/dm/async-array-reduce.svg?style=flat)](https://npmjs.org/package/async-array-reduce) [![NPM total downloads](https://img.shields.io/npm/dt/async-array-reduce.svg?style=flat)](https://npmjs.org/package/async-array-reduce) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/async-array-reduce.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/async-array-reduce) | ||
Async reduce. | ||
> Async array reduce. Asynchronously applies a function against an accumulator and each element in an array (from left to right, in series) to reduce it to a single value. | ||
Follow this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), for updates on this project and others. | ||
## Install | ||
@@ -18,6 +20,6 @@ | ||
reduce(['a', 'b', 'c'], [], function (acc, val, next) { | ||
acc.push(val + val); | ||
next(null, acc); | ||
}, function (err, arr) { | ||
reduce(['a', 'b', 'c'], [], function(acc, val, next) { | ||
next(null, acc.concat(val + val)); | ||
}, function (err, result) { | ||
console.log(result); | ||
//=> ['aa', 'bb', 'cc'] | ||
@@ -31,4 +33,6 @@ }); | ||
You might also be interested in these projects: | ||
* [arr-filter](https://www.npmjs.com/package/arr-filter): Faster alternative to javascript's native filter method. | [homepage](https://github.com/jonschlinkert/arr-filter "Faster alternative to javascript's native filter method.") | ||
* [arr-flatten](https://www.npmjs.com/package/arr-flatten): Recursively flatten an array or arrays. This is the fastest implementation of array flatten. | [homepage](https://github.com/jonschlinkert/arr-flatten "Recursively flatten an array or arrays. This is the fastest implementation of array flatten.") | ||
* [arr-flatten](https://www.npmjs.com/package/arr-flatten): Recursively flatten an array or arrays. | [homepage](https://github.com/jonschlinkert/arr-flatten "Recursively flatten an array or arrays.") | ||
* [arr-reduce](https://www.npmjs.com/package/arr-reduce): Fast array reduce that also loops over sparse elements. | [homepage](https://github.com/jonschlinkert/arr-reduce "Fast array reduce that also loops over sparse elements.") | ||
@@ -41,10 +45,17 @@ * [array-unique](https://www.npmjs.com/package/array-unique): Remove duplicate values from an array. Fastest ES5 implementation. | [homepage](https://github.com/jonschlinkert/array-unique "Remove duplicate values from an array. Fastest ES5 implementation.") | ||
### Contributors | ||
| **Commits** | **Contributor** | | ||
| --- | --- | | ||
| 6 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 2 | [doowb](https://github.com/doowb) | | ||
### Building docs | ||
_(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_ | ||
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ | ||
To generate the readme and API documentation with [verb](https://github.com/verbose/verb): | ||
To generate the readme, run the following command: | ||
```sh | ||
$ npm install -g verb verb-generate-readme && verb | ||
$ npm install -g verbose/verb#dev verb-generate-readme && verb | ||
``` | ||
@@ -54,6 +65,6 @@ | ||
Install dev dependencies: | ||
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: | ||
```sh | ||
$ npm install -d && npm test | ||
$ npm install && npm test | ||
``` | ||
@@ -66,11 +77,11 @@ | ||
* [github/jonschlinkert](https://github.com/jonschlinkert) | ||
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert) | ||
### License | ||
Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT license](https://github.com/jonschlinkert/async-array-reduce/blob/master/LICENSE). | ||
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT License](LICENSE). | ||
*** | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.1.28, on August 02, 2016._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on September 09, 2017._ |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
6956
0
82
0
39