Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type names to scales #274

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/band.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default function band() {
paddingOuter = 0,
align = 0.5;

scale.type= 'band';

delete scale.unknown;

function rescale() {
Expand Down Expand Up @@ -97,5 +99,7 @@ function pointish(scale) {
}

export function point() {
return pointish(band.apply(null, arguments).paddingInner(1));
const s = pointish(band.apply(null, arguments).paddingInner(1));
s.type = 'point';
return s;
}
20 changes: 15 additions & 5 deletions src/diverging.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export default function diverging() {
return copy(scale, diverging());
};

return initInterpolator.apply(scale, arguments);
const s = initInterpolator.apply(scale, arguments);
s.type = 'diverging';
return s;
}

export function divergingLog() {
Expand All @@ -76,7 +78,9 @@ export function divergingLog() {
return copy(scale, divergingLog()).base(scale.base());
};

return initInterpolator.apply(scale, arguments);
const s = initInterpolator.apply(scale, arguments);
s.type = 'divergingLog';
return s;
}

export function divergingSymlog() {
Expand All @@ -86,7 +90,9 @@ export function divergingSymlog() {
return copy(scale, divergingSymlog()).constant(scale.constant());
};

return initInterpolator.apply(scale, arguments);
const s = initInterpolator.apply(scale, arguments);
s.type = 'divergingSymlog';
return s;
}

export function divergingPow() {
Expand All @@ -96,9 +102,13 @@ export function divergingPow() {
return copy(scale, divergingPow()).exponent(scale.exponent());
};

return initInterpolator.apply(scale, arguments);
const s = initInterpolator.apply(scale, arguments);
s.type = 'divergingPow';
return s;
}

export function divergingSqrt() {
return divergingPow.apply(null, arguments).exponent(0.5);
const s = divergingPow.apply(null, arguments).exponent(0.5);
s.type = 'divergingSqrt';
return s;
}
4 changes: 3 additions & 1 deletion src/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ export default function identity(domain) {

domain = arguments.length ? Array.from(domain, number) : [0, 1];

return linearish(scale);
const s = linearish(scale);
s.type = 'identity';
return s;
}
6 changes: 4 additions & 2 deletions src/linear.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function linearish(scale) {
step = start, start = stop, stop = step;
step = i0, i0 = i1, i1 = step;
}

while (maxIter-- > 0) {
step = tickIncrement(start, stop, count);
if (step === prestep) {
Expand Down Expand Up @@ -66,5 +66,7 @@ export default function linear() {

initRange.apply(scale, arguments);

return linearish(scale);
const s = linearish(scale);
s.type = 'linear';
return s;
}
1 change: 1 addition & 0 deletions src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,6 @@ export default function log() {
const scale = loggish(transformer()).domain([1, 10]);
scale.copy = () => copy(scale, log()).base(scale.base());
initRange.apply(scale, arguments);
scale.type= 'log';
return scale;
}
2 changes: 2 additions & 0 deletions src/ordinal.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ export default function ordinal() {

initRange.apply(scale, arguments);

scale.type= 'ordinal';

return scale;
}
5 changes: 4 additions & 1 deletion src/pow.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ export default function pow() {

initRange.apply(scale, arguments);

scale.type= 'pow';
return scale;
}

export function sqrt() {
return pow.apply(null, arguments).exponent(0.5);
const s = pow.apply(null, arguments).exponent(0.5);
s.type = 'sqrt';
return s;
}
4 changes: 3 additions & 1 deletion src/quantile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,7 @@ export default function quantile() {
.unknown(unknown);
};

return initRange.apply(scale, arguments);
const s = initRange.apply(scale, arguments);
s.type = 'quantile';
return s;
}
4 changes: 3 additions & 1 deletion src/quantize.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@ export default function quantize() {
.unknown(unknown);
};

return initRange.apply(linearish(scale), arguments);
const s = initRange.apply(linearish(scale), arguments);
s.type = 'quantize';
return s;
}
4 changes: 3 additions & 1 deletion src/radial.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,7 @@ export default function radial() {

initRange.apply(scale, arguments);

return linearish(scale);
const s = linearish(scale);
s.type = 'radial';
return s;
}
20 changes: 15 additions & 5 deletions src/sequential.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export default function sequential() {
return copy(scale, sequential());
};

return initInterpolator.apply(scale, arguments);
const s = initInterpolator.apply(scale, arguments);
s.type = 'sequential';
return s;
}

