factory.js (4918B)
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 isFunction = require( '@stdlib/assert/is-function' ); 24 var isCollection = require( '@stdlib/assert/is-collection' ); 25 var PINF = require( '@stdlib/constants/float64/pinf' ); 26 var validate = require( './validate.js' ); 27 var limit = require( './limit.js' ); 28 29 30 // MAIN // 31 32 /** 33 * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. 34 * 35 * ## Notes 36 * 37 * - 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. 38 * - 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`). 39 * 40 * 41 * @param {Options} [options] - function options 42 * @param {*} [options.thisArg] - execution context 43 * @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time 44 * @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 45 * @param {Function} fcn - function to invoke for each element in a collection 46 * @throws {TypeError} options argument must be an object 47 * @throws {TypeError} must provide valid options 48 * @throws {TypeError} last argument must be a function 49 * @returns {Function} function which invokes the provided function once for each element in a collection 50 * 51 * @example 52 * var readFile = require( '@stdlib/fs/read-file' ); 53 * 54 * function read( acc, file, next ) { 55 * var opts = { 56 * 'encoding': 'utf8' 57 * }; 58 * readFile( file, opts, onFile ); 59 * 60 * function onFile( error, data ) { 61 * if ( error ) { 62 * return next( null, acc ); 63 * } 64 * acc.count += 1; 65 * next( null, acc ); 66 * } 67 * } 68 * 69 * var opts = { 70 * 'series': false 71 * }; 72 * 73 * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: 74 * var reduceAsync = factory( opts, read ); 75 * 76 * // Create a collection over which to iterate: 77 * var files = [ 78 * './beep.js', 79 * './boop.js' 80 * ]; 81 * 82 * // Define a callback which handles errors: 83 * function done( error, acc ) { 84 * if ( error ) { 85 * throw error; 86 * } 87 * console.log( acc.count ); 88 * } 89 * 90 * // Run `read` for each element in `files`: 91 * var acc = { 92 * 'count': 0 93 * }; 94 * reduceAsync( files, acc, done ); 95 */ 96 function factory( options, fcn ) { 97 var opts; 98 var err; 99 var f; 100 101 opts = {}; 102 if ( arguments.length > 1 ) { 103 err = validate( opts, options ); 104 if ( err ) { 105 throw err; 106 } 107 f = fcn; 108 } else { 109 f = options; 110 } 111 if ( !isFunction( f ) ) { 112 throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+f+'`.' ); 113 } 114 if ( opts.series === void 0 && opts.limit === void 0 ) { 115 opts.series = true; 116 } 117 if ( opts.series ) { 118 opts.limit = 1; 119 } else if ( !opts.limit ) { 120 opts.limit = PINF; 121 } 122 return reduceAsync; 123 124 /** 125 * Applies a function against an accumulator and each element in a collection and return the accumulated result. 126 * 127 * @private 128 * @param {Collection} collection - input collection 129 * @param {*} initial - initial value 130 * @param {Callback} done - function to invoke upon completion 131 * @throws {TypeError} first argument must be a collection 132 * @throws {TypeError} last argument must be a function 133 * @returns {void} 134 */ 135 function reduceAsync( collection, initial, done ) { 136 if ( !isCollection( collection ) ) { 137 throw new TypeError( 'invalid argument. First argument must be a collection. Value: `'+collection+'.`' ); 138 } 139 if ( !isFunction( done ) ) { 140 throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+done+'`.' ); 141 } 142 return limit( collection, initial, opts, f, clbk ); 143 144 /** 145 * Callback invoked upon completion. 146 * 147 * @private 148 * @param {*} [error] - error 149 * @param {*} [acc] - accumulated value 150 * @returns {void} 151 */ 152 function clbk( error, acc ) { 153 if ( error ) { 154 return done( error ); 155 } 156 done( null, acc ); 157 } 158 } 159 } 160 161 162 // EXPORTS // 163 164 module.exports = factory;