Skip to content

Commit bc68508

Browse files
committed
improve error when given invalid scale definition; #772
1 parent b44a874 commit bc68508

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/scales.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {parse as isoParse} from "isoformat";
2-
import {isColor, isEvery, isOrdinal, isFirst, isSymbol, isTemporal, maybeSymbol, order, isTemporalString, isNumericString} from "./options.js";
2+
import {isColor, isEvery, isOrdinal, isFirst, isSymbol, isTemporal, maybeSymbol, order, isTemporalString, isNumericString, isScaleOptions} from "./options.js";
33
import {registry, color, position, radius, opacity, symbol, length} from "./scales/index.js";
44
import {ScaleLinear, ScaleSqrt, ScalePow, ScaleLog, ScaleSymlog, ScaleQuantile, ScaleThreshold, ScaleIdentity} from "./scales/quantitative.js";
55
import {ScaleDiverging, ScaleDivergingSqrt, ScaleDivergingPow, ScaleDivergingLog, ScaleDivergingSymlog} from "./scales/diverging.js";
@@ -384,6 +384,7 @@ export function scale(options = {}) {
384384
let scale;
385385
for (const key in options) {
386386
if (!registry.has(key)) continue; // ignore unknown properties
387+
if (!isScaleOptions(options[key])) continue; // e.g., ignore {color: "red"}
387388
if (scale !== undefined) throw new Error("ambiguous scale definition");
388389
scale = exposeScale(normalizeScale(key, options[key]));
389390
}

test/scales/scales-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ it("Plot.scale(description) returns a standalone scale", () => {
1414
});
1515
});
1616

17+
it("Plot.scale({}) throws an error", () => {
18+
assert.throws(() => Plot.scale({}), /invalid scale definition/);
19+
});
20+
21+
it("Plot.scale({color: {}}) throws an error", () => {
22+
assert.throws(() => Plot.scale({color: {}}), /invalid scale definition/);
23+
});
24+
1725
it("plot(…).scale(name) returns undefined for an unused scale", () => {
1826
const plot = Plot.dot([1, 2], {x: d => d, y: d => d}).plot();
1927
assert.deepStrictEqual(plot.scale("r"), undefined);

0 commit comments

Comments
 (0)