| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = mapValues;
|
|---|
| 7 |
|
|---|
| 8 | var _mapValuesLimit = require('./mapValuesLimit.js');
|
|---|
| 9 |
|
|---|
| 10 | var _mapValuesLimit2 = _interopRequireDefault(_mapValuesLimit);
|
|---|
| 11 |
|
|---|
| 12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * A relative of [`map`]{@link module:Collections.map}, designed for use with objects.
|
|---|
| 16 | *
|
|---|
| 17 | * Produces a new Object by mapping each value of `obj` through the `iteratee`
|
|---|
| 18 | * function. The `iteratee` is called each `value` and `key` from `obj` and a
|
|---|
| 19 | * callback for when it has finished processing. Each of these callbacks takes
|
|---|
| 20 | * two arguments: an `error`, and the transformed item from `obj`. If `iteratee`
|
|---|
| 21 | * passes an error to its callback, the main `callback` (for the `mapValues`
|
|---|
| 22 | * function) is immediately called with the error.
|
|---|
| 23 | *
|
|---|
| 24 | * Note, the order of the keys in the result is not guaranteed. The keys will
|
|---|
| 25 | * be roughly in the order they complete, (but this is very engine-specific)
|
|---|
| 26 | *
|
|---|
| 27 | * @name mapValues
|
|---|
| 28 | * @static
|
|---|
| 29 | * @memberOf module:Collections
|
|---|
| 30 | * @method
|
|---|
| 31 | * @category Collection
|
|---|
| 32 | * @param {Object} obj - A collection to iterate over.
|
|---|
| 33 | * @param {AsyncFunction} iteratee - A function to apply to each value and key
|
|---|
| 34 | * in `coll`.
|
|---|
| 35 | * The iteratee should complete with the transformed value as its result.
|
|---|
| 36 | * Invoked with (value, key, callback).
|
|---|
| 37 | * @param {Function} [callback] - A callback which is called when all `iteratee`
|
|---|
| 38 | * functions have finished, or an error occurs. `result` is a new object consisting
|
|---|
| 39 | * of each key from `obj`, with each transformed value on the right-hand side.
|
|---|
| 40 | * Invoked with (err, result).
|
|---|
| 41 | * @returns {Promise} a promise, if no callback is passed
|
|---|
| 42 | * @example
|
|---|
| 43 | *
|
|---|
| 44 | * // file1.txt is a file that is 1000 bytes in size
|
|---|
| 45 | * // file2.txt is a file that is 2000 bytes in size
|
|---|
| 46 | * // file3.txt is a file that is 3000 bytes in size
|
|---|
| 47 | * // file4.txt does not exist
|
|---|
| 48 | *
|
|---|
| 49 | * const fileMap = {
|
|---|
| 50 | * f1: 'file1.txt',
|
|---|
| 51 | * f2: 'file2.txt',
|
|---|
| 52 | * f3: 'file3.txt'
|
|---|
| 53 | * };
|
|---|
| 54 | *
|
|---|
| 55 | * const withMissingFileMap = {
|
|---|
| 56 | * f1: 'file1.txt',
|
|---|
| 57 | * f2: 'file2.txt',
|
|---|
| 58 | * f3: 'file4.txt'
|
|---|
| 59 | * };
|
|---|
| 60 | *
|
|---|
| 61 | * // asynchronous function that returns the file size in bytes
|
|---|
| 62 | * function getFileSizeInBytes(file, key, callback) {
|
|---|
| 63 | * fs.stat(file, function(err, stat) {
|
|---|
| 64 | * if (err) {
|
|---|
| 65 | * return callback(err);
|
|---|
| 66 | * }
|
|---|
| 67 | * callback(null, stat.size);
|
|---|
| 68 | * });
|
|---|
| 69 | * }
|
|---|
| 70 | *
|
|---|
| 71 | * // Using callbacks
|
|---|
| 72 | * async.mapValues(fileMap, getFileSizeInBytes, function(err, result) {
|
|---|
| 73 | * if (err) {
|
|---|
| 74 | * console.log(err);
|
|---|
| 75 | * } else {
|
|---|
| 76 | * console.log(result);
|
|---|
| 77 | * // result is now a map of file size in bytes for each file, e.g.
|
|---|
| 78 | * // {
|
|---|
| 79 | * // f1: 1000,
|
|---|
| 80 | * // f2: 2000,
|
|---|
| 81 | * // f3: 3000
|
|---|
| 82 | * // }
|
|---|
| 83 | * }
|
|---|
| 84 | * });
|
|---|
| 85 | *
|
|---|
| 86 | * // Error handling
|
|---|
| 87 | * async.mapValues(withMissingFileMap, getFileSizeInBytes, function(err, result) {
|
|---|
| 88 | * if (err) {
|
|---|
| 89 | * console.log(err);
|
|---|
| 90 | * // [ Error: ENOENT: no such file or directory ]
|
|---|
| 91 | * } else {
|
|---|
| 92 | * console.log(result);
|
|---|
| 93 | * }
|
|---|
| 94 | * });
|
|---|
| 95 | *
|
|---|
| 96 | * // Using Promises
|
|---|
| 97 | * async.mapValues(fileMap, getFileSizeInBytes)
|
|---|
| 98 | * .then( result => {
|
|---|
| 99 | * console.log(result);
|
|---|
| 100 | * // result is now a map of file size in bytes for each file, e.g.
|
|---|
| 101 | * // {
|
|---|
| 102 | * // f1: 1000,
|
|---|
| 103 | * // f2: 2000,
|
|---|
| 104 | * // f3: 3000
|
|---|
| 105 | * // }
|
|---|
| 106 | * }).catch (err => {
|
|---|
| 107 | * console.log(err);
|
|---|
| 108 | * });
|
|---|
| 109 | *
|
|---|
| 110 | * // Error Handling
|
|---|
| 111 | * async.mapValues(withMissingFileMap, getFileSizeInBytes)
|
|---|
| 112 | * .then( result => {
|
|---|
| 113 | * console.log(result);
|
|---|
| 114 | * }).catch (err => {
|
|---|
| 115 | * console.log(err);
|
|---|
| 116 | * // [ Error: ENOENT: no such file or directory ]
|
|---|
| 117 | * });
|
|---|
| 118 | *
|
|---|
| 119 | * // Using async/await
|
|---|
| 120 | * async () => {
|
|---|
| 121 | * try {
|
|---|
| 122 | * let result = await async.mapValues(fileMap, getFileSizeInBytes);
|
|---|
| 123 | * console.log(result);
|
|---|
| 124 | * // result is now a map of file size in bytes for each file, e.g.
|
|---|
| 125 | * // {
|
|---|
| 126 | * // f1: 1000,
|
|---|
| 127 | * // f2: 2000,
|
|---|
| 128 | * // f3: 3000
|
|---|
| 129 | * // }
|
|---|
| 130 | * }
|
|---|
| 131 | * catch (err) {
|
|---|
| 132 | * console.log(err);
|
|---|
| 133 | * }
|
|---|
| 134 | * }
|
|---|
| 135 | *
|
|---|
| 136 | * // Error Handling
|
|---|
| 137 | * async () => {
|
|---|
| 138 | * try {
|
|---|
| 139 | * let result = await async.mapValues(withMissingFileMap, getFileSizeInBytes);
|
|---|
| 140 | * console.log(result);
|
|---|
| 141 | * }
|
|---|
| 142 | * catch (err) {
|
|---|
| 143 | * console.log(err);
|
|---|
| 144 | * // [ Error: ENOENT: no such file or directory ]
|
|---|
| 145 | * }
|
|---|
| 146 | * }
|
|---|
| 147 | *
|
|---|
| 148 | */
|
|---|
| 149 | function mapValues(obj, iteratee, callback) {
|
|---|
| 150 | return (0, _mapValuesLimit2.default)(obj, Infinity, iteratee, callback);
|
|---|
| 151 | }
|
|---|
| 152 | module.exports = exports.default; |
|---|