Skip to content

Commit

Permalink
refactor: rename to toDayDiffInclusive with minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
motss committed Sep 20, 2021
1 parent 56c8404 commit 47a3a3b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
* Compute the date range with given `min` and `max` (inclusive).
* When the difference between `min` and `max` is within 1 day, it will be rounded as 1 day.
*/
export function toDateRange(min: number | Date, max: number | Date): number {
return Math.ceil((+max - +min) / 864e5) + 1;
export function toDayDiffInclusive(min: number | Date, max: number | Date): number {
return 1 + Math.floor((+max - +min) / 864e5);
}
4 changes: 2 additions & 2 deletions src/helpers/to-next-selectable-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { toUTCDate } from 'nodemod/dist/calendar/helpers/to-utc-date.js';

import { navigationKeySetDayNext, navigationKeySetDayPrevious } from '../constants.js';
import type { InferredFromSet } from '../typings.js';
import { toDateRange } from './to-date-range.js';
import { toDayDiffInclusive } from './to-day-diff-inclusive.js';
import type { ToNextSelectableDateInit } from './typings.js';

export function toNextSelectableDate({
Expand All @@ -14,7 +14,7 @@ export function toNextSelectableDate({
minTime,
}: ToNextSelectableDateInit): Date {
// Bail when there is no valid date range (<= 1 day).
if (toDateRange(minTime, maxTime) <= 1) return date;
if (toDayDiffInclusive(minTime, maxTime) <= 1) return date;

const focusedDateTime = +date;

Expand Down
4 changes: 2 additions & 2 deletions src/helpers/to-year-list.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { toDateRange } from './to-date-range.js';
import { toDayDiffInclusive } from './to-day-diff-inclusive.js';

export function toYearList(min: Date, max: Date): number[] {
if (toDateRange(min, max) < 1) return [];
if (toDayDiffInclusive(min, max) < 1) return [];

const fy = min.getUTCFullYear();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { expect } from '@open-wc/testing';

import { toDateRange } from '../../helpers/to-date-range';
import { toDayDiffInclusive } from '../../helpers/to-day-diff-inclusive';
import { messageFormatter } from '../test-utils/message-formatter';

describe(toDateRange.name, () => {
describe(toDayDiffInclusive.name, () => {
type A = [number | Date, number | Date, number];

const cases: A[] = [
Expand All @@ -23,7 +23,7 @@ describe(toDateRange.name, () => {
it(
messageFormatter('returns date range (min=%s, max=%s)', a),
() => {
const result = toDateRange(testMin, testMax);
const result = toDayDiffInclusive(testMin, testMax);

expect(result).equal(expected);
}
Expand Down

0 comments on commit 47a3a3b

Please sign in to comment.