Skip to content

Commit

Permalink
refactor(calendar-utils): use Moment types (#1873)
Browse files Browse the repository at this point in the history
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
adnasa and kodiakhq[bot] authored Apr 22, 2021
1 parent bf77496 commit 72667c8
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-bugs-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-uikit/calendar-time-utils': patch
---

migrate to TypeScript, use Moment Types
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,34 @@
// respects the timezone and uses full iso date strings instead of
// just YYYY-MM-DD.

import moment from 'moment-timezone';
import moment, {
DurationInputArg1,
MomentInput,
LocaleSpecifier,
} from 'moment-timezone';
import { parseTime } from '@commercetools-uikit/utils';

export const getDaysInMonth = (day, timeZone) =>
type ParsedTime = {
hours: number;
minutes: number;
seconds: number;
milliseconds: number;
};

export const getDaysInMonth = (day: MomentInput, timeZone: string) =>
moment.tz(day, timeZone).daysInMonth();
export const changeDateInMonth = (day, timeZone, dayOfMonth) =>
moment.tz(day, timeZone).date(dayOfMonth).toISOString();

export const changeTime = (dateString, timeZone, parsedTime) => {
export const changeDateInMonth = (
day: MomentInput,
timeZone: string,
dayOfMonth: number
) => moment.tz(day, timeZone).date(dayOfMonth).toISOString();

export const changeTime = (
dateString: string,
timeZone: string,
parsedTime: ParsedTime
) => {
const date = moment.tz(dateString, timeZone);
date.hours(parsedTime.hours);
date.minutes(parsedTime.minutes);
Expand All @@ -20,64 +39,85 @@ export const changeTime = (dateString, timeZone, parsedTime) => {
return date.toISOString();
};

export const getPreviousDay = (day) =>
export const getPreviousDay = (day: MomentInput) =>
moment(day).subtract(1, 'day').format('YYYY-MM-DD');

export const formatTime = (day, locale, timeZone) => {
export const formatTime = (
day: MomentInput,
locale: LocaleSpecifier,
timeZone: string
) => {
const date = moment.tz(day, timeZone).locale(locale);
if (date.milliseconds()) return date.format('HH:mm:ss.SSS');
if (date.seconds()) return date.format('LTS'); // 5:13:51 PM
return date.format('LT'); // 5:13 PM
};

export const formatDate = (day, locale, timeZone) => {
export const formatDate = (
day: MomentInput,
locale: string,
timeZone: string
) => {
const date = moment(day, moment.ISO_8601, locale).tz(timeZone).format('L');
const time = formatTime(day, locale, timeZone);
return `${date} ${time}`;
};

export const getDateInMonth = (day, timeZone) =>
export const getDateInMonth = (day: MomentInput, timeZone: string): number =>
moment.tz(day, timeZone).date();

export const getToday = (timeZone) =>
export const getToday = (timeZone: string) =>
moment.tz(timeZone).startOf('day').toISOString();

export const changeMonth = (day, timeZone, delta) =>
moment.tz(day, timeZone).add(delta, 'month').toISOString();

export const getPaddingDayCount = (day, locale, timeZone) => {
export const changeMonth = (
day: MomentInput,
timeZone: string,
delta: DurationInputArg1
) => moment.tz(day, timeZone).add(delta, 'month').toISOString();

export const getPaddingDayCount = (
day: MomentInput,
locale: string,
timeZone: string
) => {
const firstDayOfWeek = moment.localeData(locale).firstDayOfWeek();
const firstDayOfMonth = moment
.tz(day, timeZone)
// .tz(timeZone)
.startOf('month')
.day();
const firstDayOfMonth = moment.tz(day, timeZone).startOf('month').day();

// ensure number is always positive
return (firstDayOfMonth - firstDayOfWeek + 7) % 7;
};

export const getWeekdayNames = (locale) => {
export const getWeekdayNames = (locale: string) => {
const weekDays = moment.localeData(locale).weekdaysMin();
const firstDay = moment.localeData(locale).firstDayOfWeek();
return [...weekDays.slice(firstDay), ...weekDays.slice(0, firstDay)];
};

export const getStartOf = (day, timeZone) =>
export const getStartOf = (day: MomentInput, timeZone: string) =>
moment.tz(day, timeZone).startOf('day').toISOString();

export const getMonthCalendarLabel = (day, locale) =>
export const getMonthCalendarLabel = (day: MomentInput, locale: string) =>
moment(day, moment.ISO_8601, locale).format('MMMM');
export const getYearCalendarLabel = (day, locale) =>
export const getYearCalendarLabel = (day: MomentInput, locale: string) =>
moment(day, moment.ISO_8601, locale).format('YYYY');
export const isSameDay = (a, b) => moment(a).isSame(b, 'day');
export const getCalendarDayLabel = (day, timeZone) =>
export const isSameDay = (a: MomentInput, b: MomentInput) =>
moment(a).isSame(b, 'day');
export const getCalendarDayLabel = (day: MomentInput, timeZone: string) =>
moment.tz(day, timeZone).format('D');

export const createItemDateTimeToString = (locale, timeZone) => (item) =>
item ? formatDate(item, locale, timeZone) : '';

export const createCalendarItems = (day, timeString, intl, timeZone) => {
export const createItemDateTimeToString = (
locale: string,
timeZone: string
) => (item: string) => (item ? formatDate(item, locale, timeZone) : '');

export const createCalendarItems = (
day: MomentInput,
timeString: string,
// @deprecated
// @ts-ignore
intl,
timeZone: string
) => {
const parsedTime = parseTime(timeString);

return Array.from({ length: getDaysInMonth(day, timeZone) }).map((_, i) => {
Expand All @@ -90,12 +130,16 @@ export const createCalendarItems = (day, timeString, intl, timeZone) => {
});
};

export const createSuggestedItems = (inputValue, timeZone) => {
export const createSuggestedItems = (inputValue: string, timeZone: string) => {
if (inputValue.startsWith('t')) return [getToday(timeZone)];
return [];
};

export const parseInputText = (text, locale, timeZone) => {
export const parseInputText = (
text: string,
locale: string,
timeZone: string
) => {
const parts = text.split(' ');
const dateString = parts[0];
const timeString = parts.slice(1).join(' ');
Expand Down
File renamed without changes.
File renamed without changes.

1 comment on commit 72667c8

@vercel
Copy link

@vercel vercel bot commented on 72667c8 Apr 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.