UNPKG

5.56 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = transform;
7
8var _eachOf = require('./eachOf.js');
9
10var _eachOf2 = _interopRequireDefault(_eachOf);
11
12var _once = require('./internal/once.js');
13
14var _once2 = _interopRequireDefault(_once);
15
16var _wrapAsync = require('./internal/wrapAsync.js');
17
18var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
19
20var _promiseCallback = require('./internal/promiseCallback.js');
21
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
24/**
25 * A relative of `reduce`. Takes an Object or Array, and iterates over each
26 * element in parallel, each step potentially mutating an `accumulator` value.
27 * The type of the accumulator defaults to the type of collection passed in.
28 *
29 * @name transform
30 * @static
31 * @memberOf module:Collections
32 * @method
33 * @category Collection
34 * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
35 * @param {*} [accumulator] - The initial state of the transform. If omitted,
36 * it will default to an empty Object or Array, depending on the type of `coll`
37 * @param {AsyncFunction} iteratee - A function applied to each item in the
38 * collection that potentially modifies the accumulator.
39 * Invoked with (accumulator, item, key, callback).
40 * @param {Function} [callback] - A callback which is called after all the
41 * `iteratee` functions have finished. Result is the transformed accumulator.
42 * Invoked with (err, result).
43 * @returns {Promise} a promise, if no callback provided
44 * @example
45 *
46 * // file1.txt is a file that is 1000 bytes in size
47 * // file2.txt is a file that is 2000 bytes in size
48 * // file3.txt is a file that is 3000 bytes in size
49 *
50 * // helper function that returns human-readable size format from bytes
51 * function formatBytes(bytes, decimals = 2) {
52 * // implementation not included for brevity
53 * return humanReadbleFilesize;
54 * }
55 *
56 * const fileList = ['file1.txt','file2.txt','file3.txt'];
57 *
58 * // asynchronous function that returns the file size, transformed to human-readable format
59 * // e.g. 1024 bytes = 1KB, 1234 bytes = 1.21 KB, 1048576 bytes = 1MB, etc.
60 * function transformFileSize(acc, value, key, callback) {
61 * fs.stat(value, function(err, stat) {
62 * if (err) {
63 * return callback(err);
64 * }
65 * acc[key] = formatBytes(stat.size);
66 * callback(null);
67 * });
68 * }
69 *
70 * // Using callbacks
71 * async.transform(fileList, transformFileSize, function(err, result) {
72 * if(err) {
73 * console.log(err);
74 * } else {
75 * console.log(result);
76 * // [ '1000 Bytes', '1.95 KB', '2.93 KB' ]
77 * }
78 * });
79 *
80 * // Using Promises
81 * async.transform(fileList, transformFileSize)
82 * .then(result => {
83 * console.log(result);
84 * // [ '1000 Bytes', '1.95 KB', '2.93 KB' ]
85 * }).catch(err => {
86 * console.log(err);
87 * });
88 *
89 * // Using async/await
90 * (async () => {
91 * try {
92 * let result = await async.transform(fileList, transformFileSize);
93 * console.log(result);
94 * // [ '1000 Bytes', '1.95 KB', '2.93 KB' ]
95 * }
96 * catch (err) {
97 * console.log(err);
98 * }
99 * })();
100 *
101 * @example
102 *
103 * // file1.txt is a file that is 1000 bytes in size
104 * // file2.txt is a file that is 2000 bytes in size
105 * // file3.txt is a file that is 3000 bytes in size
106 *
107 * // helper function that returns human-readable size format from bytes
108 * function formatBytes(bytes, decimals = 2) {
109 * // implementation not included for brevity
110 * return humanReadbleFilesize;
111 * }
112 *
113 * const fileMap = { f1: 'file1.txt', f2: 'file2.txt', f3: 'file3.txt' };
114 *
115 * // asynchronous function that returns the file size, transformed to human-readable format
116 * // e.g. 1024 bytes = 1KB, 1234 bytes = 1.21 KB, 1048576 bytes = 1MB, etc.
117 * function transformFileSize(acc, value, key, callback) {
118 * fs.stat(value, function(err, stat) {
119 * if (err) {
120 * return callback(err);
121 * }
122 * acc[key] = formatBytes(stat.size);
123 * callback(null);
124 * });
125 * }
126 *
127 * // Using callbacks
128 * async.transform(fileMap, transformFileSize, function(err, result) {
129 * if(err) {
130 * console.log(err);
131 * } else {
132 * console.log(result);
133 * // { f1: '1000 Bytes', f2: '1.95 KB', f3: '2.93 KB' }
134 * }
135 * });
136 *
137 * // Using Promises
138 * async.transform(fileMap, transformFileSize)
139 * .then(result => {
140 * console.log(result);
141 * // { f1: '1000 Bytes', f2: '1.95 KB', f3: '2.93 KB' }
142 * }).catch(err => {
143 * console.log(err);
144 * });
145 *
146 * // Using async/await
147 * async () => {
148 * try {
149 * let result = await async.transform(fileMap, transformFileSize);
150 * console.log(result);
151 * // { f1: '1000 Bytes', f2: '1.95 KB', f3: '2.93 KB' }
152 * }
153 * catch (err) {
154 * console.log(err);
155 * }
156 * }
157 *
158 */
159function transform(coll, accumulator, iteratee, callback) {
160 if (arguments.length <= 3 && typeof accumulator === 'function') {
161 callback = iteratee;
162 iteratee = accumulator;
163 accumulator = Array.isArray(coll) ? [] : {};
164 }
165 callback = (0, _once2.default)(callback || (0, _promiseCallback.promiseCallback)());
166 var _iteratee = (0, _wrapAsync2.default)(iteratee);
167
168 (0, _eachOf2.default)(coll, (v, k, cb) => {
169 _iteratee(accumulator, v, k, cb);
170 }, err => callback(err, accumulator));
171 return callback[_promiseCallback.PROMISE_SYMBOL];
172}
173module.exports = exports.default;
\No newline at end of file