Skip to content

Commit 970b4f2

Browse files
committed
fix(core): apply plugin architecture
1 parent 8a37fd4 commit 970b4f2

13 files changed

+445
-100
lines changed

src/core/createCalendarInfo.test.ts

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,45 +23,81 @@ describe('createCalendarInfo function', () => {
2323
expect(result.weeksInMonth).toBe(5)
2424
})
2525

26-
it('getTargetDate return Date value', () => {
26+
it('getCurrentWeekIndex return current week (number)', () => {
2727
// Given
28-
const date = new Date(2020, 11, 27)
29-
const weekStartsOn = 0 // start Sunday
3028
// When
31-
const result = createCalendarInfo(date, weekStartsOn).getTargetDate(1, 1)
29+
const info1 = createCalendarInfo(new Date(2020, 11, 27), 0)
30+
const info2 = createCalendarInfo(new Date(2020, 10, 1), 0)
31+
const info3 = createCalendarInfo(new Date(2020, 11, 7), 0)
3232
// Then
33-
expect(result.getMonth()).toBe(11)
34-
expect(result.getDate()).toBe(7)
33+
expect(info1.getCurrentWeekIndex()).toBe(4)
34+
expect(info2.getCurrentWeekIndex()).toBe(0)
35+
expect(info3.getCurrentWeekIndex()).toBe(1)
3536
})
3637

37-
it('getCurrentWeek return current week (number)', () => {
38+
it('getDateCellByIndex return Date value', () => {
3839
// Given
40+
const date = new Date(2020, 11, 27)
41+
const weekStartsOn = 0 // start Sunday
3942
// When
40-
const info1 = createCalendarInfo(new Date(2020, 11, 27), 0)
41-
const info2 = createCalendarInfo(new Date(2020, 10, 1), 0)
42-
const info3 = createCalendarInfo(new Date(2020, 11, 7), 0)
43+
const result = createCalendarInfo(date, weekStartsOn).getDateCellByIndex(
44+
1,
45+
1,
46+
)
4347
// Then
44-
expect(info1.getCurrentWeek()).toBe(4)
45-
expect(info2.getCurrentWeek()).toBe(0)
46-
expect(info3.getCurrentWeek()).toBe(1)
48+
expect(result.value).toStrictEqual(new Date(2020, 11, 7))
4749
})
4850

49-
it('getWeek return target week', () => {
51+
it('getWeekRow return target week', () => {
5052
// Given
5153
const weekIndex = 4
52-
54+
5355
// When
5456
const calendar = createCalendarInfo(new Date(2020, 11, 27), 0)
5557
// Then
5658
const expected = [
57-
new Date(2020, 11, 27),
58-
new Date(2020, 11, 28),
59-
new Date(2020, 11, 29),
60-
new Date(2020, 11, 30),
61-
new Date(2020, 11, 31),
62-
new Date(2021, 0, 1),
63-
new Date(2021, 0, 2),
59+
{ value: new Date(2020, 11, 27) },
60+
{ value: new Date(2020, 11, 28) },
61+
{ value: new Date(2020, 11, 29) },
62+
{ value: new Date(2020, 11, 30) },
63+
{ value: new Date(2020, 11, 31) },
64+
{ value: new Date(2021, 0, 1) },
65+
{ value: new Date(2021, 0, 2) },
6466
]
65-
expect(calendar.getWeek(weekIndex)).toEqual(expected)
67+
expect(calendar.getWeekRow(weekIndex).value).toStrictEqual(expected)
68+
})
69+
70+
it('getMonth return target month', () => {
71+
// Given
72+
const weeks = 2
73+
74+
// When
75+
const result = createCalendarInfo(new Date(2020, 11, 27), 0).getMonth(weeks)
76+
77+
// Then
78+
expect(result.value).toStrictEqual([
79+
{
80+
value: [
81+
{ value: new Date(2020, 10, 29) },
82+
{ value: new Date(2020, 10, 30) },
83+
{ value: new Date(2020, 11, 1) },
84+
{ value: new Date(2020, 11, 2) },
85+
{ value: new Date(2020, 11, 3) },
86+
{ value: new Date(2020, 11, 4) },
87+
{ value: new Date(2020, 11, 5) },
88+
],
89+
},
90+
{
91+
value: [
92+
{ value: new Date(2020, 11, 6) },
93+
{ value: new Date(2020, 11, 7) },
94+
{ value: new Date(2020, 11, 8) },
95+
{ value: new Date(2020, 11, 9) },
96+
{ value: new Date(2020, 11, 10) },
97+
{ value: new Date(2020, 11, 11) },
98+
{ value: new Date(2020, 11, 12) },
99+
],
100+
},
101+
])
66102
})
67103
})

src/core/createCalendarInfo.ts

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,58 @@
1-
import {
2-
getDaysInMonth,
3-
setDay,
4-
startOfMonth,
5-
} from 'date-fns'
1+
import { getDaysInMonth, setDay, startOfMonth } from 'date-fns'
62

7-
import { WeekDayType } from '../models'
3+
import { DateCell, MonthMatrix, WeekDayType, WeekRow } from '../models'
84
import { arrayOf, parseDate } from '../utils'
95

10-
export default function createCalendarInfo(date: Date, weekStartsOn: WeekDayType) {
11-
const { year, month, day } = parseDate(date)
12-
const startWeekdayInMonth = getStartWeekdayInMonth(date, weekStartsOn)
13-
const weeksInMonth = getWeeksInMonth(date, weekStartsOn)
14-
const weekendDays = arrayOf(7).map(index => setDay(date, (index + weekStartsOn) % 7))
6+
export default function createCalendarInfo(
7+
cursorDate: Date,
8+
weekStartsOn: WeekDayType,
9+
) {
10+
const { year, month, day } = parseDate(cursorDate)
11+
const startWeekdayInMonth = getStartWeekdayInMonth(cursorDate, weekStartsOn)
12+
const weeksInMonth = getWeeksInMonth(cursorDate, weekStartsOn)
13+
const weekendDays = arrayOf(7).map((index) => ({
14+
value: setDay(cursorDate, (index + weekStartsOn) % 7),
15+
}))
1516

16-
const getDateByIndex = (weekIndex: number, dayIndex: number) => {
17-
const day = weekIndex * 7 + dayIndex - startWeekdayInMonth + 1
18-
19-
return new Date(year, month, day)
20-
}
21-
2217
const getCurrentWeekIndex = () => {
2318
const control = (day - 1) % 7 > startWeekdayInMonth ? 0 : -1
2419

2520
return Math.ceil(day / 7) + control
2621
}
2722

28-
const getMonth = () => {
29-
return arrayOf(weeksInMonth).map(weekIndex => getWeek(weekIndex))
23+
const getDateCellByIndex = (
24+
weekIndex: number,
25+
dayIndex: number,
26+
): DateCell => {
27+
const day = weekIndex * 7 + dayIndex - startWeekdayInMonth + 1
28+
29+
return { value: new Date(year, month, day) }
30+
}
31+
32+
const getWeekRow = (weekIndex: number = getCurrentWeekIndex()): WeekRow => {
33+
return {
34+
value: arrayOf(7).map((dayIndex) =>
35+
getDateCellByIndex(weekIndex, dayIndex),
36+
),
37+
}
3038
}
3139

32-
const getWeek = (weekIndex: number = getCurrentWeekIndex()) => {
33-
return arrayOf(7).map(dayIndex => getDateByIndex(weekIndex, dayIndex))
40+
const getMonth = (weeks = weeksInMonth): MonthMatrix => {
41+
return { value: arrayOf(weeks).map(getWeekRow) }
3442
}
3543

3644
return {
37-
date,
45+
cursorDate,
3846
year,
3947
month,
4048
day,
4149
weekStartsOn,
4250
startWeekdayInMonth,
4351
weeksInMonth,
4452
weekendDays,
45-
getDateByIndex,
53+
getDateCellByIndex,
4654
getCurrentWeekIndex,
47-
getWeek,
55+
getWeekRow,
4856
getMonth,
4957
}
5058
}
@@ -58,6 +66,6 @@ function getStartWeekdayInMonth(date: Date, weekStartsOn: WeekDayType) {
5866
function getWeeksInMonth(date: Date, weekStartsOn: WeekDayType) {
5967
const { month } = parseDate(date)
6068
const totalDaysOfMonth = getDaysInMonth(month)
61-
69+
6270
return Math.ceil((weekStartsOn + totalDaysOfMonth) / 7)
6371
}

src/models/Calendar.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export interface DateCell extends Record<string, unknown> {
2+
value: Date
3+
}
4+
5+
export interface WeekRow extends Record<string, unknown> {
6+
value: DateCell[]
7+
}
8+
9+
export interface MonthMatrix extends Record<string, unknown> {
10+
value: WeekRow[]
11+
}

src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
export * from './Calendar'
12
export { default as CalendarViewType } from './CalendarViewType'
23
export type { default as WeekDayType } from './WeekDayType'

src/plugins/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default as withCurrentPropsMonth } from './withCurrentPropsMonth'
2+
export { default as withKeysMonth } from './withKeysMonth'
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import withCurrentPropsMonth from './withCurrentPropsMonth'
2+
3+
describe('withCurrentPropsMonth plugin', () => {
4+
it('return value with isCurrent Props', () => {
5+
// Given
6+
const month = {
7+
value: [
8+
{
9+
value: [
10+
{ value: new Date(2020, 11, 13) },
11+
{ value: new Date(2020, 11, 14) },
12+
{ value: new Date(2020, 11, 15) },
13+
{ value: new Date(2020, 11, 16) },
14+
{ value: new Date(2020, 11, 17) },
15+
{ value: new Date(2020, 11, 18) },
16+
{ value: new Date(2020, 11, 19) },
17+
],
18+
},
19+
{
20+
value: [
21+
{ value: new Date(2020, 11, 20) },
22+
{ value: new Date(2020, 11, 21) },
23+
{ value: new Date(2020, 11, 22) },
24+
{ value: new Date(2020, 11, 23) },
25+
{ value: new Date(2020, 11, 24) },
26+
{ value: new Date(2020, 11, 25) },
27+
{ value: new Date(2020, 11, 26) },
28+
],
29+
},
30+
{
31+
value: [
32+
{ value: new Date(2020, 11, 27) },
33+
{ value: new Date(2020, 11, 28) },
34+
{ value: new Date(2020, 11, 29) },
35+
{ value: new Date(2020, 11, 30) },
36+
{ value: new Date(2020, 11, 31) },
37+
{ value: new Date(2020, 12, 1) },
38+
{ value: new Date(2020, 12, 2) },
39+
],
40+
},
41+
],
42+
}
43+
44+
const baseDate = new Date(2020, 11, 13)
45+
const cursorDate = new Date(2020, 11, 17)
46+
// When
47+
const func = withCurrentPropsMonth(baseDate, cursorDate)
48+
const result = func(month)
49+
// Then
50+
const expected = {
51+
value: [
52+
{
53+
value: [
54+
{
55+
value: new Date(2020, 11, 13),
56+
isCurrentDate: true,
57+
isCurrentMonth: true,
58+
},
59+
{
60+
value: new Date(2020, 11, 14),
61+
isCurrentDate: false,
62+
isCurrentMonth: true,
63+
},
64+
{
65+
value: new Date(2020, 11, 15),
66+
isCurrentDate: false,
67+
isCurrentMonth: true,
68+
},
69+
{
70+
value: new Date(2020, 11, 16),
71+
isCurrentDate: false,
72+
isCurrentMonth: true,
73+
},
74+
{
75+
value: new Date(2020, 11, 17),
76+
isCurrentDate: false,
77+
isCurrentMonth: true,
78+
},
79+
{
80+
value: new Date(2020, 11, 18),
81+
isCurrentDate: false,
82+
isCurrentMonth: true,
83+
},
84+
{
85+
value: new Date(2020, 11, 19),
86+
isCurrentDate: false,
87+
isCurrentMonth: true,
88+
},
89+
],
90+
},
91+
{
92+
value: [
93+
{
94+
value: new Date(2020, 11, 20),
95+
isCurrentDate: false,
96+
isCurrentMonth: true,
97+
},
98+
{
99+
value: new Date(2020, 11, 21),
100+
isCurrentDate: false,
101+
isCurrentMonth: true,
102+
},
103+
{
104+
value: new Date(2020, 11, 22),
105+
isCurrentDate: false,
106+
isCurrentMonth: true,
107+
},
108+
{
109+
value: new Date(2020, 11, 23),
110+
isCurrentDate: false,
111+
isCurrentMonth: true,
112+
},
113+
{
114+
value: new Date(2020, 11, 24),
115+
isCurrentDate: false,
116+
isCurrentMonth: true,
117+
},
118+
{
119+
value: new Date(2020, 11, 25),
120+
isCurrentDate: false,
121+
isCurrentMonth: true,
122+
},
123+
{
124+
value: new Date(2020, 11, 26),
125+
isCurrentDate: false,
126+
isCurrentMonth: true,
127+
},
128+
],
129+
},
130+
{
131+
value: [
132+
{
133+
value: new Date(2020, 11, 27),
134+
isCurrentDate: false,
135+
isCurrentMonth: true,
136+
},
137+
{
138+
value: new Date(2020, 11, 28),
139+
isCurrentDate: false,
140+
isCurrentMonth: true,
141+
},
142+
{
143+
value: new Date(2020, 11, 29),
144+
isCurrentDate: false,
145+
isCurrentMonth: true,
146+
},
147+
{
148+
value: new Date(2020, 11, 30),
149+
isCurrentDate: false,
150+
isCurrentMonth: true,
151+
},
152+
{
153+
value: new Date(2020, 11, 31),
154+
isCurrentDate: false,
155+
isCurrentMonth: true,
156+
},
157+
{
158+
value: new Date(2020, 12, 1),
159+
isCurrentDate: false,
160+
isCurrentMonth: false,
161+
},
162+
{
163+
value: new Date(2020, 12, 2),
164+
isCurrentDate: false,
165+
isCurrentMonth: false,
166+
},
167+
],
168+
},
169+
],
170+
}
171+
expect(result).toEqual(expected)
172+
})
173+
})

0 commit comments

Comments
 (0)