1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _isArrayLike = require('./internal/isArrayLike.js');
|
8 |
|
9 | var _isArrayLike2 = _interopRequireDefault(_isArrayLike);
|
10 |
|
11 | var _breakLoop = require('./internal/breakLoop.js');
|
12 |
|
13 | var _breakLoop2 = _interopRequireDefault(_breakLoop);
|
14 |
|
15 | var _eachOfLimit = require('./eachOfLimit.js');
|
16 |
|
17 | var _eachOfLimit2 = _interopRequireDefault(_eachOfLimit);
|
18 |
|
19 | var _once = require('./internal/once.js');
|
20 |
|
21 | var _once2 = _interopRequireDefault(_once);
|
22 |
|
23 | var _onlyOnce = require('./internal/onlyOnce.js');
|
24 |
|
25 | var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
|
26 |
|
27 | var _wrapAsync = require('./internal/wrapAsync.js');
|
28 |
|
29 | var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
30 |
|
31 | var _awaitify = require('./internal/awaitify.js');
|
32 |
|
33 | var _awaitify2 = _interopRequireDefault(_awaitify);
|
34 |
|
35 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
36 |
|
37 | // eachOf implementation optimized for array-likes
|
38 | function eachOfArrayLike(coll, iteratee, callback) {
|
39 | callback = (0, _once2.default)(callback);
|
40 | var index = 0,
|
41 | completed = 0,
|
42 | { length } = coll,
|
43 | canceled = false;
|
44 | if (length === 0) {
|
45 | callback(null);
|
46 | }
|
47 |
|
48 | function iteratorCallback(err, value) {
|
49 | if (err === false) {
|
50 | canceled = true;
|
51 | }
|
52 | if (canceled === true) return;
|
53 | if (err) {
|
54 | callback(err);
|
55 | } else if (++completed === length || value === _breakLoop2.default) {
|
56 | callback(null);
|
57 | }
|
58 | }
|
59 |
|
60 | for (; index < length; index++) {
|
61 | iteratee(coll[index], index, (0, _onlyOnce2.default)(iteratorCallback));
|
62 | }
|
63 | }
|
64 |
|
65 | // a generic version of eachOf which can handle array, object, and iterator cases.
|
66 | function eachOfGeneric(coll, iteratee, callback) {
|
67 | return (0, _eachOfLimit2.default)(coll, Infinity, iteratee, callback);
|
68 | }
|
69 |
|
70 | /**
|
71 | * Like [`each`]{@link module:Collections.each}, except that it passes the key (or index) as the second argument
|
72 | * to the iteratee.
|
73 | *
|
74 | * @name eachOf
|
75 | * @static
|
76 | * @memberOf module:Collections
|
77 | * @method
|
78 | * @alias forEachOf
|
79 | * @category Collection
|
80 | * @see [async.each]{@link module:Collections.each}
|
81 | * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
82 | * @param {AsyncFunction} iteratee - A function to apply to each
|
83 | * item in `coll`.
|
84 | * The `key` is the item's key, or index in the case of an array.
|
85 | * Invoked with (item, key, callback).
|
86 | * @param {Function} [callback] - A callback which is called when all
|
87 | * `iteratee` functions have finished, or an error occurs. Invoked with (err).
|
88 | * @returns {Promise} a promise, if a callback is omitted
|
89 | * @example
|
90 | *
|
91 | * // dev.json is a file containing a valid json object config for dev environment
|
92 | * // dev.json is a file containing a valid json object config for test environment
|
93 | * // prod.json is a file containing a valid json object config for prod environment
|
94 | * // invalid.json is a file with a malformed json object
|
95 | *
|
96 | * let configs = {}; //global variable
|
97 | * let validConfigFileMap = {dev: 'dev.json', test: 'test.json', prod: 'prod.json'};
|
98 | * let invalidConfigFileMap = {dev: 'dev.json', test: 'test.json', invalid: 'invalid.json'};
|
99 | *
|
100 | * // asynchronous function that reads a json file and parses the contents as json object
|
101 | * function parseFile(file, key, callback) {
|
102 | * fs.readFile(file, "utf8", function(err, data) {
|
103 | * if (err) return calback(err);
|
104 | * try {
|
105 | * configs[key] = JSON.parse(data);
|
106 | * } catch (e) {
|
107 | * return callback(e);
|
108 | * }
|
109 | * callback();
|
110 | * });
|
111 | * }
|
112 | *
|
113 | * // Using callbacks
|
114 | * async.forEachOf(validConfigFileMap, parseFile, function (err) {
|
115 | * if (err) {
|
116 | * console.error(err);
|
117 | * } else {
|
118 | * console.log(configs);
|
119 | * // configs is now a map of JSON data, e.g.
|
120 | * // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json}
|
121 | * }
|
122 | * });
|
123 | *
|
124 | * //Error handing
|
125 | * async.forEachOf(invalidConfigFileMap, parseFile, function (err) {
|
126 | * if (err) {
|
127 | * console.error(err);
|
128 | * // JSON parse error exception
|
129 | * } else {
|
130 | * console.log(configs);
|
131 | * }
|
132 | * });
|
133 | *
|
134 | * // Using Promises
|
135 | * async.forEachOf(validConfigFileMap, parseFile)
|
136 | * .then( () => {
|
137 | * console.log(configs);
|
138 | * // configs is now a map of JSON data, e.g.
|
139 | * // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json}
|
140 | * }).catch( err => {
|
141 | * console.error(err);
|
142 | * });
|
143 | *
|
144 | * //Error handing
|
145 | * async.forEachOf(invalidConfigFileMap, parseFile)
|
146 | * .then( () => {
|
147 | * console.log(configs);
|
148 | * }).catch( err => {
|
149 | * console.error(err);
|
150 | * // JSON parse error exception
|
151 | * });
|
152 | *
|
153 | * // Using async/await
|
154 | * async () => {
|
155 | * try {
|
156 | * let result = await async.forEachOf(validConfigFileMap, parseFile);
|
157 | * console.log(configs);
|
158 | * // configs is now a map of JSON data, e.g.
|
159 | * // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json}
|
160 | * }
|
161 | * catch (err) {
|
162 | * console.log(err);
|
163 | * }
|
164 | * }
|
165 | *
|
166 | * //Error handing
|
167 | * async () => {
|
168 | * try {
|
169 | * let result = await async.forEachOf(invalidConfigFileMap, parseFile);
|
170 | * console.log(configs);
|
171 | * }
|
172 | * catch (err) {
|
173 | * console.log(err);
|
174 | * // JSON parse error exception
|
175 | * }
|
176 | * }
|
177 | *
|
178 | */
|
179 | function eachOf(coll, iteratee, callback) {
|
180 | var eachOfImplementation = (0, _isArrayLike2.default)(coll) ? eachOfArrayLike : eachOfGeneric;
|
181 | return eachOfImplementation(coll, (0, _wrapAsync2.default)(iteratee), callback);
|
182 | }
|
183 |
|
184 | exports.default = (0, _awaitify2.default)(eachOf, 3);
|
185 | module.exports = exports.default; |
\ | No newline at end of file |