Skip to content

Commit a8d263d

Browse files
committed
refactor(core): ♻️ clean the code to be more reddeable and scalable - update deps to latest version
1 parent f477273 commit a8d263d

File tree

20 files changed

+1571
-616
lines changed

20 files changed

+1571
-616
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: 🚀 pkg-pr-new
2+
on:
3+
push:
4+
pull_request:
5+
branches-ignore: main
6+
paths-ignore:
7+
- "website/*"
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v2
16+
17+
- uses: pnpm/action-setup@v4
18+
with:
19+
version: 9
20+
21+
- name: Install dependencies
22+
run: pnpm install --frozen-lockfile
23+
24+
- name: Build
25+
run: pnpm run build
26+
27+
- run: npx pkg-pr-new publish

bun.lock

Lines changed: 922 additions & 0 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "qwik-date",
33
"version": "1.0.0",
4+
"sideEffects": false,
45
"description": "Qwik calendar, simple integration.",
56
"keywords": [
67
"qwik",
@@ -76,7 +77,7 @@
7677
"build": "qwik build",
7778
"build.lib": "vite build --mode lib",
7879
"build.types": "tsc --emitDeclarationOnly -p ./tsconfig.lib.json",
79-
"dev": "vite --mode ssr",
80+
"dev": "vite --force --mode ssr",
8081
"dev.debug": "node --inspect-brk ./node_modules/vite/bin/vite.js --mode ssr --force",
8182
"fmt": "prettier --write .",
8283
"fmt.check": "prettier --check .",
@@ -88,19 +89,18 @@
8889
"qwik": "qwik"
8990
},
9091
"dependencies": {
91-
"@floating-ui/dom": "^1.6.8"
92+
"@floating-ui/dom": "^1.6.13"
9293
},
9394
"devDependencies": {
9495
"@biomejs/biome": "1.8.3",
95-
"@builder.io/qwik": "^1.7.3",
96-
"@playwright/test": "^1.45.3",
97-
"@types/node": "^22.0.2",
98-
"bumpp": "9.4.1",
99-
"np": "^10.0.7",
100-
"tsx": "^4.16.3",
101-
"typescript": "^5.5.4",
102-
"undici": "^6.19.5",
103-
"vite": "^5.3.5",
96+
"@builder.io/qwik": "1.12.0",
97+
"@playwright/test": "^1.50.0",
98+
"@types/node": "^22.12.0",
99+
"bumpp": "^10.0.1",
100+
"np": "^10.2.0",
101+
"typescript": "^5.7.3",
102+
"undici": "^7.3.0",
103+
"vite": "^5.4.14",
104104
"vite-tsconfig-paths": "^4.3.2"
105105
}
106-
}
106+
}

src/lib/core/constants.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Locale } from './types';
22

