factory.js (4842B)
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 for testing whether at least one element in a collection passes a test implemented by a predicate function. 34 * 35 * ## Notes 36 * 37 * - If a predicate 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} predicate - predicate 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 predicate function once for each element in a collection 50 * 51 * @example 52 * var readFile = require( '@stdlib/fs/read-file' ); 53 * 54 * function predicate( 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( null, false ); 63 * } 64 * next( null, true ); 65 * } 66 * } 67 * 68 * var opts = { 69 * 'series': true 70 * }; 71 * 72 * // Create a `anyByAsync` function which invokes the predicate function for each collection element sequentially: 73 * var anyByAsync = factory( opts, predicate ); 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 results: 82 * function done( error, bool ) { 83 * if ( error ) { 84 * throw error; 85 * } 86 * if ( bool ) { 87 * console.log( 'Successfully read at least one file.' ); 88 * } else { 89 * console.log( 'Unable to read any files.' ); 90 * } 91 * } 92 * 93 * // Try to read each element in `files`: 94 * anyByAsync( files, done ); 95 */ 96 function factory( options, predicate ) { 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 = predicate; 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 ) { 115 opts.limit = 1; 116 } else if ( !opts.limit ) { 117 opts.limit = PINF; 118 } 119 return anyByAsync; 120 121 /** 122 * Invokes a predicate function for each element in a collection. 123 * 124 * @private 125 * @param {Collection} collection - input collection 126 * @param {Callback} done - function to invoke upon completion 127 * @throws {TypeError} first argument must be a collection 128 * @throws {TypeError} last argument must be a function 129 * @returns {void} 130 */ 131 function anyByAsync( collection, done ) { 132 if ( !isCollection( collection ) ) { 133 throw new TypeError( 'invalid argument. First argument must be a collection. Value: `'+collection+'.`' ); 134 } 135 if ( !isFunction( done ) ) { 136 throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+done+'`.' ); 137 } 138 return limit( collection, opts, f, clbk ); 139 140 /** 141 * Callback invoked upon completion. 142 * 143 * @private 144 * @param {*} [error] - error 145 * @param {boolean} bool - test result 146 * @returns {void} 147 */ 148 function clbk( error, bool ) { 149 if ( error ) { 150 return done( error, false ); 151 } 152 done( null, bool ); 153 } 154 } 155 } 156 157 158 // EXPORTS // 159 160 module.exports = factory;