Skip to content

Commit f25e9f3

Browse files
Filmbostock
andauthored
case independent scale type matching (#1904)
* case independent scale type matching closes #1894 * normalize type in inferScaleType --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent 32462b1 commit f25e9f3

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/scales.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,16 @@ function formatScaleType(type) {
376376
return typeof type === "symbol" ? type.description : type;
377377
}
378378

379+
function maybeScaleType(type) {
380+
return typeof type === "string" ? `${type}`.toLowerCase() : type;
381+
}
382+
379383
// A special type symbol when the x and y scales are replaced with a projection.
380384
const typeProjection = {toString: () => "projection"};
381385

382386
function inferScaleType(key, channels, {type, domain, range, scheme, pivot, projection}) {
387+
type = maybeScaleType(type);
388+
383389
// The facet scales are always band scales; this cannot be changed.
384390
if (key === "fx" || key === "fy") return "band";
385391

@@ -391,7 +397,8 @@ function inferScaleType(key, channels, {type, domain, range, scheme, pivot, proj
391397
// If a channel dictates a scale type, make sure that it is consistent with
392398
// the user-specified scale type (if any) and all other channels. For example,
393399
// barY requires x to be a band scale and disallows any other scale type.
394-
for (const {type: t} of channels) {
400+
for (const channel of channels) {
401+
const t = maybeScaleType(channel.type);
395402
if (t === undefined) continue;
396403
else if (type === undefined) type = t;
397404
else if (type !== t) throw new Error(`scale incompatible with channel: ${type} !== ${t}`);

test/scales/scales-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ it("Plot does not throw an error if a quantile scale has a non-monotonic domain"
3737
});
3838
});
3939

40+
it("Scale types are lowercased", () => {
41+
assert.strictEqual(Plot.scale({x: {type: "UTC"}}).type, "utc");
42+
assert.strictEqual(Plot.scale({color: {type: "OrDiNaL"}}).type, "ordinal");
43+
assert.strictEqual(Plot.scale({fx: {type: "BAND"}}).type, "band");
44+
});
45+
4046
it("Plot.scale(description) returns a standalone scale", () => {
4147
const color = Plot.scale({color: {type: "linear"}});
4248
scaleEqual(color, {

0 commit comments

Comments
 (0)