1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199import { DateTime, Interval } from 'luxon';
import {
viewOfDays,
viewOfInterval,
dayOf,
weekOf,
yearOf,
intervalDuration,
previousUnitInterval,
nextUnitInterval,
numUnit,
splitByUnit,
today,
isToday,
isFuture,
isPast,
} from '../src';
describe('days()', () => {
it('returns dayOfs[]', () => {
const week = weekOf(DateTime.local());
const weekdays = splitByUnit(week, 'days');
expect(weekdays.length).toBe(numUnit(week, 'days'));
expect(Math.round(intervalDuration(week, 'days'))).toBe(7);
});
});
describe('weeks()', () => {
it('returns an array of week intervals', () => {
const interval = yearOf(DateTime.local());
const yearweeks = splitByUnit(interval, 'weeks');
expect(yearweeks.length).toBe(numUnit(interval, 'weeks'));
expect(Math.round(intervalDuration(interval, 'weeks'))).toBe(52);
});
});
describe('dayOf()', () => {
it('returns 00:00:00 - 23:59:59.999', () => {
const interval = dayOf(DateTime.local());
const { start, end } = interval;
expect(start.hour).toBe(0);
expect(start.minute).toBe(0);
expect(end.hour).toBe(23);
expect(end.minute).toBe(59);
expect(Math.fround(intervalDuration(interval, 'hours'))).toBe(24);
});
});
describe('weekOf()', () => {
it('returns Sunday - Saturday', () => {
const interval = weekOf(DateTime.local());
expect(interval.start.weekday).toBe(7);
expect(interval.end.weekday).toBe(6);
});
});
describe('yearOf()', () => {
it('returns Jan 1 - Dec 31', () => {
const interval = yearOf(DateTime.local());
const { start, end } = interval;
expect(start.month).toBe(1);
expect(start.day).toBe(1);
expect(end.month).toBe(12);
expect(end.day).toBe(31);
expect(start.year).toBe(end.year);
});
});
describe('viewOfInterval()', () => {
it('returns a day interval for day view', () => {
const datetime = DateTime.fromObject({ year: 2019, month: 12, day: 30 });
const interval = viewOfInterval(datetime, 'days');
expect(numUnit(interval, 'days')).toBe(1);
expect(interval.start.day).toBe(30);
expect(interval.end.day).toBe(30);
});
it('returns a week interval for week view', () => {
const datetime = DateTime.fromObject({ year: 2019, month: 12, day: 30 });
const interval = viewOfInterval(datetime, 'weeks');
expect(numUnit(interval, 'days')).toBe(7);
expect(interval.start.month).toBe(12);
expect(interval.start.day).toBe(29);
expect(interval.end.month).toBe(1);
expect(interval.end.day).toBe(4);
});
it('returns a month interval for month view', () => {
const datetime = DateTime.fromObject({ year: 2019, month: 12, day: 30 });
const interval = viewOfInterval(datetime, 'months');
expect(numUnit(interval, 'days')).toBe(31);
expect(interval.start.day).toBe(1);
expect(interval.end.day).toBe(31);
});
it('returns a year interval for year view', () => {
const datetime = DateTime.fromObject({ year: 2019, month: 12, day: 30 });
const interval = viewOfInterval(datetime, 'years');
expect(numUnit(interval, 'days')).toBe(365);
expect(interval.start.month).toBe(1);
expect(interval.start.day).toBe(1);
expect(interval.start.year).toBe(2019);
expect(interval.end.month).toBe(12);
expect(interval.end.day).toBe(31);
expect(interval.end.year).toBe(2019);
});
});
describe('viewOfDays()', () => {
it('returns day intervals for view and datetime', () => {
const datetime = DateTime.fromObject({ year: 2019, month: 12, day: 30 });
const days = viewOfDays(datetime, 'month');
expect(days.length).toBe(31);
expect(days[0].start.day).toBe(1);
expect(days[30].start.day).toBe(31);
});
});
describe('previousUnitInterval()', () => {
it('returns the interval minus 1 unit', () => {
const interval = weekOf(DateTime.local());
const result = previousUnitInterval(interval, { weeks: 1 });
expect(result.start.toISO()).toEqual(
interval.start.minus({ weeks: 1 }).toISO()
);
});
});
describe('nextUnitInterval()', () => {
it('returns the interval pluse 1 unit', () => {
const interval = weekOf(DateTime.local());
const result = nextUnitInterval(interval, { weeks: 1 });
expect(result.start.toISO()).toEqual(
interval.start.plus({ weeks: 1 }).toISO()
);
});
});
describe('intervalDuration()', () => {
it('returns the duration length as unit', () => {
const interval = weekOf(DateTime.local());
const result = intervalDuration(interval, 'weeks');
const days = intervalDuration(interval, 'days');
expect(Math.round(result)).toBe(1);
expect(Math.round(days)).toBe(7);
});
});
describe('presets', () => {
describe('today()', () => {
it('returns the start of today datetime', () => {
const now = DateTime.local();
const test = Interval.fromDateTimes(now.startOf('day'), now.endOf('day'));
const result = today();
expect(test.toISO()).toBe(result.toISO());
});
it('returns utc when passed utc || UTC timezone arg', () => {
const now = DateTime.utc();
const result = today('utc');
expect(now.startOf('day').toISO()).toBe(result.start.toISO());
expect(now.startOf('day').toISO()).toBe(today('UTC').start.toISO());
});
});
});
describe('predicates', () => {
describe('isToday()', () => {
it('returns true if it is today', () => {
const test = DateTime.local();
expect(isToday(test)).toBe(true);
});
it('returns false if it is not today', () => {
const test = DateTime.local().minus({ days: 1 });
expect(isToday(test)).toBe(false);
});
});
describe('isFuture()', () => {
it('returns false if date is not in the future', () => {
const test = DateTime.local();
const test2 = test.minus({ days: 1 });
expect(isFuture(test)).toBe(false);
expect(isFuture(test2)).toBe(false);
});
it('returns true if date is in the future', () => {
const test = DateTime.local().plus({ days: 1 });
expect(isFuture(test)).toBe(true);
});
});
describe('isPast()', () => {
it('returns true if date is in the past', () => {
const test = DateTime.local();
const test2 = test.minus({ days: 1 });
expect(isPast(test)).toBe(true);
expect(isPast(test2)).toBe(true);
});
it('returns false if date is in the future', () => {
const test = DateTime.local().plus({ days: 1 });
expect(isPast(test)).toBe(false);
});
});
});