[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = transform;
|
---|
| 7 |
|
---|
| 8 | var _isArray = require('lodash/isArray');
|
---|
| 9 |
|
---|
| 10 | var _isArray2 = _interopRequireDefault(_isArray);
|
---|
| 11 |
|
---|
| 12 | var _noop = require('lodash/noop');
|
---|
| 13 |
|
---|
| 14 | var _noop2 = _interopRequireDefault(_noop);
|
---|
| 15 |
|
---|
| 16 | var _eachOf = require('./eachOf');
|
---|
| 17 |
|
---|
| 18 | var _eachOf2 = _interopRequireDefault(_eachOf);
|
---|
| 19 |
|
---|
| 20 | var _once = require('./internal/once');
|
---|
| 21 |
|
---|
| 22 | var _once2 = _interopRequireDefault(_once);
|
---|
| 23 |
|
---|
| 24 | var _wrapAsync = require('./internal/wrapAsync');
|
---|
| 25 |
|
---|
| 26 | var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
---|
| 27 |
|
---|
| 28 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * A relative of `reduce`. Takes an Object or Array, and iterates over each
|
---|
| 32 | * element in series, each step potentially mutating an `accumulator` value.
|
---|
| 33 | * The type of the accumulator defaults to the type of collection passed in.
|
---|
| 34 | *
|
---|
| 35 | * @name transform
|
---|
| 36 | * @static
|
---|
| 37 | * @memberOf module:Collections
|
---|
| 38 | * @method
|
---|
| 39 | * @category Collection
|
---|
| 40 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
---|
| 41 | * @param {*} [accumulator] - The initial state of the transform. If omitted,
|
---|
| 42 | * it will default to an empty Object or Array, depending on the type of `coll`
|
---|
| 43 | * @param {AsyncFunction} iteratee - A function applied to each item in the
|
---|
| 44 | * collection that potentially modifies the accumulator.
|
---|
| 45 | * Invoked with (accumulator, item, key, callback).
|
---|
| 46 | * @param {Function} [callback] - A callback which is called after all the
|
---|
| 47 | * `iteratee` functions have finished. Result is the transformed accumulator.
|
---|
| 48 | * Invoked with (err, result).
|
---|
| 49 | * @example
|
---|
| 50 | *
|
---|
| 51 | * async.transform([1,2,3], function(acc, item, index, callback) {
|
---|
| 52 | * // pointless async:
|
---|
| 53 | * process.nextTick(function() {
|
---|
| 54 | * acc.push(item * 2)
|
---|
| 55 | * callback(null)
|
---|
| 56 | * });
|
---|
| 57 | * }, function(err, result) {
|
---|
| 58 | * // result is now equal to [2, 4, 6]
|
---|
| 59 | * });
|
---|
| 60 | *
|
---|
| 61 | * @example
|
---|
| 62 | *
|
---|
| 63 | * async.transform({a: 1, b: 2, c: 3}, function (obj, val, key, callback) {
|
---|
| 64 | * setImmediate(function () {
|
---|
| 65 | * obj[key] = val * 2;
|
---|
| 66 | * callback();
|
---|
| 67 | * })
|
---|
| 68 | * }, function (err, result) {
|
---|
| 69 | * // result is equal to {a: 2, b: 4, c: 6}
|
---|
| 70 | * })
|
---|
| 71 | */
|
---|
| 72 | function transform(coll, accumulator, iteratee, callback) {
|
---|
| 73 | if (arguments.length <= 3) {
|
---|
| 74 | callback = iteratee;
|
---|
| 75 | iteratee = accumulator;
|
---|
| 76 | accumulator = (0, _isArray2.default)(coll) ? [] : {};
|
---|
| 77 | }
|
---|
| 78 | callback = (0, _once2.default)(callback || _noop2.default);
|
---|
| 79 | var _iteratee = (0, _wrapAsync2.default)(iteratee);
|
---|
| 80 |
|
---|
| 81 | (0, _eachOf2.default)(coll, function (v, k, cb) {
|
---|
| 82 | _iteratee(accumulator, v, k, cb);
|
---|
| 83 | }, function (err) {
|
---|
| 84 | callback(err, accumulator);
|
---|
| 85 | });
|
---|
| 86 | }
|
---|
| 87 | module.exports = exports['default']; |
---|