factory.js (4779B)
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 invoke a function once for each element in a collection and to update the collection in-place. 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. Note, however, that the function may have mutated an input collection during prior invocations, resulting in a partially mutated collection. 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=false] - 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( 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( error ); 63 * } 64 * next( null, data ); 65 * } 66 * } 67 * 68 * var opts = { 69 * 'series': true 70 * }; 71 * 72 * // Create a `inmapAsync` function which invokes `read` for each collection element sequentially: 73 * var inmapAsync = factory( opts, read ); 74 * 75 * // Create a collection over which to iterate: 76 * var files = [ 77 * './beep.js', 78 * './boop.js' 79 * ]; 80 * 81 * // Define a callback which handles errors: 82 * function done( error, results ) { 83 * if ( error ) { 84 * throw error; 85 * } 86 * console.log( results ); 87 * } 88 * 89 * // Run `read` for each element in `files`: 90 * inmapAsync( files, done ); 91 */ 92 function factory( options, fcn ) { 93 var opts; 94 var err; 95 var f; 96 97 opts = {}; 98 if ( arguments.length > 1 ) { 99 err = validate( opts, options ); 100 if ( err ) { 101 throw err; 102 } 103 f = fcn; 104 } else { 105 f = options; 106 } 107 if ( !isFunction( f ) ) { 108 throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+f+'`.' ); 109 } 110 if ( opts.series ) { 111 opts.limit = 1; 112 } else if ( !opts.limit ) { 113 opts.limit = PINF; 114 } 115 return inmapAsync; 116 117 /** 118 * Invokes a function for each element in a collection and updates a collection in-place. 119 * 120 * @private 121 * @param {Collection} collection - input collection 122 * @param {Callback} done - function to invoke upon completion 123 * @throws {TypeError} first argument must be a collection 124 * @throws {TypeError} last argument must be a function 125 * @returns {void} 126 */ 127 function inmapAsync( collection, done ) { 128 if ( !isCollection( collection ) ) { 129 throw new TypeError( 'invalid argument. First argument must be a collection. Value: `'+collection+'.`' ); 130 } 131 if ( !isFunction( done ) ) { 132 throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+done+'`.' ); 133 } 134 return limit( collection, opts, f, clbk ); 135 136 /** 137 * Callback invoked upon completion. 138 * 139 * @private 140 * @param {*} [error] - error 141 * @returns {void} 142 */ 143 function clbk( error ) { 144 if ( error ) { 145 return done( error, collection ); 146 } 147 done( null, collection ); 148 } 149 } 150 } 151 152 153 // EXPORTS // 154 155 module.exports = factory;