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

Use const/let instead of var #212

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 17 additions & 17 deletions src/band.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down
38 changes: 19 additions & 19 deletions src/continuous.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,25 +16,25 @@ 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)); };
}

// 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]) {
Expand All @@ -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));
};
}
Expand All @@ -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;
Expand Down
38 changes: 20 additions & 18 deletions src/diverging.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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)];
};
}
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
58 changes: 28 additions & 30 deletions src/linear.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,51 @@ 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;
let maxIter = 10;

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;

while (maxIter-- > 0) {
step = tickIncrement(start, stop, count);
}

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);
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 {
break;
}
prestep = step;
}

return scale;
Expand All @@ -60,7 +58,7 @@ export function linearish(scale) {
}

export default function linear() {
var scale = continuous();
const scale = continuous();

scale.copy = function() {
return copy(scale, linear());
Expand Down
40 changes: 20 additions & 20 deletions src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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) : "";
};
Expand All @@ -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());
Expand Down
Loading