@lumx/react 3.5.2 → 3.5.4-alpha-remove-moment.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,32 @@
1
+ import { getCurrentLocale } from '../locale/getCurrentLocale';
2
+ import { getFirstDayOfWeek } from './getFirstDayOfWeek';
3
+
4
+ export type WeekDayInfo = { letter: string; number: number };
5
+
6
+ export const DAYS_PER_WEEK = 7;
7
+
8
+ /**
9
+ * List week days (based on locale) with the week day letter (ex: "M" for "Monday") and week day number
10
+ * (0-based index starting on Sunday).
11
+ */
12
+ export const getWeekDays = (locale = getCurrentLocale(), referenceDate = new Date()): Array<WeekDayInfo> => {
13
+ const iterDate = new Date(referenceDate.getTime());
14
+ const firstDay = getFirstDayOfWeek(locale) ?? 1;
15
+
16
+ // Go to start of the week
17
+ const offset = firstDay - iterDate.getDay();
18
+ iterDate.setDate(iterDate.getDate() + offset);
19
+
20
+ // Iterate through the days of the week
21
+ const weekDays: Array<WeekDayInfo> = [];
22
+ for (let i = 0; i < DAYS_PER_WEEK; i++) {
23
+ // Single letter week day (ex: "M" for "Monday", "L" for "Lundi", etc.)
24
+ const letter = iterDate.toLocaleDateString(locale, { weekday: 'narrow' });
25
+ // Day number (1-based index starting on Monday)
26
+ const number = iterDate.getDay();
27
+
28
+ weekDays.push({ letter, number });
29
+ iterDate.setDate(iterDate.getDate() + 1);
30
+ }
31
+ return weekDays;
32
+ };
@@ -0,0 +1,15 @@
1
+ import { isDateValid } from '@lumx/react/utils/date/isDateValid';
2
+
3
+ describe(isDateValid.name, () => {
4
+ it('should mark `undefined` as invalid', () => {
5
+ expect(isDateValid(undefined)).toBe(false);
6
+ });
7
+
8
+ it('should mark invalid date as invalid', () => {
9
+ expect(isDateValid(new Date('foo'))).toBe(false);
10
+ });
11
+
12
+ it('should mark valid date as valid', () => {
13
+ expect(isDateValid(new Date())).toBe(true);
14
+ });
15
+ });
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Check if given date is valid.
3
+ */
4
+ export const isDateValid = (date?: Date) => date instanceof Date && !Number.isNaN(date.getTime());
@@ -0,0 +1,27 @@
1
+ import { isSameDay } from '@lumx/react/utils/date/isSameDay';
2
+
3
+ describe(isSameDay, () => {
4
+ it('should return true for same dates', () => {
5
+ const date1 = new Date('2023-08-26T12:00:00');
6
+ const date2 = new Date('2023-08-26T15:30:00');
7
+ expect(isSameDay(date1, date2)).toBe(true);
8
+ });
9
+
10
+ it('should return false for different dates', () => {
11
+ const date1 = new Date('2023-08-26T10:00:00');
12
+ const date2 = new Date('2023-08-27T10:00:00');
13
+ expect(isSameDay(date1, date2)).toBe(false);
14
+ });
15
+
16
+ it('should handle different months', () => {
17
+ const date1 = new Date('2023-08-15T08:00:00');
18
+ const date2 = new Date('2023-09-15T08:00:00');
19
+ expect(isSameDay(date1, date2)).toBe(false);
20
+ });
21
+
22
+ it('should handle different years', () => {
23
+ const date1 = new Date('2022-12-25T18:30:00');
24
+ const date2 = new Date('2023-12-25T18:30:00');
25
+ expect(isSameDay(date1, date2)).toBe(false);
26
+ });
27
+ });
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Check `date1` is on the same day as `date2`.
3
+ */
4
+ export const isSameDay = (date1: Date, date2: Date) =>
5
+ date1.getDate() === date2.getDate() &&
6
+ date1.getMonth() === date2.getMonth() &&
7
+ date1.getFullYear() === date2.getFullYear();
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Get current browser locale.
3
+ */
4
+ export const getCurrentLocale = (): string => navigator.languages?.[0] || navigator.language;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Get the language part of a locale code.
3
+ * ex: `en-us` => `en`
4
+ */
5
+ export const getLocaleLang = (locale: string) => locale.split('-')[0];