time-to-botec

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

factory.js (4597B)


      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 PINF = require( '@stdlib/constants/float64/pinf' );
     25 var validate = require( './validate.js' );
     26 var limit = require( './limit.js' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Returns a function to map keys from one object to a new object having the same values.
     33 *
     34 * ## Notes
     35 *
     36 * -   If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.
     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 * -   Iteration and insertion order are **not** guaranteed.
     39 * -   The function only operates on own properties, not inherited properties.
     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 own property
     46 * @param {Function} transform - transform function
     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 maps keys from one object to a new object having the same values
     51 *
     52 * @example
     53 * var readFile = require( '@stdlib/fs/read-file' );
     54 *
     55 * function read( key, value, next ) {
     56 *     var opts = {
     57 *         'encoding': 'utf8'
     58 *     };
     59 *     readFile( value, opts, onFile );
     60 *
     61 *     function onFile( error ) {
     62 *         if ( error ) {
     63 *             return next( null, key+':unreadable' );
     64 *         }
     65 *         next( null, key+':readable' );
     66 *     }
     67 * }
     68 *
     69 * var opts = {
     70 *     'series': true
     71 * };
     72 *
     73 * // Create a reusable function:
     74 * var mapKeysAsync = factory( opts, read );
     75 *
     76 * // Create a dictionary of file names:
     77 * var files = {
     78 *     'file1': './beep.js',
     79 *     'file2': './boop.js'
     80 * };
     81 *
     82 * // Define a callback which handles errors:
     83 * function done( error, out ) {
     84 *     if ( error ) {
     85 *         throw error;
     86 *     }
     87 *     console.log( out );
     88 * }
     89 *
     90 * // Process each file in `files`:
     91 * mapKeysAsync( files, done );
     92 */
     93 function factory( options, transform ) {
     94 	var opts;
     95 	var err;
     96 	var f;
     97 
     98 	opts = {};
     99 	if ( arguments.length > 1 ) {
    100 		err = validate( opts, options );
    101 		if ( err ) {
    102 			throw err;
    103 		}
    104 		f = transform;
    105 	} else {
    106 		f = options;
    107 	}
    108 	if ( !isFunction( f ) ) {
    109 		throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+f+'`.' );
    110 	}
    111 	if ( opts.series ) {
    112 		opts.limit = 1;
    113 	} else if ( !opts.limit ) {
    114 		opts.limit = PINF;
    115 	}
    116 	return mapKeysAsync;
    117 
    118 	/**
    119 	* Maps keys from one object to a new object having the same values.
    120 	*
    121 	* @private
    122 	* @param {Object} obj - source object
    123 	* @param {Callback} done - function to invoke upon completion
    124 	* @throws {TypeError} first argument must be an object
    125 	* @throws {TypeError} last argument must be a function
    126 	* @returns {void}
    127 	*/
    128 	function mapKeysAsync( obj, done ) {
    129 		if ( typeof obj !== 'object' || obj === null ) {
    130 			throw new TypeError( 'invalid argument. First argument must be an object. Value: `'+obj+'.`' );
    131 		}
    132 		if ( !isFunction( done ) ) {
    133 			throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+done+'`.' );
    134 		}
    135 		return limit( obj, opts, f, clbk );
    136 
    137 		/**
    138 		* Callback invoked upon completion.
    139 		*
    140 		* @private
    141 		* @param {*} [error] - error
    142 		* @param {Object} [out] - output object
    143 		* @returns {void}
    144 		*/
    145 		function clbk( error, out ) {
    146 			if ( error ) {
    147 				return done( error );
    148 			}
    149 			done( null, out );
    150 		}
    151 	}
    152 }
    153 
    154 
    155 // EXPORTS //
    156 
    157 module.exports = factory;