factory.js (4688B)
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, iterating from right to left. 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=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 ) { 61 * if ( error ) { 62 * return next( error ); 63 * } 64 * console.log( 'Successfully read file: %s', file ); 65 * next(); 66 * } 67 * } 68 * 69 * var opts = { 70 * 'series': true 71 * }; 72 * 73 * // Create a `forEachRightAsync` function which invokes `read` for each collection element sequentially: 74 * var forEachRightAsync = 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 ) { 84 * if ( error ) { 85 * throw error; 86 * } 87 * console.log( 'Successfully read all files.' ); 88 * } 89 * 90 * // Run `read` for each element in `files`: 91 * forEachRightAsync( files, done ); 92 */ 93 function factory( options, fcn ) { 94 var opts; 95 var err; 96 var f; 97 98 opts = {}; 99 if ( arguments.length > 1 ) { 100 err = validate( opts, options ); 101 if ( err ) { 102 throw err; 103 } 104 f = fcn; 105 } else { 106 f = options; 107 } 108 if ( !isFunction( f ) ) { 109 throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+f+'`.' ); 110 } 111 if ( opts.series ) { 112 opts.limit = 1; 113 } else if ( !opts.limit ) { 114 opts.limit = PINF; 115 } 116 return forEachRightAsync; 117 118 /** 119 * Invokes a function for each element in a collection, iterating from right to left. 120 * 121 * @private 122 * @param {Collection} collection - input collection 123 * @param {Callback} done - function to invoke upon completion 124 * @throws {TypeError} first argument must be a collection 125 * @throws {TypeError} last argument must be a function 126 * @returns {void} 127 */ 128 function forEachRightAsync( collection, done ) { 129 if ( !isCollection( collection ) ) { 130 throw new TypeError( 'invalid argument. First argument must be a collection. Value: `'+collection+'.`' ); 131 } 132 if ( !isFunction( done ) ) { 133 throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+done+'`.' ); 134 } 135 return limit( collection, opts, f, clbk ); 136 137 /** 138 * Callback invoked upon completion. 139 * 140 * @private 141 * @param {*} [error] - error 142 * @returns {void} 143 */ 144 function clbk( error ) { 145 if ( error ) { 146 return done( error ); 147 } 148 done(); 149 } 150 } 151 } 152 153 154 // EXPORTS // 155 156 module.exports = factory;