Skip to content

Commit

Permalink
refactor(core): ♻️ clean the code to be more reddeable and scalable -…
Browse files Browse the repository at this point in the history
… update deps to latest version
  • Loading branch information
diecodev committed Jan 30, 2025
1 parent f477273 commit a8d263d
Show file tree
Hide file tree
Showing 20 changed files with 1,571 additions and 616 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/commit-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: 🚀 pkg-pr-new
on:
push:
pull_request:
branches-ignore: main
paths-ignore:
- "website/*"

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- uses: pnpm/action-setup@v4
with:
version: 9

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Build
run: pnpm run build

- run: npx pkg-pr-new publish
922 changes: 922 additions & 0 deletions bun.lock

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "qwik-date",
"version": "1.0.0",
"sideEffects": false,
"description": "Qwik calendar, simple integration.",
"keywords": [
"qwik",
Expand Down Expand Up @@ -76,7 +77,7 @@
"build": "qwik build",
"build.lib": "vite build --mode lib",
"build.types": "tsc --emitDeclarationOnly -p ./tsconfig.lib.json",
"dev": "vite --mode ssr",
"dev": "vite --force --mode ssr",
"dev.debug": "node --inspect-brk ./node_modules/vite/bin/vite.js --mode ssr --force",
"fmt": "prettier --write .",
"fmt.check": "prettier --check .",
Expand All @@ -88,19 +89,18 @@
"qwik": "qwik"
},
"dependencies": {
"@floating-ui/dom": "^1.6.8"
"@floating-ui/dom": "^1.6.13"
},
"devDependencies": {
"@biomejs/biome": "1.8.3",
"@builder.io/qwik": "^1.7.3",
"@playwright/test": "^1.45.3",
"@types/node": "^22.0.2",
"bumpp": "9.4.1",
"np": "^10.0.7",
"tsx": "^4.16.3",
"typescript": "^5.5.4",
"undici": "^6.19.5",
"vite": "^5.3.5",
"@builder.io/qwik": "1.12.0",
"@playwright/test": "^1.50.0",
"@types/node": "^22.12.0",
"bumpp": "^10.0.1",
"np": "^10.2.0",
"typescript": "^5.7.3",
"undici": "^7.3.0",
"vite": "^5.4.14",
"vite-tsconfig-paths": "^4.3.2"
}
}
}
20 changes: 20 additions & 0 deletions src/lib/core/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Locale } from './types';

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

/** Accessibility labels for screen readers */
export const ARIA_LABELS = {
en: {
previous: 'go to previous month',
Expand All @@ -49,6 +51,7 @@ export const ARIA_LABELS = {
},
} as const;

/** Weekday names by locale starting from Sunday */
export const WEEKDAYS = {
en: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
es: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'],
Expand All @@ -58,3 +61,20 @@ export const TRIGGER_LABELS = {
en: 'Select a date',
es: 'Seleccionar una fecha',
} as const;

/** Regex pattern for YYYY-MM-DD date validation */
export const DATE_REGEX = /^\d{4}-(0[1-9]|1[0-2])-\d{2}$/;

/** Keyboard keys used for calendar navigation */
export const ACTION_KEYS = [
'enter',
' ',
'arrowup',
'arrowdown',
'arrowleft',
'arrowright',
'home',
'end',
'pageup',
'pagedown',
];
5 changes: 3 additions & 2 deletions src/lib/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './types';
export * from './constants';
export * from './utils/date-generator';
export * from './utils/get-week-number';
export * from './utils/date';
export * from './utils/keyboard';
export * from './types';
3 changes: 3 additions & 0 deletions src/lib/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ export type DateFormat =

export type Locale = 'en' | 'es';
export type Month = '01' | '02' | '03' | '04' | '05' | '06' | '07' | '08' | '09' | '10' | '11' | '12';

/** Date string in YYYY-MM-DD format */
export type LocalDate = `${number}-${number}-${number}`;
80 changes: 0 additions & 80 deletions src/lib/core/utils/date-generator.ts

This file was deleted.

70 changes: 70 additions & 0 deletions src/lib/core/utils/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { LocalDate, Month } from '../types';

type DaysArrParams = {
month: Month;
year: string;
fullWeeks?: boolean;
};

const formatDate = (y: number, m: number, d: number) => {
const mm = m < 10 ? `0${m}` : m;
const dd = d < 10 ? `0${d}` : d;
return `${y}-${mm}-${dd}`;
};

const getCalendarStartDate = (year: number, month: number) => {
const firstDayOfMonth = new Date(year, month - 1, 1);
const firstDayOfWeek = firstDayOfMonth.getDay();
const startDate = new Date(firstDayOfMonth);
startDate.setDate(1 - firstDayOfWeek);
return startDate;
};

const generateFullWeeks = (year: number, month: number) => {
const startDate = getCalendarStartDate(year, month);
const days: string[] = [];

for (let i = 0; i < 42; i++) {
const currentDate = new Date(startDate);
currentDate.setDate(startDate.getDate() + i);
days.push(formatDate(currentDate.getFullYear(), currentDate.getMonth() + 1, currentDate.getDate()));
}

return Array.from({ length: 6 }, (_, i) => days.slice(i * 7, (i + 1) * 7));
};

const generatePartialWeeks = (year: number, month: number) => {
const firstDayOfMonth = new Date(year, month - 1, 1);
// Fix: Handle Sunday (0) by converting it to 7, otherwise use the day number
const firstDayOfWeek = firstDayOfMonth.getDay();
console.log({ firstDayOfWeek, firstDayOfMonth });
const daysInMonth = new Date(year, month, 0).getDate();

const previousDays = Array(firstDayOfWeek).fill(null);
const currentDays = Array.from({ length: daysInMonth }, (_, i) => formatDate(year, month, i + 1));
const totalDays = previousDays.length + currentDays.length;
const trailingNulls = Array((7 - (totalDays % 7)) % 7).fill(null);

const allDays = [...previousDays, ...currentDays, ...trailingNulls];
return Array.from({ length: allDays.length / 7 }, (_, i) => allDays.slice(i * 7, (i + 1) * 7));
};

export const generateCalendarDays = ({ month, year, fullWeeks = false }: DaysArrParams) => {
const numericYear = Number.parseInt(year, 10);
const numericMonth = Number.parseInt(month, 10);

return fullWeeks ? generateFullWeeks(numericYear, numericMonth) : generatePartialWeeks(numericYear, numericMonth);
};

/**
* Calculates ISO week number for a date
* @param date - Date in LocalDate format
* @returns ISO week number
*/
export const getISOWeekNumber = (date: LocalDate): number => {
const d = new Date(date);
d.setHours(0, 0, 0, 0);
d.setDate(d.getDate() + 4 - (d.getDay() || 7));
const yearStart = new Date(d.getFullYear(), 0, 1);
return Math.ceil(((d.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);
};
11 changes: 0 additions & 11 deletions src/lib/core/utils/get-week-number.ts

This file was deleted.

Loading

0 comments on commit a8d263d

Please sign in to comment.