e80d1ee512dc3437017a034ce51d5330aace5976
[yaffs-website] / node_modules / grunt-uncss / node_modules / async / transform.js
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 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
26 /**
27  * A relative of `reduce`.  Takes an Object or Array, and iterates over each
28  * element in series, each step potentially mutating an `accumulator` value.
29  * The type of the accumulator defaults to the type of collection passed in.
30  *
31  * @name transform
32  * @static
33  * @memberOf module:Collections
34  * @method
35  * @category Collection
36  * @param {Array|Iterable|Object} coll - A collection to iterate over.
37  * @param {*} [accumulator] - The initial state of the transform.  If omitted,
38  * it will default to an empty Object or Array, depending on the type of `coll`
39  * @param {Function} iteratee - A function applied to each item in the
40  * collection that potentially modifies the accumulator. The `iteratee` is
41  * passed a `callback(err)` which accepts an optional error as its first
42  * argument. If an error is passed to the callback, the transform is stopped
43  * and the main `callback` is immediately called with the error.
44  * Invoked with (accumulator, item, key, callback).
45  * @param {Function} [callback] - A callback which is called after all the
46  * `iteratee` functions have finished. Result is the transformed accumulator.
47  * Invoked with (err, result).
48  * @example
49  *
50  * async.transform([1,2,3], function(acc, item, index, callback) {
51  *     // pointless async:
52  *     process.nextTick(function() {
53  *         acc.push(item * 2)
54  *         callback(null)
55  *     });
56  * }, function(err, result) {
57  *     // result is now equal to [2, 4, 6]
58  * });
59  *
60  * @example
61  *
62  * async.transform({a: 1, b: 2, c: 3}, function (obj, val, key, callback) {
63  *     setImmediate(function () {
64  *         obj[key] = val * 2;
65  *         callback();
66  *     })
67  * }, function (err, result) {
68  *     // result is equal to {a: 2, b: 4, c: 6}
69  * })
70  */
71 function transform(coll, accumulator, iteratee, callback) {
72     if (arguments.length === 3) {
73         callback = iteratee;
74         iteratee = accumulator;
75         accumulator = (0, _isArray2.default)(coll) ? [] : {};
76     }
77     callback = (0, _once2.default)(callback || _noop2.default);
78
79     (0, _eachOf2.default)(coll, function (v, k, cb) {
80         iteratee(accumulator, v, k, cb);
81     }, function (err) {
82         callback(err, accumulator);
83     });
84 }
85 module.exports = exports['default'];