factory.js (4681B)
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 grouping values according to an indicator function. 34 * 35 * ## Notes 36 * 37 * - 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`). 38 * 39 * 40 * @param {Options} [options] - function options 41 * @param {*} [options.thisArg] - execution context 42 * @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time 43 * @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 44 * @param {string} [options.returns="values"] - if `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned 45 * @param {Function} indicator - indicator function specifying which group an element in the input collection belongs to 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 indicator function once for each element in a collection 50 * 51 * @example 52 * var readFile = require( '@stdlib/fs/read-file' ); 53 * 54 * function indicator( 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, 'nonreadable' ); 63 * } 64 * next( null, 'readable' ); 65 * } 66 * } 67 * 68 * var opts = { 69 * 'series': true 70 * }; 71 * 72 * // Create a `groupByAsync` function which invokes the indicator function for each collection element sequentially: 73 * var groupByAsync = factory( opts, indicator ); 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, result ) { 83 * if ( error ) { 84 * throw error; 85 * } 86 * console.log( result ); 87 * } 88 * 89 * // Try to read each element in `files`: 90 * groupByAsync( files, done ); 91 */ 92 function factory( options, indicator ) { 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 = indicator; 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 groupByAsync; 116 117 /** 118 * Invokes an indicator function for each element in a collection. 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 groupByAsync( 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 * @param {Object} result - group-by result 142 * @returns {void} 143 */ 144 function clbk( error, result ) { 145 if ( error ) { 146 return done( error ); 147 } 148 done( null, result ); 149 } 150 } 151 } 152 153 154 // EXPORTS // 155 156 module.exports = factory;