Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: calendar core #10

Open
wants to merge 128 commits into
base: v0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
169e06d
refactor: update isValidDate return type for improved type safety
bart-krakowski May 29, 2024
75cb7b3
refactor: update isValidDate return type for improved type safety
bart-krakowski May 29, 2024
5dca8d5
refactor: update isValidDate return type for improved type safety
bart-krakowski May 30, 2024
e9e4be9
revert: isValidDate util
bart-krakowski May 30, 2024
d64d04a
feat: useCalendar hook
bart-krakowski Jun 1, 2024
28b4304
test: useCalendar tests
bart-krakowski Jun 2, 2024
28f491c
docs: useCalendar hook
bart-krakowski Jun 2, 2024
468dc26
docs: useCalendar hook
bart-krakowski Jun 2, 2024
0b9bdeb
test: useCalendar tests
bart-krakowski Jun 2, 2024
5b6533e
test: useCalendar tests
bart-krakowski Jun 2, 2024
37effd4
test: overlaping events
bart-krakowski Jun 2, 2024
096f95d
feat(react-time/useCalendar): getCurrentTimeMarkerProps
bart-krakowski Jun 3, 2024
26b0e1e
feat(react-time/useCalendar): getCurrentTimeMarkerProps
bart-krakowski Jun 3, 2024
3cf89c3
feat(react-time/useCalendar): getCurrentTimeMarkerProps
bart-krakowski Jun 3, 2024
4ae3c45
refactor: reducer
bart-krakowski Jun 3, 2024
4009dae
refactor: reducer
bart-krakowski Jun 3, 2024
fa3716a
refactor(useCalendar): types
bart-krakowski Jun 4, 2024
131984b
refactor(useCalendar): types
bart-krakowski Jun 4, 2024
e542934
refactor: useCalendar reducer
bart-krakowski Jun 4, 2024
a149778
refactor: useCalendar reducer
bart-krakowski Jun 5, 2024
28ac64f
refactor: useCalendar reducer
bart-krakowski Jun 5, 2024
dfa353a
feat: add the isToday field
bart-krakowski Jun 5, 2024
8742087
feat: add the isToday field
bart-krakowski Jun 5, 2024
3d6f74a
feat: add the isToday field
bart-krakowski Jun 5, 2024
d93b29d
docs: useCalendar
bart-krakowski Jun 5, 2024
6432973
feat: useCalendar hook
bart-krakowski Jun 5, 2024
75a445a
cd
bart-krakowski Jun 5, 2024
7536efc
feat: useCalendar hook
bart-krakowski Jun 5, 2024
29d20ae
feat: useCalendar hook
bart-krakowski Jun 5, 2024
e37dbf7
feat: useCalendar hook
bart-krakowski Jun 5, 2024
d76ddc1
feat: useCalendar hook
bart-krakowski Jun 5, 2024
f6b6f29
test: useCalendar
bart-krakowski Jun 5, 2024
d9ed5e7
test: useCalendar
bart-krakowski Jun 5, 2024
7b8662b
test: useCalendar
bart-krakowski Jun 5, 2024
be2e360
feat: custom reducer
bart-krakowski Jun 6, 2024
4ccd56b
docs: tsdocs
bart-krakowski Jun 6, 2024
cc59409
docs: tsdocs
bart-krakowski Jun 6, 2024
8d51b58
docs: tsdocs
bart-krakowski Jun 6, 2024
1e71684
refactor: useCalendar
bart-krakowski Jun 6, 2024
e134065
refactor: useCalendar
bart-krakowski Jun 6, 2024
f3f47ed
refactor: useCalendar
bart-krakowski Jun 6, 2024
1ee814a
refactor: change viewMode type
bart-krakowski Jun 9, 2024
c2419de
refactor: change viewMode type
bart-krakowski Jun 9, 2024
215f722
fix: missing days in view
bart-krakowski Jun 9, 2024
0f05c00
refactor: api
bart-krakowski Jun 11, 2024
def6499
refactor: api
bart-krakowski Jun 11, 2024
110e8be
fix: types
bart-krakowski Jun 11, 2024
3333fcb
fix: types
bart-krakowski Jun 11, 2024
492405b
fix: types
bart-krakowski Jun 11, 2024
7bee6b4
fix: types
bart-krakowski Jun 11, 2024
83f0724
docs: useCalendar
bart-krakowski Jun 11, 2024
e4fb70a
docs: useCalendar
bart-krakowski Jun 11, 2024
e598c20
fix: types
bart-krakowski Jun 11, 2024
a9665a7
refactor: add the fillMissingDays argument
bart-krakowski Jun 11, 2024
d9e9b88
refactor: add the fillMissingDays argument
bart-krakowski Jun 11, 2024
fe13c2c
refactor: add the fillMissingDays argument
bart-krakowski Jun 11, 2024
9f99846
refactor: move utilities to the core package
bart-krakowski Jun 11, 2024
93b519a
fix: getCurrentTimeMarkerProps
bart-krakowski Jun 12, 2024
87544e2
docs: update jsdocs
bart-krakowski Jun 12, 2024
b5ae21e
test: useCalendar
bart-krakowski Jun 12, 2024
f5bd2b7
refactor: getCurrentTimeMarkerProps
bart-krakowski Jun 12, 2024
c03128c
feat: calendar core
bart-krakowski Jun 13, 2024
3c48a5e
feat: calendar core
bart-krakowski Jun 13, 2024
6499475
feat: calendar core
bart-krakowski Jun 13, 2024
2ac6065
docs: calendar core
bart-krakowski Jun 13, 2024
a71f5a8
refactor: useCalendar
bart-krakowski Jun 13, 2024
965de14
test: calendar core
bart-krakowski Jun 17, 2024
301d4b8
refactor: useCalendar
bart-krakowski Jun 17, 2024
1324311
refactor: useCalendar
bart-krakowski Jun 17, 2024
042bb13
refactor: useCalendar
bart-krakowski Jun 17, 2024
82c1c87
refactor(useCalendar): types
bart-krakowski Jun 17, 2024
f61a99d
refactor: move calendar-related files to the core directory
bart-krakowski Jun 20, 2024
1465e85
refactor: Update CalendarCoreOptions interface in calendar.ts
bart-krakowski Jun 20, 2024
3f4ef79
test: imports
bart-krakowski Jun 20, 2024
bff5f34
docs: typo
bart-krakowski Jun 20, 2024
57c547e
refactor: DatePickerCore
bart-krakowski Jun 21, 2024
3dc223d
refactor: weekInfoPolyfill
bart-krakowski Jun 23, 2024
2eabee4
refactor: weekInfoPolyfill
bart-krakowski Jun 23, 2024
564c4e7
refactor: weekInfoPolyfill
bart-krakowski Jun 23, 2024
1a5f193
refactor: weekInfoPolyfill
bart-krakowski Jun 24, 2024
3331c17
refactor: date defaults
bart-krakowski Jun 24, 2024
e1f9c12
refactor: date defaults
bart-krakowski Jun 24, 2024
1dfecfb
refactor: date defaults
bart-krakowski Jun 24, 2024
277f416
refactor: types
bart-krakowski Jun 25, 2024
4131b8e
docs: jsdocs
bart-krakowski Jun 25, 2024
b7f67f0
refactor: types
bart-krakowski Jun 25, 2024
4165b20
refactor: types
bart-krakowski Jun 25, 2024
1b71c7e
test: useCalendar
bart-krakowski Jun 25, 2024
604c8bb
refactor: useIsomorphicLayoutEffect
bart-krakowski Jun 25, 2024
6311195
docs: calendar
bart-krakowski Jun 26, 2024
6627c0c
docs: calendar
bart-krakowski Jun 26, 2024
df925d4
refactor: zonedDateTime
bart-krakowski Jun 28, 2024
c65b760
refactor: rename startDate to start and endDate to end
bart-krakowski Jun 28, 2024
18ad00c
refactor: remove the getCurrentTimeMarkerProps method
bart-krakowski Jun 28, 2024
403df99
refactor: weekInfoPolyfill
bart-krakowski Jun 30, 2024
a805131
refactor: add a resources field to event
bart-krakowski Jun 30, 2024
e17f0cd
refactor: add a resources field to event
bart-krakowski Jun 30, 2024
396d629
refactor: add a resources field to event
bart-krakowski Jun 30, 2024
adc9998
refactor: add startOf and endOf helpers
bart-krakowski Jun 30, 2024
31470b4
refactor: remove all the layout specific code
bart-krakowski Jun 30, 2024
16867e8
refactor: update getDaysNames method to accept weekday parameter
bart-krakowski Jul 1, 2024
be68798
refactor: update useCalendar test to mock current date and time
bart-krakowski Jul 1, 2024
c3ba0c9
refactor: update useCalendar test to mock current date and time
bart-krakowski Jul 1, 2024
5afea3e
test: update useCalendar tests
bart-krakowski Jul 1, 2024
15aced5
test: update useCalendar tests
bart-krakowski Jul 1, 2024
eb8b7b7
test: useCalendar
bart-krakowski Jul 1, 2024
4d71196
refactor: update CalendarCore tests to mock current date and time
bart-krakowski Jul 1, 2024
fbbae15
refactor: Update CalendarCore to use destructuring assignment for dat…
bart-krakowski Jul 2, 2024
ce9b924
feat: add the activeDate property
bart-krakowski Jul 2, 2024
66bfe53
refactor: use compare method
bart-krakowski Jul 2, 2024
06f633d
feat: add workWeek unit
bart-krakowski Jul 2, 2024
0c8c868
feat: add workWeek unit
bart-krakowski Jul 2, 2024
4b7ea40
test: tests reorganisation
bart-krakowski Jul 2, 2024
d544348
chore: add the get-week-info-polyfill
bart-krakowski Jul 3, 2024
d090483
feat: getWeekInfo polyfill
bart-krakowski Jul 3, 2024
178aa34
refactor: getEventProps
bart-krakowski Jul 3, 2024
79fdfdd
refactor: getEventProps
bart-krakowski Jul 3, 2024
20fb06d
refactor: Update generateDateRange to accept string inputs
bart-krakowski Jul 3, 2024
8ae1bcd
chore: upgrade polyfill
bart-krakowski Jul 4, 2024
0e974ac
fix: getEventProps
bart-krakowski Jul 4, 2024
85c6a77
fix: getEventProps
bart-krakowski Jul 4, 2024
72d43d4
refactor: update date inputs to accept string format
bart-krakowski Jul 4, 2024
cbc2186
refactor: convert activeDate, currentPeriod to string in useCalendar
bart-krakowski Jul 4, 2024
1f6048e
refactor: startOf and endOf helpers
bart-krakowski Jul 18, 2024
291cfb1
test: calendar core
bart-krakowski Jul 18, 2024
3aa0488
refactor: update useCalendar to accept string inputs for activeDate a…
bart-krakowski Jul 20, 2024
8850d2a
refactor: Update get-week-info-polyfill dependency to version 1.0.6
bart-krakowski Jul 20, 2024
fe67772
refactor: Update startOf and endOf helpers to accept string inputs
bart-krakowski Jul 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions packages/time/src/calendar/splitMultiDayEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ export const splitMultiDayEvents = <
let currentDay = startDate
while (Temporal.ZonedDateTime.compare(currentDay, endDate) < 0) {
const eventStart =
Temporal.PlainDateTime.compare(currentDay, startDate) === 0
Temporal.ZonedDateTime.compare(currentDay, startDate) === 0
? startDate
: startOf(currentDay, 'day')
: startOf({ date: currentDay, unit: 'day' })
const eventEnd =
Temporal.PlainDateTime.compare(endDate, endOf(currentDay, 'day')) < 0
Temporal.ZonedDateTime.compare(endDate, endOf({ date: currentDay, unit: 'day' })) < 0
? endDate
: endOf(currentDay, 'day')
: endOf({ date: currentDay, unit: 'day' })

events.push({ ...event, start: eventStart, end: eventEnd })
events.push({
...event,
start: eventStart.toString(),
end: eventEnd.toString(),
})

currentDay = startOf(currentDay, 'day').add({ days: 1 })
currentDay = startOf({ date: currentDay, unit: 'day' }).add({ days: 1 })
}