export function sequentialLog() {
Expand All @@ -79,7 +81,9 @@ export function sequentialLog() {
return copy(scale, sequentialLog()).base(scale.base());
};

return initInterpolator.apply(scale, arguments);
const s = initInterpolator.apply(scale, arguments);
s.type = 'sequentialLog';
return s;
}

export function sequentialSymlog() {
Expand All @@ -89,7 +93,9 @@ export function sequentialSymlog() {
return copy(scale, sequentialSymlog()).constant(scale.constant());
};

return initInterpolator.apply(scale, arguments);
const s = initInterpolator.apply(scale, arguments);
s.type = 'sequentialSymlog';
return s;
}

export function sequentialPow() {
Expand All @@ -99,9 +105,13 @@ export function sequentialPow() {
return copy(scale, sequentialPow()).exponent(scale.exponent());
};

return initInterpolator.apply(scale, arguments);
const s = initInterpolator.apply(scale, arguments);
s.type = 'sequentialPow';
return s;
}

export function sequentialSqrt() {
return sequentialPow.apply(null, arguments).exponent(0.5);
const s = sequentialPow.apply(null, arguments).exponent(0.5);
s.type = 'sequentialSqrt';
return s;
}
4 changes: 3 additions & 1 deletion src/sequentialQuantile.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ export default function sequentialQuantile() {
return sequentialQuantile(interpolator).domain(domain);
};

return initInterpolator.apply(scale, arguments);
const s = initInterpolator.apply(scale, arguments);
s.type = 'sequentialQuantile';
return s;
}
4 changes: 3 additions & 1 deletion src/symlog.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ export default function symlog() {
return copy(scale, symlog()).constant(scale.constant());
};

return initRange.apply(scale, arguments);
const s = initRange.apply(scale, arguments);
s.type = 'symlog';
return s;
}
4 changes: 3 additions & 1 deletion src/threshold.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ export default function threshold() {
.unknown(unknown);
};

return initRange.apply(scale, arguments);
const s = initRange.apply(scale, arguments);
s.type = 'threshold';
return s;
}
4 changes: 3 additions & 1 deletion src/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,7 @@ export function calendar(ticks, tickInterval, year, month, week, day, hour, minu
}

export default function time() {
return initRange.apply(calendar(timeTicks, timeTickInterval, timeYear, timeMonth, timeWeek, timeDay, timeHour, timeMinute, timeSecond, timeFormat).domain([new Date(2000, 0, 1), new Date(2000, 0, 2)]), arguments);
const s = initRange.apply(calendar(timeTicks, timeTickInterval, timeYear, timeMonth, timeWeek, timeDay, timeHour, timeMinute, timeSecond, timeFormat).domain([new Date(2000, 0, 1), new Date(2000, 0, 2)]), arguments);
s.type = 'time';
return s;
}
4 changes: 3 additions & 1 deletion src/utcTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ import {calendar} from "./time.js";
import {initRange} from "./init.js";

export default function utcTime() {
return initRange.apply(calendar(utcTicks, utcTickInterval, utcYear, utcMonth, utcWeek, utcDay, utcHour, utcMinute, utcSecond, utcFormat).domain([Date.UTC(2000, 0, 1), Date.UTC(2000, 0, 2)]), arguments);
const s = initRange.apply(calendar(utcTicks, utcTickInterval, utcYear, utcMonth, utcWeek, utcDay, utcHour, utcMinute, utcSecond, utcFormat).domain([Date.UTC(2000, 0, 1), Date.UTC(2000, 0, 2)]), arguments);
s.type = 'utc';
return s;
}
1 change: 1 addition & 0 deletions test/band-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ it("scaleBand() has the expected defaults", () => {
assert.strictEqual(s.paddingInner(), 0);
assert.strictEqual(s.paddingOuter(), 0);
assert.strictEqual(s.align(), 0.5);
assert.strictEqual(s.type, 'band');
});