3+
/** Full month names by locale */
34
export const MONTHS_LG = {
45
en: [
56
'January',
@@ -36,6 +37,7 @@ export const MONTHS_SM = {
3637
es: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'],
3738
} as const;
3839

40+
/** Accessibility labels for screen readers */
3941
export const ARIA_LABELS = {
4042
en: {
4143
previous: 'go to previous month',
@@ -49,6 +51,7 @@ export const ARIA_LABELS = {
4951
},
5052
} as const;
5153

54+
/** Weekday names by locale starting from Sunday */
5255
export const WEEKDAYS = {
5356
en: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
5457
es: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'],
@@ -58,3 +61,20 @@ export const TRIGGER_LABELS = {
5861
en: 'Select a date',
5962
es: 'Seleccionar una fecha',
6063
} as const;
64+
65+
/** Regex pattern for YYYY-MM-DD date validation */
66+
export const DATE_REGEX = /^\d{4}-(0[1-9]|1[0-2])-\d{2}$/;
67+
68+
/** Keyboard keys used for calendar navigation */
69+
export const ACTION_KEYS = [
70+
'enter',
71+
' ',
72+
'arrowup',
73+
'arrowdown',
74+
'arrowleft',
75+
'arrowright',
76+
'home',
77+
'end',
78+
'pageup',
79+
'pagedown',
80+
];

src/lib/core/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './types';
22
export * from './constants';
3-
export * from './utils/date-generator';
4-
export * from './utils/get-week-number';
3+
export * from './utils/date';
4+
export * from './utils/keyboard';
5+
export * from './types';

src/lib/core/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ export type DateFormat =
99

1010
export type Locale = 'en' | 'es';
1111
export type Month = '01' | '02' | '03' | '04' | '05' | '06' | '07' | '08' | '09' | '10' | '11' | '12';
12+
13+
/** Date string in YYYY-MM-DD format */
14+
export type LocalDate = `${number}-${number}-${number}`;

src/lib/core/utils/date-generator.ts

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/lib/core/utils/date.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import type { LocalDate, Month } from '../types';
2+
3+
type DaysArrParams = {
4+
month: Month;
5+
year: string;
6+
fullWeeks?: boolean;
7+
};
8+
9+
const formatDate = (y: number, m: number, d: number) => {
10+
const mm = m < 10 ? `0${m}` : m;
11+
const dd = d < 10 ? `0${d}` : d;
12+
return `${y}-${mm}-${dd}`;
13+
};
14+
15+
const getCalendarStartDate = (year: number, month: number) => {
16+
const firstDayOfMonth = new Date(year, month - 1, 1);
17+
const firstDayOfWeek = firstDayOfMonth.getDay();
18+
const startDate = new Date(firstDayOfMonth);
19+
startDate.setDate(1 - firstDayOfWeek);
20+
return startDate;
21+
};
22+
23+
const generateFullWeeks = (year: number, month: number) => {
24+
const startDate = getCalendarStartDate(year, month);
25+
const days: string[] = [];
26+
27+
for (let i = 0; i < 42; i++) {
28+
const currentDate = new Date(startDate);
29+
currentDate.setDate(startDate.getDate() + i);
30+
days.push(formatDate(currentDate.getFullYear(), currentDate.getMonth() + 1, currentDate.getDate()));
31+
}
32+
33+
return Array.from({ length: 6 }, (_, i) => days.slice(i * 7, (i + 1) * 7));
34+
};
35+
36+
const generatePartialWeeks = (year: number, month: number) => {
37+
const firstDayOfMonth = new Date(year, month - 1, 1);
38+
// Fix: Handle Sunday (0) by converting it to 7, otherwise use the day number
39+
const firstDayOfWeek = firstDayOfMonth.getDay();
40+
console.log({ firstDayOfWeek, firstDayOfMonth });
41+
const daysInMonth = new Date(year, month, 0).getDate();
42+
43+
const previousDays = Array(firstDayOfWeek).fill(null);
44+
const currentDays = Array.from({ length: daysInMonth }, (_, i) => formatDate(year, month, i + 1));
45+
const totalDays = previousDays.length + currentDays.length;
46+
const trailingNulls = Array((7 - (totalDays % 7)) % 7).fill(null);
47+
48+
const allDays = [...previousDays, ...currentDays, ...trailingNulls];
49+
return Array.from({ length: allDays.length / 7 }, (_, i) => allDays.slice(i * 7, (i + 1) * 7));
50+
};
51+
52+
export const generateCalendarDays = ({ month, year, fullWeeks = false }: DaysArrParams) => {
53+
const numericYear = Number.parseInt(year, 10);
54+
const numericMonth = Number.parseInt(month, 10);
55+
56+
return fullWeeks ? generateFullWeeks(numericYear, numericMonth) : generatePartialWeeks(numericYear, numericMonth);
57+
};
58+
59+
/**
60+
* Calculates ISO week number for a date
61+
* @param date - Date in LocalDate format
62+
* @returns ISO week number
63+
*/
64+
export const getISOWeekNumber = (date: LocalDate): number => {
65+
const d = new Date(date);
66+
d.setHours(0, 0, 0, 0);
67+
d.setDate(d.getDate() + 4 - (d.getDay() || 7));
68+
const yearStart = new Date(d.getFullYear(), 0, 1);
69+
return Math.ceil(((d.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);
70+
};

src/lib/core/utils/get-week-number.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)