return events
}
}
8 changes: 4 additions & 4 deletions packages/time/src/tests/calendar-core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ describe('CalendarCore', () => {
start:
Temporal.PlainDateTime.from('2024-06-12T11:00').toZonedDateTime(
mockTimeZone,
),
).toString(),
end: Temporal.PlainDateTime.from(
'2024-06-12T13:00',
).toZonedDateTime(mockTimeZone),
).toZonedDateTime(mockTimeZone).toString(),
},
],
})
Expand All @@ -147,10 +147,10 @@ describe('CalendarCore', () => {
start:
Temporal.PlainDateTime.from('2024-06-12T11:00').toZonedDateTime(
mockTimeZone,
),
).toString(),
end: Temporal.PlainDateTime.from(
'2024-06-12T12:00',
).toZonedDateTime(mockTimeZone),
).toZonedDateTime(mockTimeZone).toString(),
},
],
})
Expand Down
74 changes: 61 additions & 13 deletions packages/time/src/tests/endOf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,91 @@ import { endOf } from '../utils'
describe('endOf', () => {
test('should get the end of the given day', () => {
const date = Temporal.ZonedDateTime.from('2023-07-16T12:34:56.789+01:00[Europe/London]')
const result = endOf(date, 'day')
const expected = Temporal.ZonedDateTime.from('2023-07-16T23:59:59.999+01:00[Europe/London]')
const result = endOf({
date,
unit: 'day',
})
const expected = Temporal.ZonedDateTime.from('2023-07-16T23:59:59.999999999+01:00[Europe/London]')
expect(result.equals(expected)).toBe(true)
})

test('should get the end of the given week', () => {
const date = Temporal.ZonedDateTime.from('2023-07-13T12:34:56.789+01:00[Europe/London]')
const result = endOf(date, 'week')
const expected = Temporal.ZonedDateTime.from('2023-07-16T23:59:59.999+01:00[Europe/London]')
const result = endOf({
date,
unit: 'week',
})
const expected = Temporal.ZonedDateTime.from('2023-07-16T23:59:59.999999999+01:00[Europe/London]')
expect(result.equals(expected)).toBe(true)
})

test('should get the end of the given month', () => {
const date = Temporal.ZonedDateTime.from('2023-07-16T12:34:56.789+01:00[Europe/London]')
const result = endOf(date, 'month')
const expected = Temporal.ZonedDateTime.from('2023-07-31T23:59:59.999+01:00[Europe/London]')
const result = endOf({
date,
unit: 'month',
})
const expected = Temporal.ZonedDateTime.from('2023-07-31T23:59:59.999999999+01:00[Europe/London]')
expect(result.equals(expected)).toBe(true)
})

test('should get the end of the given year', () => {
const date = Temporal.ZonedDateTime.from('2023-07-16T12:34:56.789+01:00[Europe/London]')
const result = endOf(date, 'year')
const expected = Temporal.ZonedDateTime.from('2023-12-31T23:59:59.999+00:00[Europe/London]')
const result = endOf({
date,
unit: 'year',
})
const expected = Temporal.ZonedDateTime.from('2023-12-31T23:59:59.999999999+00:00[Europe/London]')
expect(result.equals(expected)).toBe(true)
})

test('should get the end of the given work week', () => {
const date = Temporal.ZonedDateTime.from('2023-07-10T12:34:56.789+01:00[Europe/London]')
const result = endOf(date, 'workWeek')
const expected = Temporal.ZonedDateTime.from('2023-07-14T23:59:59.999+01:00[Europe/London]')
const result = endOf({
date,
unit: 'workWeek',
})
const expected = Temporal.ZonedDateTime.from('2023-07-14T23:59:59.999999999+01:00[Europe/London]')
expect(result.equals(expected)).toBe(true)
})

test('should get the end of the given decade', () => {
const date = Temporal.ZonedDateTime.from('2023-07-16T12:34:56.789+01:00[Europe/London]')
const result = endOf(date, 'decade')
const expected = Temporal.ZonedDateTime.from('2029-12-31T23:59:59.999+00:00[Europe/London]')
const result = endOf({
date,
unit: 'decade',
})
const expected = Temporal.ZonedDateTime.from('2029-12-31T23:59:59.999999999+00:00[Europe/London]')
expect(result.equals(expected)).toBe(true)
})
})

