Skip to content

Commit 6f76064

Browse files
DzyubSpiritnechaido
authored andcommitted
Fix reduce
* Fix invalid behavior when an empty array with no initial value is passed * Rename error message constant to better represent its meaning * Add missing documentation PR-URL: #406
1 parent a08758f commit 6f76064

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

lib/array.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ const filter = (items, fn, done) => {
151151
}
152152
};
153153

154-
const EMPTY_ARR = 'Metasync: reduce of empty array with no initial value';
154+
const REDUCE_EMPTY_ARR =
155+
'Metasync: reduce of empty array with no initial value';
155156

156157
// Asynchronous reduce
157158
// items - <Array>, incoming
@@ -160,6 +161,8 @@ const EMPTY_ARR = 'Metasync: reduce of empty array with no initial value';
160161
// current - <any>, current element being processed in the array
161162
// callback - <Function>, callback for returning value
162163
// back to reduce function
164+
// err - <Error> | <null>
165+
// data - <any>, resulting value
163166
// counter - <number>, index of the current element
164167
// being processed in array
165168
// items - <Array>, the array reduce was called upon
@@ -171,15 +174,20 @@ const EMPTY_ARR = 'Metasync: reduce of empty array with no initial value';
171174
const reduce = (items, fn, done, initial) => {
172175
done = done || common.emptyness;
173176
const len = items.length;
174-
let count = typeof initial === 'undefined' ? 1 : 0;
177+
const hasInitial = typeof initial !== 'undefined';
175178

176-
if (!len) {
177-
const err = count ? new TypeError(EMPTY_ARR) : null;
178-
done(err, initial);
179+
if (len === 0 && !hasInitial) {
180+
done(new TypeError(REDUCE_EMPTY_ARR), initial);
181+
return;
182+
}
183+
184+
let previous = hasInitial ? initial : items[0];
185+
if ((len === 0 && hasInitial) || (len === 1 && !hasInitial)) {
186+
done(null, previous);
179187
return;
180188
}
181189

182-
let previous = count === 1 ? items[0] : initial;
190+
let count = hasInitial ? 0 : 1;
183191
let current = items[count];
184192
const last = len - 1;
185193

test/array.reduce.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const metasync = require('..');
44
const metatests = require('metatests');
55

6-
metatests.test('successful with initial', test => {
6+
metatests.test('reduce with initial', test => {
77
const arr = [1, 2, 3, 4, 5];
88
const initial = 10;
99
const expectedRes = 25;
@@ -54,7 +54,21 @@ metatests.test('reduce without initial and with empty array', test => {
5454
);
5555
});
5656

57-
metatests.test('successful without initial', test => {
57+
metatests.test('reduce without initial and with single-element array', test => {
58+
const arr = [2];
59+
60+
metasync.reduce(
61+
arr,
62+
(prev, cur, callback) => process.nextTick(() => callback(null, prev + cur)),
63+
(err, res) => {
64+
test.strictSame(err, null);
65+
test.strictSame(res, 2);
66+
test.end();
67+
}
68+
);
69+
});
70+
71+
metatests.test('reduce without initial', test => {
5872
const arr = [1, 2, 3, 4, 5];
5973
const expectedRes = 15;
6074

@@ -69,7 +83,7 @@ metatests.test('successful without initial', test => {
6983
);
7084
});
7185

72-
metatests.test('successful with asymetric function', test => {
86+
metatests.test('reduce with asymetric function', test => {
7387
const arr = '10110011';
7488
const expectedRes = 179;
7589

@@ -85,7 +99,7 @@ metatests.test('successful with asymetric function', test => {
8599
);
86100
});
87101

88-
metatests.test('with error', test => {
102+
metatests.test('reduce with error', test => {
89103
const arr = '10120011';
90104
const reduceError = new Error('Reduce error');
91105

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy