Skip to content

Commit

Permalink
break out default ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Mar 27, 2019
1 parent 2106a96 commit 0e7f588
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/layercake.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Canvas from './layouts/Canvas.html';
import Webgl from './layouts/Webgl.html';

import defaultScales from './settings/defaultScales.js';
import getDefaultRange from './settings/getDefaultRange.js';
import calcExtents from './lib/calcExtents.js';
import getActiveKeys from './utils/getActiveKeys.js';
import partialDomain from './utils/partialDomain.js';
Expand Down Expand Up @@ -192,13 +193,11 @@ export default class LayerCakeStore extends Store {
if (domains === null) {
return null;
}
// TODO, DRYer and more explicit version of what ranges are defined as, including fallbacks
const defaultRanges = {
x: settings.reverseX ? [width, 0] : typeof settings.xRange === 'function' ? settings.xRange({ width, height }) : [0, width],
y: settings.reverseY ? [height, 0] : typeof settings.yRange === 'function' ? settings.yRange({ width, height }) : [0, height],
r: !settings.rRange ? [1, 25] : typeof settings.rRange === 'function' ? settings.rRange({ width, height }) : settings.rRange
};

const defaultRanges = getDefaultRange(s, settings, width, height);

const scale = settings[thisScale] ? settings[thisScale].copy() : defaultScales[s]();

scale
.domain(partialDomain(domains[s], thisDoughmain)) // on creation, `thisDoughmain` will already have any nulls filled in but if we set it via the store it might not, so rerun it through partialDomain
.range(defaultRanges[s]);
Expand Down
9 changes: 9 additions & 0 deletions src/settings/getDefaultRange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const getRange = {
x: (settings, width, height) => settings.reverseX ? [width, 0] : typeof settings.xRange === 'function' ? settings.xRange({ width, height }) : (settings.xRange || [0, width]),
y: (settings, width, height) => settings.reverseY ? [height, 0] : typeof settings.yRange === 'function' ? settings.yRange({ width, height }) : (settings.yRange || [0, height]),
r: (settings, width, height) => !settings.rRange ? [1, 25] : typeof settings.rRange === 'function' ? settings.rRange({ width, height }) : settings.rRange
};

export default function getDefaultRange (s, settings, width, height) {
return getRange[s](settings, width, height);
}
45 changes: 45 additions & 0 deletions test/getDefaultRange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* globals describe it */
import getDefaultRange from '../src/settings/getDefaultRange.js';
import * as assert from 'assert';

const name = 'getDefaultRange';

const width = 900;
const height = 500;

const tests = [
/* --------------------------------------------
* Defaults
*/
{ args: ['x', { }, width, height], expected: [0, width] },
{ args: ['y', { }, width, height], expected: [0, height] },
{ args: ['r', { }, width, height], expected: [1, 25] },
/* --------------------------------------------
* Reverse it
*/
{ args: ['x', { reverseX: true }, width, height], expected: [width, 0] },
{ args: ['y', { reverseY: true }, width, height], expected: [height, 0] },
/* --------------------------------------------
* Set a manual default
*/
{ args: ['x', { xRange: [-100, 100] }, width, height], expected: [-100, 100] },
{ args: ['y', { yRange: [-100, 100] }, width, height], expected: [-100, 100] },
{ args: ['r', { rRange: [-100, 100] }, width, height], expected: [-100, 100] },
/* --------------------------------------------
* Functions!
*/
{ args: ['x', { xRange: ({ width, height }) => [0, width / 2] }, width, height], expected: [0, width / 2] },
{ args: ['y', { yRange: ({ width, height }) => [0, height / 2] }, width, height], expected: [0, height / 2] },
{ args: ['r', { rRange: ({ width, height }) => [width / 2, height / 2] }, width, height], expected: [width / 2, height / 2] }
];

describe(name, function () {
tests.forEach(test => {
describe(JSON.stringify(test.args), function () {
it(`should equal ${JSON.stringify(test.expected)}`, function () {
const actual = getDefaultRange(...test.args);
assert.deepStrictEqual(actual, test.expected);
});
});
});
});

0 comments on commit 0e7f588

Please sign in to comment.