test('should handle daylight saving time changes', () => {
// Test for a date just before the DST change
const dateBeforeDST = Temporal.ZonedDateTime.from('2023-10-28T12:00:00+01:00[Europe/London]')
const resultBeforeDST = endOf({
date: dateBeforeDST,
unit: 'day',
})
const expectedBeforeDST = Temporal.ZonedDateTime.from('2023-10-28T23:59:59.999999999+01:00[Europe/London]')
expect(resultBeforeDST.equals(expectedBeforeDST)).toBe(true)

// Test for a date just after the DST change
const dateAfterDST = Temporal.ZonedDateTime.from('2023-10-29T12:00:00+00:00[Europe/London]')
const resultAfterDST = endOf({
date: dateAfterDST,
unit: 'day',
})
const expectedAfterDST = Temporal.ZonedDateTime.from('2023-10-29T23:59:59.999999999+00:00[Europe/London]')
expect(resultAfterDST.equals(expectedAfterDST)).toBe(true)
})

test('should handle different time zones', () => {
const dateNY = Temporal.ZonedDateTime.from('2023-07-16T12:34:56.789-04:00[America/New_York]')
const resultNY = endOf({
date: dateNY,
unit: 'day',
})
const expectedNY = Temporal.ZonedDateTime.from('2023-07-16T23:59:59.999999999-04:00[America/New_York]')
expect(resultNY.equals(expectedNY)).toBe(true)
})
})
109 changes: 55 additions & 54 deletions packages/time/src/utils/endOf.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,68 @@
import type { Temporal } from '@js-temporal/polyfill'

