-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
dates-list.js
150 lines (134 loc) · 3.87 KB
/
dates-list.js
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
/**
* Copyright 2017 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {requireExternal} from '../../../src/module';
import {rrulestr} from 'rrule';
/** @enum {string} */
const DateType = {
INVALID: 'invalid',
RRULE: 'rrule',
DATE: 'date',
};
/**
* A class which wraps a list of moment or RRULE dates.
*/
export class DatesList {
/**
* @param {!Array<string>} dates
*/
constructor(dates) {
/** @private @const */
this.ReactDates_ = /** @type {!JsonObject} */ (requireExternal(
'react-dates'
));
/** @private @const */
this.moment_ = requireExternal('moment');
/** @private @const */
this.rrulestrs_ = dates
.filter(d => this.getDateType_(d) === DateType.RRULE)
.map(d => tryParseRrulestr(d));
/** @private @const */
this.dates_ = dates
.filter(d => this.getDateType_(d) == DateType.DATE)
.map(d => this.moment_(d))
.sort((a, b) => a.toDate() - b.toDate());
}
/**
* Determines if the given date is contained within the RRULEs or moment
* dates contained in the date list.
* @param {!moment|string} date
* @return {boolean}
*/
contains(date) {
const m = this.moment_(date);
return this.matchesDate_(m) || this.matchesRrule_(m);
}
/**
* Gets the first date in the date list after the given date.
* @param {!moment|string} momentOrString
* @return {!moment}
*/
firstDateAfter(momentOrString) {
const m = this.moment_(momentOrString);
const date = m.toDate();
const firstDatesAfter = [];
for (let i = 0; i < this.dates_.length; i++) {
if (this.dates_[i].toDate() >= date) {
firstDatesAfter.push(this.dates_[i]);
break;
}
}
const rruleDates = this.rrulestrs_
.map(rrule => rrule.after(date))
.filter(Boolean);
firstDatesAfter.concat(rruleDates);
return firstDatesAfter.sort((a, b) => a.toDate() - b.toDate())[0];
}
/**
* Determines if any internal moment object matches the given date.
* @param {!moment} date
* @return {boolean}
* @private
*/
matchesDate_(date) {
return this.dates_.some(d => this.ReactDates_['isSameDay'](d, date));
}
/**
* Determines if any internal RRULE object matches the given date.
* @param {!moment} date
* @return {boolean}
* @private
*/
matchesRrule_(date) {
const nextDate = date
.clone()
.startOf('day')
.add(1, 'day')
.toDate();
return this.rrulestrs_.some(rrule => {
const rruleDay = this.moment_(rrule.before(nextDate));
return this.ReactDates_['isSameDay'](rruleDay, date);
});
}
/**
* Distinguish between RRULE dates and moment dates.
* @param {!moment|string} date
* @return {!DateType}
* @private
*/
getDateType_(date) {
if (this.moment_(date).isValid()) {
return DateType.DATE;
}
const dateStr = /** @type {string} */ (date);
if (tryParseRrulestr(dateStr)) {
return DateType.RRULE;
}
return DateType.INVALID;
}
}
/**
* Tries to parse a string into an RRULE object.
* @param {string} str A string which represents a repeating date RRULE spec.
* @return {?JsonObject}
* @suppress {missingProperties} // Remove after https://github.com/google/closure-compiler/issues/3041 is fixed
*/
function tryParseRrulestr(str) {
try {
return rrulestr(str);
} catch (e) {
return null;
}
}