factory.js (4501B)
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 values from one object to a new object having the same keys. 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 values from one object to a new object having the same keys 51 * 52 * @example 53 * var stat = require( 'fs' ).stat; 54 * 55 * function getStats( file, next ) { 56 * stat( file, onStats ); 57 * 58 * function onStats( error, data ) { 59 * if ( error ) { 60 * return next( error ); 61 * } 62 * next( null, data ); 63 * } 64 * } 65 * 66 * var opts = { 67 * 'series': true 68 * }; 69 * 70 * // Create a reusable function: 71 * var mapValuesAsync = factory( opts, getStats ); 72 * 73 * // Create a dictionary of file names: 74 * var files = { 75 * 'file1': './beep.js', 76 * 'file2': './boop.js' 77 * }; 78 * 79 * // Define a callback which handles errors: 80 * function done( error, out ) { 81 * if ( error ) { 82 * throw error; 83 * } 84 * console.log( out ); 85 * } 86 * 87 * // Process each file in `files`: 88 * mapValuesAsync( files, done ); 89 */ 90 function factory( options, transform ) { 91 var opts; 92 var err; 93 var f; 94 95 opts = {}; 96 if ( arguments.length > 1 ) { 97 err = validate( opts, options ); 98 if ( err ) { 99 throw err; 100 } 101 f = transform; 102 } else { 103 f = options; 104 } 105 if ( !isFunction( f ) ) { 106 throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+f+'`.' ); 107 } 108 if ( opts.series ) { 109 opts.limit = 1; 110 } else if ( !opts.limit ) { 111 opts.limit = PINF; 112 } 113 return mapValuesAsync; 114 115 /** 116 * Maps values from one object to a new object having the same keys. 117 * 118 * @private 119 * @param {Object} obj - source object 120 * @param {Callback} done - function to invoke upon completion 121 * @throws {TypeError} first argument must be an object 122 * @throws {TypeError} last argument must be a function 123 * @returns {void} 124 */ 125 function mapValuesAsync( obj, done ) { 126 if ( typeof obj !== 'object' || obj === null ) { 127 throw new TypeError( 'invalid argument. First argument must be an object. Value: `'+obj+'.`' ); 128 } 129 if ( !isFunction( done ) ) { 130 throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+done+'`.' ); 131 } 132 return limit( obj, opts, f, clbk ); 133 134 /** 135 * Callback invoked upon completion. 136 * 137 * @private 138 * @param {*} [error] - error 139 * @param {Object} [out] - output object 140 * @returns {void} 141 */ 142 function clbk( error, out ) { 143 if ( error ) { 144 return done( error ); 145 } 146 done( null, out ); 147 } 148 } 149 } 150 151 152 // EXPORTS // 153 154 module.exports = factory;