From 694ed8f631e09f45a30ad24725c7972407bebcb2 Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Wed, 20 May 2020 09:25:35 -0700 Subject: [PATCH 1/4] Ensure that niced domains always span ticks fixes #209 --- src/linear.js | 62 +++++++++++++++++++++------------------------ test/linear-test.js | 30 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 33 deletions(-) diff --git a/src/linear.js b/src/linear.js index b91f712..73b3274 100644 --- a/src/linear.js +++ b/src/linear.js @@ -4,63 +4,59 @@ import {initRange} from "./init.js"; import tickFormat from "./tickFormat.js"; export function linearish(scale) { - var domain = scale.domain; + const domain = scale.domain; scale.ticks = function(count) { - var d = domain(); + const d = domain(); return ticks(d[0], d[d.length - 1], count == null ? 10 : count); }; scale.tickFormat = function(count, specifier) { - var d = domain(); + const d = domain(); return tickFormat(d[0], d[d.length - 1], count == null ? 10 : count, specifier); }; scale.nice = function(count) { if (count == null) count = 10; - var d = domain(), - i0 = 0, - i1 = d.length - 1, - start = d[i0], - stop = d[i1], - step; + const d = domain(); + let i0 = 0; + let i1 = d.length - 1; + let start = d[i0]; + let stop = d[i1]; + let prestep; + let step; if (stop < start) { step = start, start = stop, stop = step; step = i0, i0 = i1, i1 = step; } - - step = tickIncrement(start, stop, count); - - if (step > 0) { - start = Math.floor(start / step) * step; - stop = Math.ceil(stop / step) * step; - step = tickIncrement(start, stop, count); - } else if (step < 0) { - start = Math.ceil(start * step) / step; - stop = Math.floor(stop * step) / step; - step = tickIncrement(start, stop, count); + + // eslint-disable-next-line no-constant-condition + while (true) { + const step = tickIncrement(start, stop, count); + if (step === prestep) { + d[i0] = start + d[i1] = stop + return domain(d); + } else if (step > 0) { + start = Math.floor(start / step) * step; + stop = Math.ceil(stop / step) * step; + } else if (step < 0) { + start = Math.ceil(start * step) / step; + stop = Math.floor(stop * step) / step; + } else { + return scale; + } + prestep = step; } - - if (step > 0) { - d[i0] = Math.floor(start / step) * step; - d[i1] = Math.ceil(stop / step) * step; - domain(d); - } else if (step < 0) { - d[i0] = Math.ceil(start * step) / step; - d[i1] = Math.floor(stop * step) / step; - domain(d); - } - - return scale; }; return scale; } export default function linear() { - var scale = continuous(); + const scale = continuous(); scale.copy = function() { return copy(scale, linear()); diff --git a/test/linear-test.js b/test/linear-test.js index 62de558..154760d 100644 --- a/test/linear-test.js +++ b/test/linear-test.js @@ -373,6 +373,36 @@ tape("linear.ticks(count) returns the expected ticks for a polylinear domain", f test.end(); }); +tape("linear.ticks(X) spans linear.nice(X).domain()", function(test) { + function check(domain, count) { + var s = scale.scaleLinear().domain(domain).nice(count); + var ticks = s.ticks(count); + test.deepEqual([ticks[0], ticks[ticks.length - 1]], s.domain()); + } + + check([1, 9], 2); + check([1, 9], 3); + check([1, 9], 4); + + check([8, 9], 2); + check([8, 9], 3); + check([8, 9], 4); + + check([1, 21], 2); + check([2, 21], 2); + check([3, 21], 2); + check([4, 21], 2); + check([5, 21], 2); + check([6, 21], 2); + check([7, 21], 2); + check([8, 21], 2); + check([9, 21], 2); + check([10, 21], 2); + check([11, 21], 2); + + test.end(); +}) + tape("linear.ticks(count) returns the empty array if count is not a positive integer", function(test) { var s = scale.scaleLinear(); test.deepEqual(s.ticks(NaN), []); From 96b8f4b9d6171c19ed4f1c5d8618531ef9634470 Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Wed, 20 May 2020 09:50:34 -0700 Subject: [PATCH 2/4] Limit the number of iterations in scaleLinear.nice --- src/linear.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/linear.js b/src/linear.js index 73b3274..f9f91cb 100644 --- a/src/linear.js +++ b/src/linear.js @@ -26,14 +26,14 @@ export function linearish(scale) { let stop = d[i1]; let prestep; let step; + let maxIter = 10; if (stop < start) { step = start, start = stop, stop = step; step = i0, i0 = i1, i1 = step; } - // eslint-disable-next-line no-constant-condition - while (true) { + while (maxIter-- > 0) { const step = tickIncrement(start, stop, count); if (step === prestep) { d[i0] = start @@ -46,10 +46,12 @@ export function linearish(scale) { start = Math.ceil(start * step) / step; stop = Math.floor(stop * step) / step; } else { - return scale; + break; } prestep = step; } + + return scale; }; return scale; From da99948d2fb7a4b7a93fc69be427e5cd7550c8a3 Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Mon, 15 Jun 2020 08:16:26 -0700 Subject: [PATCH 3/4] Use var --- src/linear.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/linear.js b/src/linear.js index f9f91cb..3617d32 100644 --- a/src/linear.js +++ b/src/linear.js @@ -4,29 +4,29 @@ import {initRange} from "./init.js"; import tickFormat from "./tickFormat.js"; export function linearish(scale) { - const domain = scale.domain; + var domain = scale.domain; scale.ticks = function(count) { - const d = domain(); + var d = domain(); return ticks(d[0], d[d.length - 1], count == null ? 10 : count); }; scale.tickFormat = function(count, specifier) { - const d = domain(); + var d = domain(); return tickFormat(d[0], d[d.length - 1], count == null ? 10 : count, specifier); }; scale.nice = function(count) { if (count == null) count = 10; - const d = domain(); - let i0 = 0; - let i1 = d.length - 1; - let start = d[i0]; - let stop = d[i1]; - let prestep; - let step; - let maxIter = 10; + var d = domain(); + var i0 = 0; + var i1 = d.length - 1; + var start = d[i0]; + var stop = d[i1]; + var prestep; + var step; + var maxIter = 10; if (stop < start) { step = start, start = stop, stop = step; @@ -34,7 +34,7 @@ export function linearish(scale) { } while (maxIter-- > 0) { - const step = tickIncrement(start, stop, count); + step = tickIncrement(start, stop, count); if (step === prestep) { d[i0] = start d[i1] = stop @@ -58,7 +58,7 @@ export function linearish(scale) { } export default function linear() { - const scale = continuous(); + var scale = continuous(); scale.copy = function() { return copy(scale, linear()); From 8b4f811df50673dfadee8b303a75c162e67bb8c0 Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Mon, 15 Jun 2020 08:45:37 -0700 Subject: [PATCH 4/4] Use const/let instead of var --- src/band.js | 34 +++++++++++----------- src/continuous.js | 38 ++++++++++++------------- src/diverging.js | 38 +++++++++++++------------ src/identity.js | 2 +- src/linear.js | 24 ++++++++-------- src/log.js | 40 +++++++++++++------------- src/nice.js | 10 +++---- src/ordinal.js | 11 +++---- src/pow.js | 6 ++-- src/quantile.js | 13 +++++---- src/quantize.js | 16 +++++------ src/radial.js | 10 +++---- src/sequential.js | 29 ++++++++++--------- src/sequentialQuantile.js | 4 +-- src/symlog.js | 5 ++-- src/threshold.js | 10 +++---- src/tickFormat.js | 6 ++-- src/time.js | 60 +++++++++++++++++++-------------------- 18 files changed, 181 insertions(+), 175 deletions(-) diff --git a/src/band.js b/src/band.js index 88e1fbd..2e15934 100644 --- a/src/band.js +++ b/src/band.js @@ -3,31 +3,31 @@ import {initRange} from "./init.js"; import ordinal from "./ordinal.js"; export default function band() { - var scale = ordinal().unknown(undefined), - domain = scale.domain, - ordinalRange = scale.range, - r0 = 0, - r1 = 1, - step, - bandwidth, - round = false, - paddingInner = 0, - paddingOuter = 0, - align = 0.5; + const scale = ordinal().unknown(undefined); + const domain = scale.domain; + const ordinalRange = scale.range; + let r0 = 0; + let r1 = 1; + let step; + let bandwidth; + let round = false; + let paddingInner = 0; + let paddingOuter = 0; + let align = 0.5; delete scale.unknown; function rescale() { - var n = domain().length, - reverse = r1 < r0, - start = reverse ? r1 : r0, - stop = reverse ? r0 : r1; + const n = domain().length; + const reverse = r1 < r0; + let start = reverse ? r1 : r0; + const stop = reverse ? r0 : r1; step = (stop - start) / Math.max(1, n - paddingInner + paddingOuter * 2); if (round) step = Math.floor(step); start += (stop - start - step * (n - paddingInner)) * align; bandwidth = step * (1 - paddingInner); if (round) start = Math.round(start), bandwidth = Math.round(bandwidth); - var values = sequence(n).map(function(i) { return start + step * i; }); + const values = sequence(n).map(function(i) { return start + step * i; }); return ordinalRange(reverse ? values.reverse() : values); } @@ -83,7 +83,7 @@ export default function band() { } function pointish(scale) { - var copy = scale.copy; + const copy = scale.copy; scale.padding = scale.paddingOuter; delete scale.paddingInner; diff --git a/src/continuous.js b/src/continuous.js index 2dd1cf9..978c04b 100644 --- a/src/continuous.js +++ b/src/continuous.js @@ -3,7 +3,7 @@ import {interpolate as interpolateValue, interpolateNumber, interpolateRound} fr import constant from "./constant.js"; import number from "./number.js"; -var unit = [0, 1]; +const unit = [0, 1]; export function identity(x) { return x; @@ -16,7 +16,7 @@ function normalize(a, b) { } function clamper(a, b) { - var t; + let t; if (a > b) t = a, a = b, b = t; return function(x) { return Math.max(a, Math.min(b, x)); }; } @@ -24,17 +24,17 @@ function clamper(a, b) { // normalize(a, b)(x) takes a domain value x in [a,b] and returns the corresponding parameter t in [0,1]. // interpolate(a, b)(t) takes a parameter t in [0,1] and returns the corresponding range value x in [a,b]. function bimap(domain, range, interpolate) { - var d0 = domain[0], d1 = domain[1], r0 = range[0], r1 = range[1]; + let d0 = domain[0], d1 = domain[1], r0 = range[0], r1 = range[1]; if (d1 < d0) d0 = normalize(d1, d0), r0 = interpolate(r1, r0); else d0 = normalize(d0, d1), r0 = interpolate(r0, r1); return function(x) { return r0(d0(x)); }; } function polymap(domain, range, interpolate) { - var j = Math.min(domain.length, range.length) - 1, - d = new Array(j), - r = new Array(j), - i = -1; + const j = Math.min(domain.length, range.length) - 1; + const d = new Array(j); + const r = new Array(j); + let i = -1; // Reverse descending domains. if (domain[j] < domain[0]) { @@ -48,7 +48,7 @@ function polymap(domain, range, interpolate) { } return function(x) { - var i = bisect(domain, x, 1, j) - 1; + const i = bisect(domain, x, 1, j) - 1; return r[i](d[i](x)); }; } @@ -63,19 +63,19 @@ export function copy(source, target) { } export function transformer() { - var domain = unit, - range = unit, - interpolate = interpolateValue, - transform, - untransform, - unknown, - clamp = identity, - piecewise, - output, - input; + let domain = unit; + let range = unit; + let interpolate = interpolateValue; + let transform; + let untransform; + let unknown; + let clamp = identity; + let piecewise; + let output; + let input; function rescale() { - var n = Math.min(domain.length, range.length); + const n = Math.min(domain.length, range.length); if (clamp !== identity) clamp = clamper(domain[0], domain[n - 1]); piecewise = n > 2 ? polymap : bimap; output = input = null; diff --git a/src/diverging.js b/src/diverging.js index f590748..fb3b638 100644 --- a/src/diverging.js +++ b/src/diverging.js @@ -8,19 +8,19 @@ import {symlogish} from "./symlog.js"; import {powish} from "./pow.js"; function transformer() { - var x0 = 0, - x1 = 0.5, - x2 = 1, - s = 1, - t0, - t1, - t2, - k10, - k21, - interpolator = identity, - transform, - clamp = false, - unknown; + let x0 = 0; + let x1 = 0.5; + let x2 = 1; + let s = 1; + let t0; + let t1; + let t2; + let k10; + let k21; + let interpolator = identity; + let transform; + let clamp = false; + let unknown; function scale(x) { return isNaN(x = +x) ? unknown : (x = 0.5 + ((x = +transform(x)) - t1) * (s * x < s * t1 ? k10 : k21), interpolator(clamp ? Math.max(0, Math.min(1, x)) : x)); @@ -40,7 +40,9 @@ function transformer() { function range(interpolate) { return function(_) { - var r0, r1, r2; + let r0; + let r1; + let r2; return arguments.length ? ([r0, r1, r2] = _, interpolator = piecewise(interpolate, [r0, r1, r2]), scale) : [interpolator(0), interpolator(0.5), interpolator(1)]; }; } @@ -60,7 +62,7 @@ function transformer() { } export default function diverging() { - var scale = linearish(transformer()(identity)); + const scale = linearish(transformer()(identity)); scale.copy = function() { return copy(scale, diverging()); @@ -70,7 +72,7 @@ export default function diverging() { } export function divergingLog() { - var scale = loggish(transformer()).domain([0.1, 1, 10]); + const scale = loggish(transformer()).domain([0.1, 1, 10]); scale.copy = function() { return copy(scale, divergingLog()).base(scale.base()); @@ -80,7 +82,7 @@ export function divergingLog() { } export function divergingSymlog() { - var scale = symlogish(transformer()); + const scale = symlogish(transformer()); scale.copy = function() { return copy(scale, divergingSymlog()).constant(scale.constant()); @@ -90,7 +92,7 @@ export function divergingSymlog() { } export function divergingPow() { - var scale = powish(transformer()); + const scale = powish(transformer()); scale.copy = function() { return copy(scale, divergingPow()).exponent(scale.exponent()); diff --git a/src/identity.js b/src/identity.js index af41c59..1f35b00 100644 --- a/src/identity.js +++ b/src/identity.js @@ -2,7 +2,7 @@ import {linearish} from "./linear.js"; import number from "./number.js"; export default function identity(domain) { - var unknown; + let unknown; function scale(x) { return isNaN(x = +x) ? unknown : x; diff --git a/src/linear.js b/src/linear.js index 3617d32..185afd3 100644 --- a/src/linear.js +++ b/src/linear.js @@ -4,29 +4,29 @@ import {initRange} from "./init.js"; import tickFormat from "./tickFormat.js"; export function linearish(scale) { - var domain = scale.domain; + const domain = scale.domain; scale.ticks = function(count) { - var d = domain(); + const d = domain(); return ticks(d[0], d[d.length - 1], count == null ? 10 : count); }; scale.tickFormat = function(count, specifier) { - var d = domain(); + const d = domain(); return tickFormat(d[0], d[d.length - 1], count == null ? 10 : count, specifier); }; scale.nice = function(count) { if (count == null) count = 10; - var d = domain(); - var i0 = 0; - var i1 = d.length - 1; - var start = d[i0]; - var stop = d[i1]; - var prestep; - var step; - var maxIter = 10; + const d = domain(); + let i0 = 0; + let i1 = d.length - 1; + let start = d[i0]; + let stop = d[i1]; + let prestep; + let step; + let maxIter = 10; if (stop < start) { step = start, start = stop, stop = step; @@ -58,7 +58,7 @@ export function linearish(scale) { } export default function linear() { - var scale = continuous(); + const scale = continuous(); scale.copy = function() { return copy(scale, linear()); diff --git a/src/log.js b/src/log.js index ad3f231..428fa21 100644 --- a/src/log.js +++ b/src/log.js @@ -44,11 +44,11 @@ function reflect(f) { } export function loggish(transform) { - var scale = transform(transformLog, transformExp), - domain = scale.domain, - base = 10, - logs, - pows; + const scale = transform(transformLog, transformExp); + const domain = scale.domain; + let base = 10; + let logs; + let pows; function rescale() { logs = logp(base), pows = powp(base); @@ -70,20 +70,20 @@ export function loggish(transform) { }; scale.ticks = function(count) { - var d = domain(), - u = d[0], - v = d[d.length - 1], - r; + const d = domain(); + let u = d[0]; + let v = d[d.length - 1]; + let r; + let t; - if (r = v < u) i = u, u = v, v = i; + if (r = v < u) t = u, u = v, v = t; - var i = logs(u), - j = logs(v), - p, - k, - t, - n = count == null ? 10 : +count, - z = []; + let i = logs(u); + let j = logs(v); + let p; + let k; + const n = count == null ? 10 : +count; + let z = []; if (!(base % 1) && j - i < n) { i = Math.floor(i), j = Math.ceil(j); @@ -115,9 +115,9 @@ export function loggish(transform) { if (typeof specifier !== "function") specifier = format(specifier); if (count === Infinity) return specifier; if (count == null) count = 10; - var k = Math.max(1, base * count / scale.ticks().length); // TODO fast estimate? + const k = Math.max(1, base * count / scale.ticks().length); // TODO fast estimate? return function(d) { - var i = d / pows(Math.round(logs(d))); + let i = d / pows(Math.round(logs(d))); if (i * base < base - 0.5) i *= base; return i <= k ? specifier(d) : ""; }; @@ -134,7 +134,7 @@ export function loggish(transform) { } export default function log() { - var scale = loggish(transformer()).domain([1, 10]); + const scale = loggish(transformer()).domain([1, 10]); scale.copy = function() { return copy(scale, log()).base(scale.base()); diff --git a/src/nice.js b/src/nice.js index b377c76..6b58221 100644 --- a/src/nice.js +++ b/src/nice.js @@ -1,11 +1,11 @@ export default function(domain, interval) { domain = domain.slice(); - var i0 = 0, - i1 = domain.length - 1, - x0 = domain[i0], - x1 = domain[i1], - t; + let i0 = 0; + let i1 = domain.length - 1; + let x0 = domain[i0]; + let x1 = domain[i1]; + let t; if (x1 < x0) { t = i0, i0 = i1, i1 = t; diff --git a/src/ordinal.js b/src/ordinal.js index e98f600..3526038 100644 --- a/src/ordinal.js +++ b/src/ordinal.js @@ -3,13 +3,14 @@ import {initRange} from "./init.js"; export const implicit = Symbol("implicit"); export default function ordinal() { - var index = new Map(), - domain = [], - range = [], - unknown = implicit; + let index = new Map(); + let domain = []; + let range = []; + let unknown = implicit; function scale(d) { - var key = d + "", i = index.get(key); + const key = d + ""; + let i = index.get(key); if (!i) { if (unknown !== implicit) return unknown; index.set(key, i = domain.push(d)); diff --git a/src/pow.js b/src/pow.js index 8146f1c..d5cf6d3 100644 --- a/src/pow.js +++ b/src/pow.js @@ -17,8 +17,8 @@ function transformSquare(x) { } export function powish(transform) { - var scale = transform(identity, identity), - exponent = 1; + const scale = transform(identity, identity); + let exponent = 1; function rescale() { return exponent === 1 ? transform(identity, identity) @@ -34,7 +34,7 @@ export function powish(transform) { } export default function pow() { - var scale = powish(transformer()); + const scale = powish(transformer()); scale.copy = function() { return copy(scale, pow()).exponent(scale.exponent()); diff --git a/src/quantile.js b/src/quantile.js index 61c04c0..e1628d5 100644 --- a/src/quantile.js +++ b/src/quantile.js @@ -2,13 +2,14 @@ import {ascending, bisect, quantile as threshold} from "d3-array"; import {initRange} from "./init.js"; export default function quantile() { - var domain = [], - range = [], - thresholds = [], - unknown; + let domain = []; + let range = []; + let thresholds = []; + let unknown; function rescale() { - var i = 0, n = Math.max(1, range.length); + let i = 0; + const n = Math.max(1, range.length); thresholds = new Array(n - 1); while (++i < n) thresholds[i - 1] = threshold(domain, i / n); return scale; @@ -19,7 +20,7 @@ export default function quantile() { } scale.invertExtent = function(y) { - var i = range.indexOf(y); + const i = range.indexOf(y); return i < 0 ? [NaN, NaN] : [ i > 0 ? thresholds[i - 1] : domain[0], i < thresholds.length ? thresholds[i] : domain[domain.length - 1] diff --git a/src/quantize.js b/src/quantize.js index 735a531..987767b 100644 --- a/src/quantize.js +++ b/src/quantize.js @@ -3,19 +3,19 @@ import {linearish} from "./linear.js"; import {initRange} from "./init.js"; export default function quantize() { - var x0 = 0, - x1 = 1, - n = 1, - domain = [0.5], - range = [0, 1], - unknown; + let x0 = 0; + let x1 = 1; + let n = 1; + let domain = [0.5]; + let range = [0, 1]; + let unknown; function scale(x) { return x <= x ? range[bisect(domain, x, 0, n)] : unknown; } function rescale() { - var i = -1; + let i = -1; domain = new Array(n); while (++i < n) domain[i] = ((i + 1) * x1 - (i - n) * x0) / (n + 1); return scale; @@ -30,7 +30,7 @@ export default function quantize() { }; scale.invertExtent = function(y) { - var i = range.indexOf(y); + let i = range.indexOf(y); return i < 0 ? [NaN, NaN] : i < 1 ? [x0, domain[0]] : i >= n ? [domain[n - 1], x1] diff --git a/src/radial.js b/src/radial.js index 5c00cc6..1adb9c3 100644 --- a/src/radial.js +++ b/src/radial.js @@ -12,13 +12,13 @@ function unsquare(x) { } export default function radial() { - var squared = continuous(), - range = [0, 1], - round = false, - unknown; + const squared = continuous(); + let range = [0, 1]; + let round = false; + let unknown; function scale(x) { - var y = unsquare(squared(x)); + const y = unsquare(squared(x)); return isNaN(y) ? unknown : round ? Math.round(y) : y; } diff --git a/src/sequential.js b/src/sequential.js index bd38dba..33078bd 100644 --- a/src/sequential.js +++ b/src/sequential.js @@ -7,15 +7,15 @@ import {symlogish} from "./symlog.js"; import {powish} from "./pow.js"; function transformer() { - var x0 = 0, - x1 = 1, - t0, - t1, - k10, - transform, - interpolator = identity, - clamp = false, - unknown; + let x0 = 0; + let x1 = 1; + let t0; + let t1; + let k10; + let transform; + let interpolator = identity; + let clamp = false; + let unknown; function scale(x) { return isNaN(x = +x) ? unknown : interpolator(k10 === 0 ? 0.5 : (x = (transform(x) - t0) * k10, clamp ? Math.max(0, Math.min(1, x)) : x)); @@ -35,7 +35,8 @@ function transformer() { function range(interpolate) { return function(_) { - var r0, r1; + let r0; + let r1; return arguments.length ? ([r0, r1] = _, interpolator = interpolate(r0, r1), scale) : [interpolator(0), interpolator(1)]; }; } @@ -63,7 +64,7 @@ export function copy(source, target) { } export default function sequential() { - var scale = linearish(transformer()(identity)); + const scale = linearish(transformer()(identity)); scale.copy = function() { return copy(scale, sequential()); @@ -73,7 +74,7 @@ export default function sequential() { } export function sequentialLog() { - var scale = loggish(transformer()).domain([1, 10]); + const scale = loggish(transformer()).domain([1, 10]); scale.copy = function() { return copy(scale, sequentialLog()).base(scale.base()); @@ -83,7 +84,7 @@ export function sequentialLog() { } export function sequentialSymlog() { - var scale = symlogish(transformer()); + const scale = symlogish(transformer()); scale.copy = function() { return copy(scale, sequentialSymlog()).constant(scale.constant()); @@ -93,7 +94,7 @@ export function sequentialSymlog() { } export function sequentialPow() { - var scale = powish(transformer()); + const scale = powish(transformer()); scale.copy = function() { return copy(scale, sequentialPow()).exponent(scale.exponent()); diff --git a/src/sequentialQuantile.js b/src/sequentialQuantile.js index 132ebd0..b0b4e09 100644 --- a/src/sequentialQuantile.js +++ b/src/sequentialQuantile.js @@ -3,8 +3,8 @@ import {identity} from "./continuous.js"; import {initInterpolator} from "./init.js"; export default function sequentialQuantile() { - var domain = [], - interpolator = identity; + let domain = []; + let interpolator = identity; function scale(x) { if (!isNaN(x = +x)) return interpolator((bisect(domain, x, 1) - 1) / (domain.length - 1)); diff --git a/src/symlog.js b/src/symlog.js index 125fa7b..c55e8a0 100644 --- a/src/symlog.js +++ b/src/symlog.js @@ -15,7 +15,8 @@ function transformSymexp(c) { } export function symlogish(transform) { - var c = 1, scale = transform(transformSymlog(c), transformSymexp(c)); + let c = 1; + const scale = transform(transformSymlog(c), transformSymexp(c)); scale.constant = function(_) { return arguments.length ? transform(transformSymlog(c = +_), transformSymexp(c)) : c; @@ -25,7 +26,7 @@ export function symlogish(transform) { } export default function symlog() { - var scale = symlogish(transformer()); + const scale = symlogish(transformer()); scale.copy = function() { return copy(scale, symlog()).constant(scale.constant()); diff --git a/src/threshold.js b/src/threshold.js index 79d3559..295570f 100644 --- a/src/threshold.js +++ b/src/threshold.js @@ -2,10 +2,10 @@ import {bisect} from "d3-array"; import {initRange} from "./init.js"; export default function threshold() { - var domain = [0.5], - range = [0, 1], - unknown, - n = 1; + let domain = [0.5]; + let range = [0, 1]; + let unknown; + let n = 1; function scale(x) { return x <= x ? range[bisect(domain, x, 0, n)] : unknown; @@ -20,7 +20,7 @@ export default function threshold() { }; scale.invertExtent = function(y) { - var i = range.indexOf(y); + const i = range.indexOf(y); return [domain[i - 1], domain[i]]; }; diff --git a/src/tickFormat.js b/src/tickFormat.js index 2f370ee..24e57f8 100644 --- a/src/tickFormat.js +++ b/src/tickFormat.js @@ -2,12 +2,12 @@ import {tickStep} from "d3-array"; import {format, formatPrefix, formatSpecifier, precisionFixed, precisionPrefix, precisionRound} from "d3-format"; export default function(start, stop, count, specifier) { - var step = tickStep(start, stop, count), - precision; + const step = tickStep(start, stop, count); + let precision; specifier = formatSpecifier(specifier == null ? ",f" : specifier); switch (specifier.type) { case "s": { - var value = Math.max(Math.abs(start), Math.abs(stop)); + const value = Math.max(Math.abs(start), Math.abs(stop)); if (specifier.precision == null && !isNaN(precision = precisionPrefix(step, value))) specifier.precision = precision; return formatPrefix(specifier, value); } diff --git a/src/time.js b/src/time.js index bfe7839..50e49fc 100644 --- a/src/time.js +++ b/src/time.js @@ -5,13 +5,13 @@ import continuous, {copy} from "./continuous.js"; import {initRange} from "./init.js"; import nice from "./nice.js"; -var durationSecond = 1000, - durationMinute = durationSecond * 60, - durationHour = durationMinute * 60, - durationDay = durationHour * 24, - durationWeek = durationDay * 7, - durationMonth = durationDay * 30, - durationYear = durationDay * 365; +const durationSecond = 1000; +const durationMinute = durationSecond * 60; +const durationHour = durationMinute * 60; +const durationDay = durationHour * 24; +const durationWeek = durationDay * 7; +const durationMonth = durationDay * 30; +const durationYear = durationDay * 365; function date(t) { return new Date(t); @@ -22,20 +22,20 @@ function number(t) { } export function calendar(year, month, week, day, hour, minute, second, millisecond, format) { - var scale = continuous(), - invert = scale.invert, - domain = scale.domain; - - var formatMillisecond = format(".%L"), - formatSecond = format(":%S"), - formatMinute = format("%I:%M"), - formatHour = format("%I %p"), - formatDay = format("%a %d"), - formatWeek = format("%b %d"), - formatMonth = format("%B"), - formatYear = format("%Y"); - - var tickIntervals = [ + const scale = continuous(); + const invert = scale.invert; + const domain = scale.domain; + + const formatMillisecond = format(".%L"); + const formatSecond = format(":%S"); + const formatMinute = format("%I:%M"); + const formatHour = format("%I %p"); + const formatDay = format("%a %d"); + const formatWeek = format("%b %d"); + const formatMonth = format("%B"); + const formatYear = format("%Y"); + + const tickIntervals = [ [second, 1, durationSecond], [second, 5, 5 * durationSecond], [second, 15, 15 * durationSecond], @@ -73,9 +73,9 @@ export function calendar(year, month, week, day, hour, minute, second, milliseco // based on the extent of the domain and a rough estimate of tick size. // Otherwise, assume interval is already a time interval and use it. if (typeof interval === "number") { - var target = Math.abs(stop - start) / interval, - i = bisector(function(i) { return i[2]; }).right(tickIntervals, target), - step; + const target = Math.abs(stop - start) / interval; + let i = bisector(function(i) { return i[2]; }).right(tickIntervals, target); + let step; if (i === tickIntervals.length) { step = tickStep(start / durationYear, stop / durationYear, interval); interval = year; @@ -102,11 +102,11 @@ export function calendar(year, month, week, day, hour, minute, second, milliseco }; scale.ticks = function(interval) { - var d = domain(), - t0 = d[0], - t1 = d[d.length - 1], - r = t1 < t0, - t; + const d = domain(); + let t0 = d[0]; + let t1 = d[d.length - 1]; + const r = t1 < t0; + let t; if (r) t = t0, t0 = t1, t1 = t; t = tickInterval(interval, t0, t1); t = t ? t.range(t0, t1 + 1) : []; // inclusive stop @@ -118,7 +118,7 @@ export function calendar(year, month, week, day, hour, minute, second, milliseco }; scale.nice = function(interval) { - var d = domain(); + const d = domain(); return (interval = tickInterval(interval, d[0], d[d.length - 1])) ? domain(nice(d, interval)) : scale;