Skip to content

Commit ce1c417

Browse files
committed
greatest, least, greatestIndex and leastIndex ignore nulls
closes #208
1 parent 6fda277 commit ce1c417

8 files changed

+50
-12
lines changed

src/greatest.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ export default function greatest(values, compare = ascending) {
77
let maxValue;
88
for (const element of values) {
99
const value = compare(element);
10-
if (defined
10+
if ((defined
1111
? ascending(value, maxValue) > 0
12-
: ascending(value, value) === 0) {
12+
: ascending(value, value) === 0
13+
) && value !== null) {
1314
max = element;
1415
maxValue = value;
1516
defined = true;
1617
}
1718
}
1819
} else {
1920
for (const value of values) {
20-
if (defined
21+
if ((defined
2122
? compare(value, max) > 0
22-
: compare(value, value) === 0) {
23+
: compare(value, value) === 0
24+
) && value !== null) {
2325
max = value;
2426
defined = true;
2527
}

src/greatestIndex.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ export default function greatestIndex(values, compare = ascending) {
88
let index = -1;
99
for (const value of values) {
1010
++index;
11-
if (max < 0
11+
if ((max < 0
1212
? compare(value, value) === 0
13-
: compare(value, maxValue) > 0) {
13+
: compare(value, maxValue) > 0
14+
) && value !== null) {
1415
maxValue = value;
1516
max = index;
1617
}

src/least.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ export default function least(values, compare = ascending) {
77
let minValue;
88
for (const element of values) {
99
const value = compare(element);
10-
if (defined
10+
if ((defined
1111
? ascending(value, minValue) < 0
12-
: ascending(value, value) === 0) {
12+
: ascending(value, value) === 0
13+
) && value !== null) {
1314
min = element;
1415
minValue = value;
1516
defined = true;
1617
}
1718
}
1819
} else {
1920
for (const value of values) {
20-
if (defined
21+
if ((defined
2122
? compare(value, min) < 0
22-
: compare(value, value) === 0) {
23+
: compare(value, value) === 0
24+
) && value !== null) {
2325
min = value;
2426
defined = true;
2527
}

src/leastIndex.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ export default function leastIndex(values, compare = ascending) {
88
let index = -1;
99
for (const value of values) {
1010
++index;
11-
if (min < 0
11+
if ((min < 0
1212
? compare(value, value) === 0
13-
: compare(value, minValue) < 0) {
13+
: compare(value, minValue) < 0
14+
) && value !== null) {
1415
minValue = value;
1516
min = index;
1617
}

test/greatest-test.js

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ tape("greatest(array) returns the first of equal values", (test) => {
4646
test.deepEqual(d3.greatest([3, 2, 2, 1, 1, 0, 0, 0, 3, 0].map(box), ascendingValue), {value: 3, index: 0});
4747
});
4848

49+
tape("greatest(array) ignores nulls", (test) => {
50+
test.deepEqual(d3.greatest([null, -2, null]), -2);
51+
});
52+
53+
tape("greatest(array, accessor) ignores nulls", (test) => {
54+
test.deepEqual(d3.greatest([null, -2, null], d => d), -2);
55+
});
56+
4957
function box(value, index) {
5058
return {value, index};
5159
}

test/greatestIndex-test.js

+8
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,11 @@ tape("greatestIndex(array) returns the first of equal values", (test) => {
4545
test.strictEqual(d3.greatestIndex([-2, -2, -1, -1, 0, 0, 0, -3, 0]), 4);
4646
test.strictEqual(d3.greatestIndex([-3, -2, -2, -1, -1, 0, 0, 0, -3, 0], d3.descending), 0);
4747
});
48+
49+
tape("greatestIndex(array) ignores nulls", (test) => {
50+
test.deepEqual(d3.greatestIndex([null, -2, null]), 1);
51+
});
52+
53+
tape("greatestIndex(array, accessor) ignores nulls", (test) => {
54+
test.deepEqual(d3.greatestIndex([null, -2, null], d => d), 1);
55+
});

test/least-test.js

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ tape("least(array) returns the first of equal values", (test) => {
4646
test.deepEqual(d3.least([3, 2, 2, 1, 1, 0, 0, 0, 3, 0].map(box), descendingValue), {value: 3, index: 0});
4747
});
4848

49+
tape("least(array) ignores nulls", (test) => {
50+
test.deepEqual(d3.least([null, 2, null]), 2);
51+
});
52+
53+
tape("least(array, accessor) ignores nulls", (test) => {
54+
test.deepEqual(d3.least([null, 2, null], d => d), 2);
55+
});
56+
4957
function box(value, index) {
5058
return {value, index};
5159
}

test/leastIndex-test.js

+8
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,11 @@ tape("leastIndex(array) returns the first of equal values", (test) => {
4545
test.strictEqual(d3.leastIndex([2, 2, 1, 1, 0, 0, 0, 3, 0]), 4);
4646
test.strictEqual(d3.leastIndex([3, 2, 2, 1, 1, 0, 0, 0, 3, 0], d3.descending), 0);
4747
});
48+
49+
tape("leastIndex(array) ignores nulls", (test) => {
50+
test.deepEqual(d3.leastIndex([null, 2, null]), 1);
51+
});
52+
53+
tape("leastIndex(array, accessor) ignores nulls", (test) => {
54+
test.deepEqual(d3.leastIndex([null, 2, null], d => d), 1);
55+
});

0 commit comments

Comments
 (0)