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

Allow discontinuity provider to provide its own tick filter #1762

Open
wants to merge 2 commits into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/d3fc-discontinuous-scale/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,4 @@ You can create your own discontinuity provider by providing an object that expos
discontinuities.
+ `offset` - When given a value and an offset, the value should be advanced by the offset value, skipping any discontinuities, to return the final value.
+ `copy` - Creates a copy of the discontinuity provider.
+ `tickFilter` - When given a value, the provided filter function will be used to filter the ticks.
5 changes: 5 additions & 0 deletions packages/d3fc-discontinuous-scale/src/tickFilter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export default function tickFilter(ticks, discontinuityProvider) {

if ("tickFilter" in discontinuityProvider) {
return discontinuityProvider.tickFilter(ticks);
} // use tick filter provided by the discontinuity provider

const discontinuousTicks = ticks.map(discontinuityProvider.clampUp);
if (
discontinuousTicks.length !==
Expand Down
39 changes: 39 additions & 0 deletions packages/d3fc-discontinuous-scale/test/tickFilterSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import tickFilter from '../src/tickFilter';

describe('tickFilter', () => {
it('should clampUp the ticks with the dicontinuity provider', () => {
const ticks = [new Date(), new Date()];
const provider = {
clampUp: () => { }
};

const clampUpSpy = jest.spyOn(provider, "clampUp").mockImplementation((date) => {
date.setHours(12, 0, 0, 0);
return date;
});

const filteredTicks = tickFilter(ticks, provider);

expect(filteredTicks.every(tick => tick.getHours() === 12)).toBeTruthy();
expect(clampUpSpy).toBeCalledTimes(ticks.length);
});

it('should use the tick filter provided by the discontinuity provider', () => {
const ticks = [new Date(), new Date()];
const provider = {
tickFilter: () => { }
};

const tickFilterSpy = jest.spyOn(provider, "tickFilter").mockImplementation((ticks) => {
return ticks.map(date => {
date.setHours(12, 0, 0, 0);
return date;
});
});

const filteredTicks = tickFilter(ticks, provider);

expect(filteredTicks.every(tick => tick.getHours() === 12)).toBeTruthy();
expect(tickFilterSpy).toBeCalledWith(ticks);
});
});