-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
224 lines (204 loc) · 4.76 KB
/
index.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* @overview
* Exports the `PlainDate` class.
*/
/**
* @type {Object}
* This cache object makes direct comparisons possible `==`/`===` and
* can potentially save space by reuse of existing PlainDate objects.
*/
const cache = Object.create(null)
/**
* @typedef {Object} PlainDate
*
* @description
* PlainDate objects represent calendar dates or months.
* They focus on ease of use and are very light in comparison
* with JavaScript's native Date type.
*
* They have a small set of properties, are immutable,
* and every date can only ever exist once.
*
* If you try to create a new PlainDate object with a date
* that has previously been created, you will receive that
* pre-existing PlainDate object.
*
* Additionally, PlainDate objects can be intuitively compared.
*
* PlainDate objects can be constructed with one to three parameters.
* Behaviour for unexpected inputs is not defined.
* In order to stay light-weight, there is no intelligent type checking.
*
* @property {number} year
* @property {number} month
* @property {number} date
* @property {number} timestamp
*
* @example
* new PlainDate('2019-04-01') === new PlainDate('2019-04-01') // => true
*
* @example
* new PlainDate('2019-04-01') < new PlainDate('2019-04-01') // => false
*
* @example
* new PlainDate('2019-04-01') < new PlainDate('2019-04-11') // => true
*
* @example
* new PlainDate('2019-04-01') > new PlainDate('2019-04-11') // => false
*
*/
/**
* @constructor
*
* PlainDate/3_parameters
*
* @param {number|string} year
* @param {number|string} month
* @param {number|string} date
*
* @example
* new PlainDate(2019, 4, 1)
*
* @example
* new PlainDate('2019', '04', '01')
*/
/**
* @constructor
*
* PlainDate/2_parameters
*
* @param {number|string} year
* @param {number|string} month
*
* @example
* new PlainDate(2019, 4)
*
* @example
* new PlainDate('2019', '4')
*/
/**
* @constructor
*
* PlainDate/1_parameter
*
* @param {string|Date|PlainDate|{year: number, month: number, date: ?number}} fullDate
*
* @example
* new PlainDate('2019-04-01')
*
* @example
* new PlainDate('2019-04')
*
* @example
* new PlainDate(new Date(2019,3,1))
*
* @example
* new PlainDate({
* year: 2019,
* month: 4,
* })
*/
module.exports = class PlainDate {
constructor (yearOrDate, month, date) {
if (yearOrDate instanceof PlainDate) {
return yearOrDate
}
if (!yearOrDate) {
// eslint-disable-next-line no-param-reassign
yearOrDate = new Date()
}
if (yearOrDate instanceof Date) {
const dateObject = yearOrDate
this.year = dateObject.getFullYear()
this.month = dateObject.getMonth() + 1
this.date = dateObject.getDate()
} else if (typeof yearOrDate === 'object') {
/** @type {PlainDate} */
const dateObject = yearOrDate
if (cache[dateObject.timestamp]) {
return cache[dateObject.timestamp]
}
this.year = dateObject.year
this.month = dateObject.month
this.date = dateObject.date
} else if (typeof yearOrDate === 'string' && yearOrDate.length > 4) {
const [foundYear, foundMonth, foundDate] = yearOrDate.split('-')
this.year = parseInt(foundYear, 10)
this.month = parseInt(foundMonth, 10)
if (foundDate) {
this.date = parseInt(foundDate, 10)
}
} else {
this.year = parseInt(yearOrDate, 10)
this.month = parseInt(month, 10)
if (date) {
this.date = parseInt(date, 10)
}
}
this.timestamp =
(this.year * 10000) +
(this.month * 100) +
(this.date || 0)
if (cache[this.timestamp]) {
return cache[this.timestamp]
}
Object.freeze(this)
cache[this.timestamp] = this
}
/**
* Creates a new equivalent `Date` object.
* Multiple calls yield multiple distinct objects.
*
* @returns {Date}
*/
getNativeDate () {
return new Date(this.year, this.month - 1, this.date || 1)
}
/**
* @deprecated Use `getNativeDate` instead
* @returns {Date}
*/
getComplexDate () {
return this.getNativeDate()
}
/**
* Replaces each token in the given string by
* the appropriate string representation of the related data:
* + YYYY: full year-number.
* + YY: last two digits of year.
* + MM: zero-padded two-digit month.
* + DD: zero-padded two-digit date.
*
* Defaults to the ISO format.
*
* @param {string} [format='YYYY-MM[-DD]']
*
* @returns {string}
*/
toString (format = this.date ? 'YYYY-MM-DD' : 'YYYY-MM') {
return format
.replace('YYYY', this.year)
.replace('YY', padded(this.year))
.replace('MM', padded(this.month))
.replace('DD', padded(this.date))
}
/**
* Enables fast comparison via `<` and `>`.
*
* @returns {number}
*/
valueOf () {
return this.timestamp
}
}
// HELPERS
/**
* Zero-pads a number to 2 digits.
*
* @param {number} [number=0]
*
* @returns {string}
*/
function padded (number = 0) {
return `0${number}`.slice(-2)
}