Add date-fns for better date calculations

- Add date-fns package to project
- Update dependencies in pnpm-lock.yaml
- Replace manual date calculations with date-fns functions
- Add functions to Week class for getting the next/previous week and the nth day of the week

[pnpm-lock.yaml]
- Add `date-fns` dependency to `pnpm-lock.yaml`
- Update `@codemirror/state`, `@codemirror/view`, `@tauri-apps/api`, `idb-keyval`, `mm-jsr`, and `seti-icons` dependencies in `pnpm-lock.yaml`
- Add `date-fns` package to `pnpm-lock.yaml`
[src/lib/week.ts]
- Replace manual date calculations with `date-fns` functions
- Make `Week.from` use `weekStartsOn` to set the start of the week
- Add `Week.next`, `Week.previous` and `Week.nThDay` functions
[package.json]
- Add date-fns package
This commit is contained in:
Kiril Videlov 2023-02-20 12:34:07 +01:00
parent 624796ba97
commit f92a2370bd
3 changed files with 14 additions and 19 deletions

View File

@ -17,6 +17,7 @@
"@codemirror/state": "^6.2.0",
"@codemirror/view": "^6.7.3",
"@tauri-apps/api": "^1.2.0",
"date-fns": "^2.29.3",
"idb-keyval": "^6.2.0",
"mm-jsr": "^3.0.2",
"seti-icons": "^0.0.4",

View File

@ -11,6 +11,7 @@ specifiers:
'@tauri-apps/cli': ^1.2.2
'@types/diff': ^5.0.2
autoprefixer: ^10.4.7
date-fns: ^2.29.3
eslint: ^8.34.0
eslint-config-prettier: ^8.6.0
eslint-plugin-prettier: ^4.2.1
@ -41,6 +42,7 @@ dependencies:
'@codemirror/state': 6.2.0
'@codemirror/view': 6.7.3
'@tauri-apps/api': 1.2.0
date-fns: 2.29.3
idb-keyval: 6.2.0
mm-jsr: 3.0.2
seti-icons: 0.0.4
@ -952,6 +954,11 @@ packages:
css-tree: 1.1.3
dev: false
/date-fns/2.29.3:
resolution: {integrity: sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==}
engines: {node: '>=0.11'}
dev: false
/debug/4.3.4:
resolution: {integrity: sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==}
engines: {node: '>=6.0'}

View File

@ -1,33 +1,20 @@
import { startOfWeek, endOfWeek, addWeeks, subWeeks, addDays } from 'date-fns';
export type Week = {
start: Date;
end: Date;
};
export namespace Week {
export const from = (date: Date): Week => {
const start = new Date(date);
start.setHours(0, 0, 0, 0);
start.setDate(start.getDate() - start.getDay() + 1); // Start on Monday
const end = new Date(start);
end.setDate(end.getDate() + 7);
return { start, end };
return { start: startOfWeek(date, { weekStartsOn: 1 }), end: endOfWeek(date) };
};
export const next = (week: Week): Week => {
const start = new Date(week.start);
start.setDate(start.getDate() + 7);
const end = new Date(week.end);
end.setDate(end.getDate() + 7);
return { start, end };
return { start: addWeeks(week.start, 1), end: addWeeks(week.end, 1) };
};
export const previous = (week: Week): Week => {
const start = new Date(week.start);
start.setDate(start.getDate() - 7);
const end = new Date(week.end);
end.setDate(end.getDate() - 7);
return { start, end };
return { start: subWeeks(week.start, 1), end: subWeeks(week.end, 1) };
};
export const nThDay = (week: Week, n: number): Date => {
const date = new Date(week.start);
date.setDate(date.getDate() + n);
return date;
return addDays(week.start, n)
};
}