@lumx/react 3.5.4-alpha-optimize-lumx-react-bundle.0 → 3.5.4-alpha-remove-moment.2

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 { Locale } from '@lumx/react/utils/locale/types';
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: Locale): Array<WeekDayInfo> => {
13
+ const iterDate = new Date();
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.code, { 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,37 @@
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('1923-12-25T18:30:00');
25
+ expect(isSameDay(date1, date2)).toBe(false);
26
+ });
27
+
28
+ it('should handle invalid date', () => {
29
+ // Invalid date input are not comparable, so we always return `false`
30
+ // Undefined date
31
+ expect(isSameDay(undefined as any, undefined as any)).toBe(false);
32
+ // Null date
33
+ expect(isSameDay(null as any, new Date())).toBe(false);
34
+ // Invalid date
35
+ expect(isSameDay(new Date('-'), new Date('-'))).toBe(false);
36
+ });
37
+ });
@@ -0,0 +1,11 @@
1
+ import { isDateValid } from '@lumx/react/utils/date/isDateValid';
2
+
3
+ /**
4
+ * Check `date1` is on the same day as `date2`.
5
+ */
6
+ export const isSameDay = (date1: Date, date2: Date) =>
7
+ isDateValid(date1) &&
8
+ isDateValid(date2) &&
9
+ date1.getFullYear() === date2.getFullYear() &&
10
+ date1.getMonth() === date2.getMonth() &&
11
+ date1.getDate() === date2.getDate();
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Get current browser locale.
3
+ */
4
+ export const getCurrentLocale = (): string => navigator.languages?.[0] || navigator.language;
@@ -0,0 +1,17 @@
1
+ import { parseLocale } from '@lumx/react/utils/locale/parseLocale';
2
+
3
+ describe(parseLocale.name, () => {
4
+ it('should parse various locale formats', () => {
5
+ expect(parseLocale('en')).toEqual({ code: 'en', language: 'en' });
6
+ expect(parseLocale('EN')).toEqual({ code: 'en', language: 'en' });
7
+ expect(parseLocale('en-US')).toEqual({ code: 'en-US', language: 'en', region: 'US' });
8
+ expect(parseLocale('en-us')).toEqual({ code: 'en-US', language: 'en', region: 'US' });
9
+ expect(parseLocale('en_us')).toEqual({ code: 'en-US', language: 'en', region: 'US' });
10
+ expect(parseLocale('EN-US')).toEqual({ code: 'en-US', language: 'en', region: 'US' });
11
+ });
12
+
13
+ it('should fail on invalid locale', () => {
14
+ expect(parseLocale('-')).toBe(undefined);
15
+ expect(parseLocale('-foo')).toBe(undefined);
16
+ });
17
+ });
@@ -0,0 +1,23 @@
1
+ import { Locale } from '@lumx/react/utils/locale/types';
2
+
3
+ /**
4
+ * Parse locale code
5
+ * @example
6
+ * parseLocale('EN') // => { code: 'en', language: 'en' }
7
+ * parseLocale('en_us') // => { code: 'en-US', language: 'en', region: 'US' }
8
+ * parseLocale('EN-US') // => { code: 'en-US', language: 'en', region: 'US' }
9
+ */
10
+ export function parseLocale(locale: string): Locale | undefined {
11
+ const [rawLanguage, rawRegion] = locale.split(/[-_]/);
12
+ if (!rawLanguage) {
13
+ return undefined;
14
+ }
15
+ const language = rawLanguage.toLowerCase();
16
+ let region: string | undefined;
17
+ let code = language;
18
+ if (rawRegion) {
19
+ region = rawRegion.toUpperCase();
20
+ code += `-${region}`;
21
+ }
22
+ return { code, region, language };
23
+ }
@@ -0,0 +1,8 @@
1
+ export interface Locale {
2
+ /** ISO locale code `lang-REGION` (ex: `en-US`) */
3
+ code: string;
4
+ /** ISO locale language code (ex: `en`) */
5
+ language: string;
6
+ /** ISO locale region code (ex: `US`) */
7
+ region?: string;
8
+ }