type ViewUnit = 'month' | 'week' | 'day' | 'workWeek' | 'decade' | 'year'

interface EndOfParams {
date: Temporal.ZonedDateTime
unit: ViewUnit
viewModeValue?: number
firstDayOfWeek?: number
}

/**
* Helper function to get the end of a given temporal unit.
* @param date {Temporal.ZonedDateTime} - The date for which the end of the unit is needed.
* @param unit {Unit} - The unit for which to find the end ('day', 'week', 'month', 'year', 'workWeek', 'decade').
* @param {EndOfParams} params - The parameters for the endOf function.
* @returns {Temporal.ZonedDateTime} The end of the given unit.
*/
export const endOf = (date: Temporal.ZonedDateTime, unit: 'day' | 'week' | 'month' | 'year' | 'workWeek' | 'decade'): Temporal.ZonedDateTime => {
export function endOf({
date,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like the direction here. I started a discussion in the time-maintainers discord around these types of methods...

unit,
viewModeValue = 1,
firstDayOfWeek = 1,
}: EndOfParams): Temporal.ZonedDateTime {
let endDate: Temporal.ZonedDateTime

switch (unit) {
case 'day':
return date.with({
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
});
case 'week': {
const endOfWeek = date.add({ days: 7 - date.dayOfWeek }).with({
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
});
return endOfWeek;
}
case 'month': {
const lastDayOfMonth = date.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 });
return lastDayOfMonth.with({
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
});
}
case 'year': {
const lastDayOfYear = date.with({ month: 12, day: 31 });
return lastDayOfYear.with({
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
});
}
case 'workWeek': {
const endOfWorkWeek = date.add({ days: 5 - date.dayOfWeek }).with({
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
});
return endOfWorkWeek;
}
endDate = date
break
case 'week':
endDate = date.add({ days: 7 - date.dayOfWeek })
break
case 'workWeek':
endDate = date.add({ days: 5 - date.dayOfWeek })
break
case 'month':
endDate = date
.with({ day: 1 })
.add({ months: viewModeValue })
.subtract({ days: 1 })
if (viewModeValue > 1) {
const lastDayOfMonthWeekDay =
(endDate.dayOfWeek - firstDayOfWeek + 7) % 7
endDate = endDate.add({ days: 6 - lastDayOfMonthWeekDay })
}
break
case 'year':
endDate = date.with({ month: 12, day: 31 })
break
case 'decade': {
const lastYearOfDecade = date.with({ year: date.year - (date.year % 10) + 9 });
const lastDayOfDecade = lastYearOfDecade.with({ month: 12, day: 31 });
return lastDayOfDecade.with({
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
});
const lastYearOfDecade = date.with({
year: date.year - (date.year % 10) + 9,
})
endDate = lastYearOfDecade.with({ month: 12, day: 31 })
break
}
default:
throw new Error(`Unsupported unit: ${unit}`);
throw new Error(`Unsupported unit: ${unit}`)
}

return endDate.with({
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
microsecond: 999,
nanosecond: 999,
})
}
Loading