feat(shared): add constants and utils

This commit is contained in:
Sergey Krylov 2024-04-18 06:34:11 +03:00
parent bb28d93016
commit afde44bead
6 changed files with 259 additions and 0 deletions

View File

@ -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,
};

View File

@ -0,0 +1,2 @@
export { DAYS_IN_WEEK, MONTHS, WEEK_DAYS } from './date';
export { MONTHS_LABEL, WEEK_DAYS_LABEL } from './locale';

View File

@ -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',
},
},
};

View File

@ -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);
};

View File

@ -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);
});
});

View File

@ -0,0 +1,4 @@
export {
lastDayOfMonthDate,
firstDayOfMonthDate,
} from './date';