Skip to content

Commit

Permalink
feat: add clampValue with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
motss committed Oct 3, 2021
1 parent e120bb9 commit 49d3eac
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
19 changes: 9 additions & 10 deletions src/helpers/adjust-out-of-range-value.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
export function adjustOutOfRangeValue(min: Date, max: Date, date: Date): Date {
const minTime = min.getTime();
const maxTime = max.getTime();
const currentTime = date.getTime();

let newValue = currentTime;
import { clampValue } from './clamp-value.js';

if (currentTime < minTime) newValue = minTime;
if (currentTime > maxTime) newValue = maxTime;

return new Date(newValue);
export function adjustOutOfRangeValue(min: Date, max: Date, date: Date): Date {
return new Date(
clampValue(
min.getTime(),
max.getTime(),
date.getTime()
)
);
}
10 changes: 10 additions & 0 deletions src/helpers/clamp-value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function clampValue(
min: number,
max: number,
value: number
): number {
return Math.min(
Math.max(min, value),
max
);
}
26 changes: 26 additions & 0 deletions src/tests/helpers/clamp-value.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect } from '@open-wc/testing';

import { clampValue } from '../../helpers/clamp-value';
import { messageFormatter } from '../test-utils/message-formatter';

describe(clampValue.name, () => {
type A = [number, number, number, number];
const cases: A[] = [
[100, 300, 100, 100],
[100, 300, 200, 200],
[100, 300, 301, 300],
[100, 300, 99, 100],
];
cases.forEach((a) => {
const [testMin, testMax, testValue, expected] = a;

it(
messageFormatter('clamps value (min=%s, max=%s, value=%s', a),
() => {
const result = clampValue(testMin, testMax, testValue);

expect(result).equal(expected);
}
)
});
});
3 changes: 1 addition & 2 deletions src/tests/helpers/dispatch-custom-event.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { expect } from '@open-wc/testing';

import { dateValidator } from '../../helpers/date-validator';
import { dispatchCustomEvent } from '../../helpers/dispatch-custom-event';
import type { SupportedCustomEvent } from '../../typings';
import { messageFormatter } from '../test-utils/message-formatter';

type A = [SupportedCustomEvent['changed'] | undefined, SupportedCustomEvent['changed'] | null];

describe(dateValidator.name, () => {
describe(dispatchCustomEvent.name, () => {
const cases: A[] = [
[undefined, null],
[
Expand Down
6 changes: 2 additions & 4 deletions src/year-grid/to-next-selected-year.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { clampValue } from '../helpers/clamp-value.js';
import { keyArrowDown, keyArrowLeft, keyArrowRight, keyArrowUp, keyEnd, keyHome } from '../key-values.js';
import type { ToNextSelectableYearInit } from './typings.js';

Expand Down Expand Up @@ -32,8 +33,5 @@ export function toNextSelectedYear({
return year;
}

return Math.min(
Math.max(min.getUTCFullYear(), newYear),
max.getUTCFullYear()
);
return clampValue(min.getUTCFullYear(), max.getFullYear(), newYear);
}

0 comments on commit 49d3eac

Please sign in to comment.