Skip to content

Commit

Permalink
don’t consider null comparable
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jun 5, 2021
1 parent ef6f165 commit dd844b8
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 36 deletions.
6 changes: 5 additions & 1 deletion src/ascending.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export default function(a, b) {
return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
return a == null || b == null ? NaN
: a < b ? -1
: a > b ? 1
: a >= b ? 0
: NaN;
}
6 changes: 5 additions & 1 deletion src/descending.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export default function(a, b) {
return b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN;
return a == null || b == null ? NaN
: b < a ? -1
: b > a ? 1
: b >= a ? 0
: NaN;
}
10 changes: 4 additions & 6 deletions src/greatest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ export default function greatest(values, compare = ascending) {
let maxValue;
for (const element of values) {
const value = compare(element);
if (value != null && (defined
if (defined
? ascending(value, maxValue) > 0
: ascending(value, value) === 0
)) {
: ascending(value, value) === 0) {
max = element;
maxValue = value;
defined = true;
}
}
} else {
for (const value of values) {
if (value != null && (defined
if (defined
? compare(value, max) > 0
: compare(value, value) === 0
)) {
: compare(value, value) === 0) {
max = value;
defined = true;
}
Expand Down
5 changes: 2 additions & 3 deletions src/greatestIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ export default function greatestIndex(values, compare = ascending) {
let index = -1;
for (const value of values) {
++index;
if (value != null && (max < 0
if (max < 0
? compare(value, value) === 0
: compare(value, maxValue) > 0
)) {
: compare(value, maxValue) > 0) {
maxValue = value;
max = index;
}
Expand Down
10 changes: 4 additions & 6 deletions src/least.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ export default function least(values, compare = ascending) {
let minValue;
for (const element of values) {
const value = compare(element);
if (value != null && (defined
if (defined
? ascending(value, minValue) < 0
: ascending(value, value) === 0
)) {
: ascending(value, value) === 0) {
min = element;
minValue = value;
defined = true;
}
}
} else {
for (const value of values) {
if (value != null && (defined
if (defined
? compare(value, min) < 0
: compare(value, value) === 0
)) {
: compare(value, value) === 0) {
min = value;
defined = true;
}
Expand Down
5 changes: 2 additions & 3 deletions src/leastIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ export default function leastIndex(values, compare = ascending) {
let index = -1;
for (const value of values) {
++index;
if (value != null && (min < 0
if (min < 0
? compare(value, value) === 0
: compare(value, minValue) < 0
)) {
: compare(value, minValue) < 0) {
minValue = value;
min = index;
}
Expand Down
8 changes: 4 additions & 4 deletions test/greatest-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ it("greatest(array) returns the first of equal values", () => {
assert.deepStrictEqual(greatest([3, 2, 2, 1, 1, 0, 0, 0, 3, 0].map(box), ascendingValue), {value: 3, index: 0});
});

tape("greatest(array) ignores null and undefined", (test) => {
test.deepEqual(d3.greatest([null, -2, undefined]), -2);
it("greatest(array) ignores null and undefined", () => {
assert.deepStrictEqual(greatest([null, -2, undefined]), -2);
});

tape("greatest(array, accessor) ignores null and undefined", (test) => {
test.deepEqual(d3.greatest([null, -2, undefined], d => d), -2);
it("greatest(array, accessor) ignores null and undefined", () => {
assert.deepStrictEqual(greatest([null, -2, undefined], d => d), -2);
});

function box(value, index) {
Expand Down
8 changes: 4 additions & 4 deletions test/greatestIndex-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ it("greatestIndex(array) returns the first of equal values", () => {
assert.strictEqual(greatestIndex([-3, -2, -2, -1, -1, 0, 0, 0, -3, 0], descending), 0);
});

tape("greatestIndex(array) ignores null and undefined", (test) => {
test.deepEqual(d3.greatestIndex([null, -2, undefined]), 1);
it("greatestIndex(array) ignores null and undefined", () => {
assert.deepStrictEqual(greatestIndex([null, -2, undefined]), 1);
});

tape("greatestIndex(array, accessor) ignores null and undefined", (test) => {
test.deepEqual(d3.greatestIndex([null, -2, undefined], d => d), 1);
it("greatestIndex(array, accessor) ignores null and undefined", () => {
assert.deepStrictEqual(greatestIndex([null, -2, undefined], d => d), 1);
});
8 changes: 4 additions & 4 deletions test/least-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ it("least(array) returns the first of equal values", () => {
assert.deepStrictEqual(least([3, 2, 2, 1, 1, 0, 0, 0, 3, 0].map(box), descendingValue), {value: 3, index: 0});
});

tape("least(array) ignores null and undefined", (test) => {
test.deepEqual(d3.least([null, 2, undefined]), 2);
it("least(array) ignores null and undefined", () => {
assert.deepStrictEqual(least([null, 2, undefined]), 2);
});

tape("least(array, accessor) ignores null and undefined", (test) => {
test.deepEqual(d3.least([null, 2, undefined], d => d), 2);
it("least(array, accessor) ignores null and undefined", () => {
assert.deepStrictEqual(least([null, 2, undefined], d => d), 2);
});

function box(value, index) {
Expand Down
8 changes: 4 additions & 4 deletions test/leastIndex-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ it("leastIndex(array) returns the first of equal values", () => {
assert.strictEqual(leastIndex([3, 2, 2, 1, 1, 0, 0, 0, 3, 0], descending), 0);
});

tape("leastIndex(array) ignores null and undefined", (test) => {
test.deepEqual(d3.leastIndex([null, 2, undefined]), 1);
it("leastIndex(array) ignores null and undefined", () => {
assert.deepStrictEqual(leastIndex([null, 2, undefined]), 1);
});

tape("leastIndex(array, accessor) ignores null and undefined", (test) => {
test.deepEqual(d3.leastIndex([null, 2, undefined], d => d), 1);
it("leastIndex(array, accessor) ignores null and undefined", () => {
assert.deepStrictEqual(leastIndex([null, 2, undefined], d => d), 1);
});

0 comments on commit dd844b8

Please sign in to comment.