map_keys.js (3250B)
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 factory = require( './factory.js' ); 24 25 26 // MAIN // 27 28 /** 29 * Maps keys from one object to a new object having the same values. 30 * 31 * ## Notes 32 * 33 * - 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. 34 * - 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`). 35 * - Iteration and insertion order are **not** guaranteed. 36 * - The function only operates on own properties, not inherited properties. 37 * 38 * 39 * @param {Object} obj - source object 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 own property 44 * @param {Function} transform - transform function 45 * @param {Callback} done - function to invoke upon completion 46 * @throws {TypeError} first argument must be an object 47 * @throws {TypeError} options argument must be an object 48 * @throws {TypeError} must provide valid options 49 * @throws {TypeError} second-to-last argument must be a function 50 * @throws {TypeError} last argument must be a function 51 * @returns {void} 52 * 53 * @example 54 * var readFile = require( '@stdlib/fs/read-file' ); 55 * 56 * function read( key, value, next ) { 57 * var opts = { 58 * 'encoding': 'utf8' 59 * }; 60 * readFile( value, opts, onFile ); 61 * 62 * function onFile( error ) { 63 * if ( error ) { 64 * return next( null, key+':unreadable' ); 65 * } 66 * next( null, key+':readable' ); 67 * } 68 * } 69 * 70 * // Define a callback which handles errors: 71 * function done( error, out ) { 72 * if ( error ) { 73 * throw error; 74 * } 75 * console.log( out ); 76 * } 77 * 78 * // Create a dictionary of file names: 79 * var files = { 80 * 'file1': './beep.js', 81 * 'file2': './boop.js' 82 * }; 83 * 84 * var opts = { 85 * 'series': true 86 * }; 87 * 88 * // Process each file in `files`: 89 * mapKeysAsync( files, opts, read, done ); 90 */ 91 function mapKeysAsync( obj, options, transform, done ) { 92 if ( arguments.length < 4 ) { 93 return factory( options )( obj, transform ); 94 } 95 factory( options, transform )( obj, done ); 96 } 97 98 99 // EXPORTS // 100 101 module.exports = mapKeysAsync;