1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _map2 = require('./internal/map.js');
|
8 |
|
9 | var _map3 = _interopRequireDefault(_map2);
|
10 |
|
11 | var _eachOf = require('./eachOf.js');
|
12 |
|
13 | var _eachOf2 = _interopRequireDefault(_eachOf);
|
14 |
|
15 | var _awaitify = require('./internal/awaitify.js');
|
16 |
|
17 | var _awaitify2 = _interopRequireDefault(_awaitify);
|
18 |
|
19 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
20 |
|
21 | /**
|
22 | * Produces a new collection of values by mapping each value in `coll` through
|
23 | * the `iteratee` function. The `iteratee` is called with an item from `coll`
|
24 | * and a callback for when it has finished processing. Each of these callbacks
|
25 | * takes 2 arguments: an `error`, and the transformed item from `coll`. If
|
26 | * `iteratee` passes an error to its callback, the main `callback` (for the
|
27 | * `map` function) is immediately called with the error.
|
28 | *
|
29 | * Note, that since this function applies the `iteratee` to each item in
|
30 | * parallel, there is no guarantee that the `iteratee` functions will complete
|
31 | * in order. However, the results array will be in the same order as the
|
32 | * original `coll`.
|
33 | *
|
34 | * If `map` is passed an Object, the results will be an Array. The results
|
35 | * will roughly be in the order of the original Objects' keys (but this can
|
36 | * vary across JavaScript engines).
|
37 | *
|
38 | * @name map
|
39 | * @static
|
40 | * @memberOf module:Collections
|
41 | * @method
|
42 | * @category Collection
|
43 | * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
44 | * @param {AsyncFunction} iteratee - An async function to apply to each item in
|
45 | * `coll`.
|
46 | * The iteratee should complete with the transformed item.
|
47 | * Invoked with (item, callback).
|
48 | * @param {Function} [callback] - A callback which is called when all `iteratee`
|
49 | * functions have finished, or an error occurs. Results is an Array of the
|
50 | * transformed items from the `coll`. Invoked with (err, results).
|
51 | * @returns {Promise} a promise, if no callback is passed
|
52 | * @example
|
53 | *
|
54 | * // file1.txt is a file that is 1000 bytes in size
|
55 | * // file2.txt is a file that is 2000 bytes in size
|
56 | * // file3.txt is a file that is 3000 bytes in size
|
57 | * // file4.txt does not exist
|
58 | *
|
59 | * const fileList = ['file1.txt','file2.txt','file3.txt'];
|
60 | * const withMissingFileList = ['file1.txt','file2.txt','file4.txt'];
|
61 | *
|
62 | * // asynchronous function that returns the file size in bytes
|
63 | * function getFileSizeInBytes(file, callback) {
|
64 | * fs.stat(file, function(err, stat) {
|
65 | * if (err) {
|
66 | * return callback(err);
|
67 | * }
|
68 | * callback(null, stat.size);
|
69 | * });
|
70 | * }
|
71 | *
|
72 | * // Using callbacks
|
73 | * async.map(fileList, getFileSizeInBytes, function(err, results) {
|
74 | * if (err) {
|
75 | * console.log(err);
|
76 | * } else {
|
77 | * console.log(results);
|
78 | * // results is now an array of the file size in bytes for each file, e.g.
|
79 | * // [ 1000, 2000, 3000]
|
80 | * }
|
81 | * });
|
82 | *
|
83 | * // Error Handling
|
84 | * async.map(withMissingFileList, getFileSizeInBytes, function(err, results) {
|
85 | * if (err) {
|
86 | * console.log(err);
|
87 | * // [ Error: ENOENT: no such file or directory ]
|
88 | * } else {
|
89 | * console.log(results);
|
90 | * }
|
91 | * });
|
92 | *
|
93 | * // Using Promises
|
94 | * async.map(fileList, getFileSizeInBytes)
|
95 | * .then( results => {
|
96 | * console.log(results);
|
97 | * // results is now an array of the file size in bytes for each file, e.g.
|
98 | * // [ 1000, 2000, 3000]
|
99 | * }).catch( err => {
|
100 | * console.log(err);
|
101 | * });
|
102 | *
|
103 | * // Error Handling
|
104 | * async.map(withMissingFileList, getFileSizeInBytes)
|
105 | * .then( results => {
|
106 | * console.log(results);
|
107 | * }).catch( err => {
|
108 | * console.log(err);
|
109 | * // [ Error: ENOENT: no such file or directory ]
|
110 | * });
|
111 | *
|
112 | * // Using async/await
|
113 | * async () => {
|
114 | * try {
|
115 | * let results = await async.map(fileList, getFileSizeInBytes);
|
116 | * console.log(results);
|
117 | * // results is now an array of the file size in bytes for each file, e.g.
|
118 | * // [ 1000, 2000, 3000]
|
119 | * }
|
120 | * catch (err) {
|
121 | * console.log(err);
|
122 | * }
|
123 | * }
|
124 | *
|
125 | * // Error Handling
|
126 | * async () => {
|
127 | * try {
|
128 | * let results = await async.map(withMissingFileList, getFileSizeInBytes);
|
129 | * console.log(results);
|
130 | * }
|
131 | * catch (err) {
|
132 | * console.log(err);
|
133 | * // [ Error: ENOENT: no such file or directory ]
|
134 | * }
|
135 | * }
|
136 | *
|
137 | */
|
138 | function map(coll, iteratee, callback) {
|
139 | return (0, _map3.default)(_eachOf2.default, coll, iteratee, callback);
|
140 | }
|
141 | exports.default = (0, _awaitify2.default)(map, 3);
|
142 | module.exports = exports.default; |
\ | No newline at end of file |