time-to-botec

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

factory.js (4780B)


      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 generating a frequency table 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 * -   The output frequency table is an array of arrays. Each sub-array corresponds to a unique value in the input collection and is structured as follows:
     40 *
     41 *     -   0: unique value
     42 *     -   1: value count
     43 *     -   2: frequency percentage
     44 *
     45 *
     46 * @param {Options} [options] - function options
     47 * @param {*} [options.thisArg] - execution context
     48 * @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time
     49 * @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
     50 * @param {Function} indicator - function whose return values are used to populate the output frequency table
     51 * @throws {TypeError} options argument must be an object
     52 * @throws {TypeError} must provide valid options
     53 * @throws {TypeError} last argument must be a function
     54 * @returns {Function} function which invokes the indicator function once for each element in a collection
     55 *
     56 * @example
     57 * var readFile = require( '@stdlib/fs/read-file' );
     58 *
     59 * function indicator( file, next ) {
     60 *     var opts = {
     61 *         'encoding': 'utf8'
     62 *     };
     63 *     readFile( file, opts, onFile );
     64 *
     65 *     function onFile( error ) {
     66 *         if ( error ) {
     67 *             return next( null, 'nonreadable' );
     68 *         }
     69 *         next( null, 'readable' );
     70 *     }
     71 * }
     72 *
     73 * var opts = {
     74 *     'series': true
     75 * };
     76 *
     77 * // Create a `tabulateByAsync` function which invokes the indicator function for each collection element sequentially:
     78 * var tabulateByAsync = factory( opts, indicator );
     79 *
     80 * // Create a collection over which to iterate:
     81 * var files = [
     82 *     './beep.js',
     83 *     './boop.js'
     84 * ];
     85 *
     86 * // Define a callback which handles results:
     87 * function done( error, result ) {
     88 *     if ( error ) {
     89 *         throw error;
     90 *     }
     91 *     console.log( result );
     92 * }
     93 *
     94 * // Try to read each element in `files`:
     95 * tabulateByAsync( files, done );
     96 */
     97 function factory( options, indicator ) {
     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 = indicator;
    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 tabulateByAsync;
    121 
    122 	/**
    123 	* Invokes an indicator function for each element in a collection.
    124 	*
    125 	* @private
    126 	* @param {Collection} collection - input collection
    127 	* @param {Callback} done - function to invoke upon completion
    128 	* @throws {TypeError} first argument must be a collection
    129 	* @throws {TypeError} last argument must be a function
    130 	* @returns {void}
    131 	*/
    132 	function tabulateByAsync( collection, done ) {
    133 		if ( !isCollection( collection ) ) {
    134 			throw new TypeError( 'invalid argument. First argument must be a collection. Value: `'+collection+'.`' );
    135 		}
    136 		if ( !isFunction( done ) ) {
    137 			throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+done+'`.' );
    138 		}
    139 		return limit( collection, opts, f, clbk );
    140 
    141 		/**
    142 		* Callback invoked upon completion.
    143 		*
    144 		* @private
    145 		* @param {*} [error] - error
    146 		* @param {Object} result - frequency table
    147 		* @returns {void}
    148 		*/
    149 		function clbk( error, result ) {
    150 			if ( error ) {
    151 				return done( error );
    152 			}
    153 			done( null, result );
    154 		}
    155 	}
    156 }
    157 
    158 
    159 // EXPORTS //
    160 
    161 module.exports = factory;