factory.js (5294B)
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 isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; 25 var isCollection = require( '@stdlib/assert/is-collection' ); 26 var PINF = require( '@stdlib/constants/float64/pinf' ); 27 var validate = require( './validate.js' ); 28 var limit = require( './limit.js' ); 29 30 31 // MAIN // 32 33 /** 34 * Returns a function for testing whether a collection contains at least `n` elements which pass a test implemented by a predicate function, iterating from right to left. 35 * 36 * ## Notes 37 * 38 * - 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. 39 * - 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`). 40 * 41 * 42 * @param {Options} [options] - function options 43 * @param {*} [options.thisArg] - execution context 44 * @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time 45 * @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 46 * @param {Function} predicate - predicate function to invoke for each element in a collection 47 * @throws {TypeError} options argument must be an object 48 * @throws {TypeError} must provide valid options 49 * @throws {TypeError} last argument must be a function 50 * @returns {Function} function which invokes the predicate function once for each element in a collection 51 * 52 * @example 53 * var readFile = require( '@stdlib/fs/read-file' ); 54 * 55 * function predicate( file, next ) { 56 * var opts = { 57 * 'encoding': 'utf8' 58 * }; 59 * readFile( file, opts, onFile ); 60 * 61 * function onFile( error ) { 62 * if ( error ) { 63 * return next( null, false ); 64 * } 65 * next( null, true ); 66 * } 67 * } 68 * 69 * var opts = { 70 * 'series': true 71 * }; 72 * 73 * // Create a `someByRightAsync` function which invokes the predicate function for each collection element sequentially: 74 * var someByRightAsync = factory( opts, predicate ); 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 results: 83 * function done( error, bool ) { 84 * if ( error ) { 85 * throw error; 86 * } 87 * if ( bool ) { 88 * console.log( 'Successfully read some files.' ); 89 * } else { 90 * console.log( 'Unable to read some files.' ); 91 * } 92 * } 93 * 94 * // Try to read each element in `files`: 95 * someByRightAsync( files, 2, done ); 96 */ 97 function factory( options, predicate ) { 98 var opts; 99 var err; 100 var f; 101 102 opts = {}; 103 if ( arguments.length > 1 ) { 104 err = validate( opts, options ); 105 if ( err ) { 106 throw err; 107 } 108 f = predicate; 109 } else { 110 f = options; 111 } 112 if ( !isFunction( f ) ) { 113 throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+f+'`.' ); 114 } 115 if ( opts.series ) { 116 opts.limit = 1; 117 } else if ( !opts.limit ) { 118 opts.limit = PINF; 119 } 120 return someByRightAsync; 121 122 /** 123 * Invokes a predicate function for each element in a collection, iterating from right to left. 124 * 125 * @private 126 * @param {Collection} collection - input collection 127 * @param {PositiveInteger} n - number of elements 128 * @param {Callback} done - function to invoke upon completion 129 * @throws {TypeError} first argument must be a collection 130 * @throws {TypeError} second argument must be a positive integer 131 * @throws {TypeError} last argument must be a function 132 * @returns {void} 133 */ 134 function someByRightAsync( collection, n, done ) { 135 if ( !isCollection( collection ) ) { 136 throw new TypeError( 'invalid argument. First argument must be a collection. Value: `'+collection+'.`' ); 137 } 138 if ( !isPositiveInteger( n ) ) { 139 throw new TypeError( 'invalid argument. Second argument must be a positive integer. Value: `'+n+'`.' ); 140 } 141 if ( !isFunction( done ) ) { 142 throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+done+'`.' ); 143 } 144 return limit( collection, n, opts, f, clbk ); 145 146 /** 147 * Callback invoked upon completion. 148 * 149 * @private 150 * @param {*} [error] - error 151 * @param {boolean} bool - test result 152 * @returns {void} 153 */ 154 function clbk( error, bool ) { 155 if ( error ) { 156 return done( error, false ); 157 } 158 done( null, bool ); 159 } 160 } 161 } 162 163 164 // EXPORTS // 165 166 module.exports = factory;