reduce.js (3130B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var factory = require( './factory.js' ); 24 25 26 // MAIN // 27 28 /** 29 * Applies a function against an accumulator and each element in a collection and return the accumulated result. 30 * 31 * ## Notes 32 * 33 * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. 34 * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). 35 * 36 * 37 * @param {Collection} collection - input collection 38 * @param {*} initial - initial value 39 * @param {Options} [options] - function options 40 * @param {*} [options.thisArg] - execution context 41 * @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time 42 * @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection 43 * @param {Function} fcn - function to invoke for each element in a collection 44 * @param {Callback} done - function to invoke upon completion 45 * @throws {TypeError} first argument must be a collection 46 * @throws {TypeError} options argument must be an object 47 * @throws {TypeError} must provide valid options 48 * @throws {TypeError} second-to-last argument must be a function 49 * @throws {TypeError} last argument must be a function 50 * @returns {void} 51 * 52 * @example 53 * var readFile = require( '@stdlib/fs/read-file' ); 54 * 55 * function done( error, acc ) { 56 * if ( error ) { 57 * throw error; 58 * } 59 * console.log( acc.count ); 60 * } 61 * 62 * function read( acc, file, next ) { 63 * var opts = { 64 * 'encoding': 'utf8' 65 * }; 66 * readFile( file, opts, onFile ); 67 * 68 * function onFile( error ) { 69 * if ( error ) { 70 * return next( null, acc ); 71 * } 72 * acc.count += 1; 73 * next( null, acc ); 74 * } 75 * } 76 * 77 * var files = [ 78 * './beep.js', 79 * './boop.js' 80 * ]; 81 * var acc = { 82 * 'count': 0 83 * }; 84 * reduceAsync( files, acc, read, done ); 85 */ 86 function reduceAsync( collection, initial, options, fcn, done ) { 87 if ( arguments.length < 5 ) { 88 return factory( options )( collection, initial, fcn ); 89 } 90 factory( options, fcn )( collection, initial, done ); 91 } 92 93 94 // EXPORTS // 95 96 module.exports = reduceAsync;