it("band(value) computes discrete bands in a continuous range", () => {
Expand Down
1 change: 1 addition & 0 deletions test/diverging-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ it("scaleDiverging() has the expected defaults", () => {
assert.strictEqual(s( 0.5), 0.5);
assert.strictEqual(s( 1.0), 1.0);
assert.strictEqual(s( 1.5), 1.5);
assert.strictEqual(s.type, 'diverging');
});

it("diverging.clamp(true) enables clamping", () => {
Expand Down
1 change: 1 addition & 0 deletions test/identity-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ it("scaleIdentity() has the expected defaults", () => {
const s = scaleIdentity();
assert.deepStrictEqual(s.domain(), [0, 1]);
assert.deepStrictEqual(s.range(), [0, 1]);
assert.strictEqual(s.type, 'identity');
});

it("scaleIdentity(range) sets the domain and range", () => {
Expand Down
1 change: 1 addition & 0 deletions test/linear-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ it("scaleLinear() has the expected defaults", () => {
assert.strictEqual(s.clamp(), false);
assert.strictEqual(s.unknown(), undefined);
assert.deepStrictEqual(s.interpolate()({array: ["red"]}, {array: ["blue"]})(0.5), {array: ["rgb(128, 0, 128)"]});
assert.strictEqual(s.type, 'linear');
});

it("scaleLinear(range) sets the range", () => {
Expand Down
1 change: 1 addition & 0 deletions test/log-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ it("scaleLog() has the expected defaults", () => {
assertInDelta(x.invert(0.69897), 5);
assertInDelta(x(3.162278), 0.5);
assertInDelta(x.invert(0.5), 3.162278);
assert.strictEqual(x.type, 'log');
});

it("log.domain(…) coerces values to numbers", () => {
Expand Down
1 change: 1 addition & 0 deletions test/ordinal-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ it("scaleOrdinal() has the expected defaults", () => {
assert.strictEqual(s(0), undefined);
assert.strictEqual(s.unknown(), scaleImplicit);
assert.deepStrictEqual(s.domain(), [0]);
assert.strictEqual(s.type, 'ordinal');
});

it("ordinal(x) maps a unique name x in the domain to the corresponding value y in the range", () => {
Expand Down
1 change: 1 addition & 0 deletions test/point-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ it("scalePoint() has the expected defaults", () => {
assert.strictEqual(s.round(), false);
assert.strictEqual(s.padding(), 0);
assert.strictEqual(s.align(), 0.5);
assert.strictEqual(s.type, 'point');
});

it("scalePoint() does not expose paddingInner and paddingOuter", () => {
Expand Down
1 change: 1 addition & 0 deletions test/pow-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ it("scalePow() has the expected defaults", () => {
assert.strictEqual(s.clamp(), false);
assert.strictEqual(s.exponent(), 1);
assert.deepStrictEqual(s.interpolate()({array: ["red"]}, {array: ["blue"]})(0.5), {array: ["rgb(128, 0, 128)"]});
assert.strictEqual(s.type, 'pow');
});

it("pow(x) maps a domain value x to a range value y", () => {
Expand Down
1 change: 1 addition & 0 deletions test/quantile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ it("scaleQuantile() has the expected default", () => {
assert.deepStrictEqual(s.domain(), []);
assert.deepStrictEqual(s.range(), []);
assert.strictEqual(s.unknown(), undefined);
assert.strictEqual(s.type, 'quantile');
});

it("quantile(x) uses the R-7 algorithm to compute quantiles", () => {
Expand Down
1 change: 1 addition & 0 deletions test/quantize-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ it("scaleQuantize() has the expected defaults", () => {
assert.deepStrictEqual(s.thresholds(), [0.5]);
assert.strictEqual(s(0.25), 0);
assert.strictEqual(s(0.75), 1);
assert.strictEqual(s.type, 'quantize');
});

it("quantize(value) maps a number to a discrete value in the range", () => {
Expand Down
1 change: 1 addition & 0 deletions test/radial-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ it("scaleRadial() has the expected defaults", () => {
assert.deepStrictEqual(s.range(), [0, 1]);
assert.strictEqual(s.clamp(), false);
assert.strictEqual(s.round(), false);
assert.strictEqual(s.type, 'radial');
});

it("scaleRadial(range) sets the range", () => {
Expand Down
1 change: 1 addition & 0 deletions test/sequential-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ it("scaleSequential() has the expected defaults", () => {
assert.strictEqual(s( 0.5), 0.5);
assert.strictEqual(s( 1.0), 1.0);
assert.strictEqual(s( 1.5), 1.5);
assert.strictEqual(s.type, 'sequential');
});

it("sequential.clamp(true) enables clamping", () => {
Expand Down
1 change: 1 addition & 0 deletions test/sequentialQuantile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ it("sequentialQuantile() clamps", () => {
assert.strictEqual(s(1), 0.25);
assert.strictEqual(s(10), 1);
assert.strictEqual(s(20), 1);
assert.strictEqual(s.type, 'sequentialQuantile');
});

it("sequentialQuantile().domain() sorts the domain", () => {
Expand Down
1 change: 1 addition & 0 deletions test/sqrt-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ it("scaleSqrt() has the expected defaults", () => {
assert.strictEqual(s.clamp(), false);
assert.strictEqual(s.exponent(), 0.5);
assert.deepStrictEqual(s.interpolate()({array: ["red"]}, {array: ["blue"]})(0.5), {array: ["rgb(128, 0, 128)"]});
assert.strictEqual(s.type, 'sqrt');
});

it("sqrt(x) maps a domain value x to a range value y", () => {
Expand Down
1 change: 1 addition & 0 deletions test/symlog-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ it("scaleSymlog() has the expected defaults", () => {
assert.deepStrictEqual(s.range(), [0, 1]);
assert.strictEqual(s.clamp(), false);
assert.strictEqual(s.constant(), 1);
assert.strictEqual(s.type, 'symlog');
});

it("symlog(x) maps a domain value x to a range value y", () => {
Expand Down
1 change: 1 addition & 0 deletions test/threshold-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ it("scaleThreshold() has the expected defaults", () => {
assert.deepStrictEqual(x.range(), [0, 1]);
assert.strictEqual(x(0.50), 1);
assert.strictEqual(x(0.49), 0);
assert.strictEqual(x.type, 'threshold');
});

it("threshold(x) maps a number to a discrete value in the range", () => {
Expand Down
1 change: 1 addition & 0 deletions test/time-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ it("time.domain([-1e50, 1e50]) is equivalent to time.domain([NaN, NaN])", () =>
assert.strictEqual(isNaN(x.domain()[0]), true); // Note: also coerced on retrieval, so insufficient test!
assert.strictEqual(isNaN(x.domain()[1]), true);
assert.deepStrictEqual(x.ticks(10), []);
assert.strictEqual(x.type, 'time');
});

it("time.domain(domain) accepts an iterable", () => {
Expand Down
19 changes: 19 additions & 0 deletions test/types-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import assert from "assert";
import * as d3 from "../src/index.js";

const exclude = [
'tickFormat',
'scaleImplicit'
]

// test that every scale has the right `type` prop
Object.keys(d3).filter(d => {
return !exclude.includes(d);
}).forEach(d => {
it(`${d}() has the expected type`, () => {
const type = d.replace('scale', '')
.replace(/^\w/, w => w.toLowerCase())
const scale = d3[d]();
assert.strictEqual(scale.type, type);
});
});
Loading