From b1d9d3ee3ab250b74eea69687681265fbc0fd356 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Sat, 14 Aug 2021 15:07:47 -0700 Subject: [PATCH] better error for null accessor --- src/sort.js | 2 +- test/sort-test.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/sort.js b/src/sort.js index a96a14b9..8d7a41a3 100644 --- a/src/sort.js +++ b/src/sort.js @@ -4,7 +4,7 @@ export default function sort(values, ...F) { if (typeof values[Symbol.iterator] !== "function") throw new TypeError("values is not iterable"); values = Array.from(values); let [f] = F; - if ((F.length === 1 && f.length === 1) || F.length > 1) { + if ((f && f.length === 1) || F.length > 1) { const index = Uint32Array.from(values, (d, i) => i); if (F.length > 1) { F = F.map(f => values.map(f)); diff --git a/test/sort-test.js b/test/sort-test.js index efa02859..d551fb8c 100644 --- a/test/sort-test.js +++ b/test/sort-test.js @@ -61,11 +61,12 @@ it("sort(values) accepts an iterable", () => { }); it("sort(values) enforces that values is iterable", () => { - assert.throws(() => sort({}), TypeError); + assert.throws(() => sort({}), {name: "TypeError", message: "values is not iterable"}); }); it("sort(values, comparator) enforces that comparator is a function", () => { - assert.throws(() => sort([], {}), TypeError); + assert.throws(() => sort([], {}), {name: "TypeError", message: "compare is not a function"}); + assert.throws(() => sort([], null), {name: "TypeError", message: "compare is not a function"}); }); it("sort(values) does not skip sparse elements", () => {