diff --git a/src/shared/constants/date.js b/src/shared/constants/date.js new file mode 100644 index 0000000..5ae005e --- /dev/null +++ b/src/shared/constants/date.js @@ -0,0 +1,26 @@ +export const MONTHS = { + JANUARY: 0, + FEBRUARY: 1, + MARCH: 2, + APRIL: 3, + MAY: 4, + JUNE: 5, + JULY: 6, + AUGUST: 7, + SEPTEMBER: 8, + OCTOBER: 9, + NOVEMBER: 10, + DECEMBER: 11, +}; + +export const DAYS_IN_WEEK = 7; + +export const WEEK_DAYS = { + SUNDAY: 0, + MONDAY: 1, + TUESDAY: 2, + WEDNESDAY: 3, + THURSDAY: 4, + FRIDAY: 5, + SATURDAY: 6, +}; diff --git a/src/shared/constants/index.js b/src/shared/constants/index.js new file mode 100644 index 0000000..fdc476c --- /dev/null +++ b/src/shared/constants/index.js @@ -0,0 +1,2 @@ +export { DAYS_IN_WEEK, MONTHS, WEEK_DAYS } from './date'; +export { MONTHS_LABEL, WEEK_DAYS_LABEL } from './locale'; diff --git a/src/shared/constants/locale.js b/src/shared/constants/locale.js new file mode 100644 index 0000000..10d3edc --- /dev/null +++ b/src/shared/constants/locale.js @@ -0,0 +1,125 @@ +import { MONTHS, WEEK_DAYS } from './date'; + +export const MONTHS_LABEL = { + [MONTHS.JANUARY]: { + ru: 'Январь', + en: 'January', + }, + [MONTHS.FEBRUARY]: { + ru: 'Февраль', + en: 'February', + }, + [MONTHS.MARCH]: { + ru: 'Март', + en: 'March', + }, + [MONTHS.APRIL]: { + ru: 'Апрель', + en: 'April', + }, + [MONTHS.MAY]: { + ru: 'Май', + en: 'May', + }, + [MONTHS.JUNE]: { + ru: 'Июнь', + en: 'June', + }, + [MONTHS.JULY]: { + ru: 'Июль', + en: 'July', + }, + [MONTHS.AUGUST]: { + ru: 'Август', + en: 'August', + }, + [MONTHS.SEPTEMBER]: { + ru: 'Сентябрь', + en: 'September', + }, + [MONTHS.OCTOBER]: { + ru: 'Октябрь', + en: 'October', + }, + [MONTHS.NOVEMBER]: { + ru: 'Ноябрь', + en: 'November', + }, + [MONTHS.DECEMBER]: { + ru: 'Декабрь', + en: 'December', + }, +}; + +export const WEEK_DAYS_LABEL = { + [WEEK_DAYS.SUNDAY]: { + ru: { + full: 'Воскресенье', + short: 'Вс', + }, + en: { + full: 'Sunday', + short: 'Sun', + }, + }, + [WEEK_DAYS.MONDAY]: { + ru: { + full: 'Понедельник', + short: 'Пн', + }, + en: { + full: 'Monday', + short: 'Mon', + }, + }, + [WEEK_DAYS.TUESDAY]: { + ru: { + full: 'Вторник', + short: 'Вт', + }, + en: { + full: 'Tuesday', + short: 'Tue', + }, + }, + [WEEK_DAYS.WEDNESDAY]: { + ru: { + full: 'Среда', + short: 'Ср', + }, + en: { + full: 'Wednesday', + short: 'Wed', + }, + }, + [WEEK_DAYS.THURSDAY]: { + ru: { + full: 'Четверг', + short: 'Чт', + }, + en: { + full: 'Thursday', + short: 'Thu', + }, + }, + [WEEK_DAYS.FRIDAY]: { + ru: { + full: 'Пятница', + short: 'Пт', + }, + en: { + full: 'Friday', + short: 'Fri', + }, + }, + [WEEK_DAYS.SATURDAY]: { + ru: { + full: 'Суббота', + short: 'Сб', + }, + en: { + full: 'Saturday', + short: 'Sat', + }, + }, +}; diff --git a/src/shared/utils/date/date.js b/src/shared/utils/date/date.js new file mode 100644 index 0000000..0ed9892 --- /dev/null +++ b/src/shared/utils/date/date.js @@ -0,0 +1,13 @@ +export const firstDayOfMonthDate = (date) => { + const monthIndex = date.getMonth(); + const year = date.getFullYear(); + + return new Date(year, monthIndex, 1); +}; + +export const lastDayOfMonthDate = (date) => { + const monthIndex = date.getMonth(); + const year = date.getFullYear(); + + return new Date(year, monthIndex + 1, 0); +}; diff --git a/src/shared/utils/date/date.spec.js b/src/shared/utils/date/date.spec.js new file mode 100644 index 0000000..fb49beb --- /dev/null +++ b/src/shared/utils/date/date.spec.js @@ -0,0 +1,89 @@ +import { firstDayOfMonthDate, lastDayOfMonthDate } from './date'; + +describe('Check firstDayOfMonthDate func', () => { + test('Check middle of month', () => { + const date = new Date(2022, 7, 15); + const result = new Date(2022, 7, 1); + expect(firstDayOfMonthDate(date)).toEqual(result); + }); + + test('Check end of year', () => { + const date = new Date(2024, 11, 31); + const result = new Date(2024, 11, 1); + expect(firstDayOfMonthDate(date)).toEqual(result); + }); + + test('Check start of year', () => { + const date = new Date(2024, 0, 0); + const result = new Date(2023, 11, 1); + expect(firstDayOfMonthDate(date)).toEqual(result); + }); + + test('Check with 0 date', () => { + const date = new Date(2023, 12, 0); + const result = new Date(2023, 11, 1); + expect(firstDayOfMonthDate(date)).toEqual(result); + }); + + test('Check with extra month', () => { + const date = new Date(2023, 12, 1); + const result = new Date(2024, 0, 1); + expect(firstDayOfMonthDate(date)).toEqual(result); + }); + + test('Check with extra date', () => { + const date = new Date(2023, 11, 33); + const result = new Date(2024, 0, 1); + expect(firstDayOfMonthDate(date)).toEqual(result); + }); + + test('Check with extra date and month', () => { + const date = new Date(2023, 12, 33); + const result = new Date(2024, 1, 1); + expect(firstDayOfMonthDate(date)).toEqual(result); + }); +}); + +describe('Check lastDayOfMonthDate func', () => { + test('Check middle of month', () => { + const date = new Date(2022, 7, 15); + const result = new Date(2022, 7, 31); + expect(lastDayOfMonthDate(date)).toEqual(result); + }); + + test('Check end of year', () => { + const date = new Date(2024, 11, 31); + const result = new Date(2024, 11, 31); + expect(lastDayOfMonthDate(date)).toEqual(result); + }); + + test('Check start of year', () => { + const date = new Date(2024, 0, 0); + const result = new Date(2023, 11, 31); + expect(lastDayOfMonthDate(date)).toEqual(result); + }); + + test('Check with 0 date', () => { + const date = new Date(2023, 12, 0); + const result = new Date(2023, 11, 31); + expect(lastDayOfMonthDate(date)).toEqual(result); + }); + + test('Check with extra month', () => { + const date = new Date(2023, 12, 1); + const result = new Date(2024, 0, 31); + expect(lastDayOfMonthDate(date)).toEqual(result); + }); + + test('Check with extra date', () => { + const date = new Date(2023, 11, 33); + const result = new Date(2024, 0, 31); + expect(lastDayOfMonthDate(date)).toEqual(result); + }); + + test('Check with extra date and month', () => { + const date = new Date(2023, 12, 33); + const result = new Date(2024, 1, 29); + expect(lastDayOfMonthDate(date)).toEqual(result); + }); +}); diff --git a/src/shared/utils/date/index.js b/src/shared/utils/date/index.js new file mode 100644 index 0000000..560d0e6 --- /dev/null +++ b/src/shared/utils/date/index.js @@ -0,0 +1,4 @@ +export { + lastDayOfMonthDate, + firstDayOfMonthDate, +} from './date';