time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

factory.js (4535B)


      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 and returns group counts.
     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 {Function} indicator - indicator function specifying which group an element in the input collection belongs to
     45 * @throws {TypeError} options argument must be an object
     46 * @throws {TypeError} must provide valid options
     47 * @throws {TypeError} last argument must be a function
     48 * @returns {Function} function which invokes the indicator function once for each element in a collection
     49 *
     50 * @example
     51 * var readFile = require( '@stdlib/fs/read-file' );
     52 *
     53 * function indicator( file, next ) {
     54 *     var opts = {
     55 *         'encoding': 'utf8'
     56 *     };
     57 *     readFile( file, opts, onFile );
     58 *
     59 *     function onFile( error ) {
     60 *         if ( error ) {
     61 *             return next( null, 'nonreadable' );
     62 *         }
     63 *         next( null, 'readable' );
     64 *     }
     65 * }
     66 *
     67 * var opts = {
     68 *     'series': true
     69 * };
     70 *
     71 * // Create a `countByAsync` function which invokes the indicator function for each collection element sequentially:
     72 * var countByAsync = factory( opts, indicator );
     73 *
     74 * // Create a collection over which to iterate:
     75 * var files = [
     76 *     './beep.js',
     77 *     './boop.js'
     78 * ];
     79 *
     80 * // Define a callback which handles results:
     81 * function done( error, result ) {
     82 *     if ( error ) {
     83 *         throw error;
     84 *     }
     85 *     console.log( result );
     86 * }
     87 *
     88 * // Try to read each element in `files`:
     89 * countByAsync( files, done );
     90 */
     91 function factory( options, indicator ) {
     92 	var opts;
     93 	var err;
     94 	var f;
     95 
     96 	opts = {};
     97 	if ( arguments.length > 1 ) {
     98 		err = validate( opts, options );
     99 		if ( err ) {
    100 			throw err;
    101 		}
    102 		f = indicator;
    103 	} else {
    104 		f = options;
    105 	}
    106 	if ( !isFunction( f ) ) {
    107 		throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+f+'`.' );
    108 	}
    109 	if ( opts.series ) {
    110 		opts.limit = 1;
    111 	} else if ( !opts.limit ) {
    112 		opts.limit = PINF;
    113 	}
    114 	return countByAsync;
    115 
    116 	/**
    117 	* Invokes an indicator function for each element in a collection.
    118 	*
    119 	* @private
    120 	* @param {Collection} collection - input collection
    121 	* @param {Callback} done - function to invoke upon completion
    122 	* @throws {TypeError} first argument must be a collection
    123 	* @throws {TypeError} last argument must be a function
    124 	* @returns {void}
    125 	*/
    126 	function countByAsync( collection, done ) {
    127 		if ( !isCollection( collection ) ) {
    128 			throw new TypeError( 'invalid argument. First argument must be a collection. Value: `'+collection+'.`' );
    129 		}
    130 		if ( !isFunction( done ) ) {
    131 			throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+done+'`.' );
    132 		}
    133 		return limit( collection, opts, f, clbk );
    134 
    135 		/**
    136 		* Callback invoked upon completion.
    137 		*
    138 		* @private
    139 		* @param {*} [error] - error
    140 		* @param {Object} result - counts
    141 		* @returns {void}
    142 		*/
    143 		function clbk( error, result ) {
    144 			if ( error ) {
    145 				return done( error );
    146 			}
    147 			done( null, result );
    148 		}
    149 	}
    150 }
    151 
    152 
    153 // EXPORTS //
    154 
    155 module.exports = factory;