iamcal 3.1.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/component.d.ts +2 -1
- package/lib/component.d.ts.map +1 -1
- package/lib/component.js +14 -16
- package/lib/components/CalendarEvent.d.ts +34 -8
- package/lib/components/CalendarEvent.d.ts.map +1 -1
- package/lib/components/CalendarEvent.js +67 -8
- package/lib/date.d.ts +41 -3
- package/lib/date.d.ts.map +1 -1
- package/lib/date.js +75 -7
- package/lib/duration.d.ts +106 -0
- package/lib/duration.d.ts.map +1 -0
- package/lib/duration.js +277 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -1
- package/lib/property/Property.d.ts +24 -2
- package/lib/property/Property.d.ts.map +1 -1
- package/lib/property/Property.js +71 -4
- package/lib/property/validate.js +2 -2
- package/package.json +1 -1
- package/src/component.ts +15 -23
- package/src/components/CalendarEvent.ts +73 -9
- package/src/date.ts +96 -13
- package/src/duration.ts +329 -0
- package/src/index.ts +1 -0
- package/src/property/Property.ts +80 -5
- package/src/property/validate.ts +1 -1
package/src/duration.ts
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CalendarDateOrTime,
|
|
3
|
+
isDateObject,
|
|
4
|
+
isCalendarDateOrTime,
|
|
5
|
+
ONE_DAY_MS,
|
|
6
|
+
ONE_DAY_SECONDS,
|
|
7
|
+
ONE_HOUR_SECONDS,
|
|
8
|
+
ONE_MINUTE_SECONDS,
|
|
9
|
+
ONE_SECOND_MS,
|
|
10
|
+
ONE_WEEK_SECONDS,
|
|
11
|
+
} from './date'
|
|
12
|
+
import { validateDuration } from './property'
|
|
13
|
+
|
|
14
|
+
export type DurationUnit = 'W' | 'D' | 'H' | 'M' | 'S'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Represents a DURATION value as defined by RFC5545.
|
|
18
|
+
*/
|
|
19
|
+
export class CalendarDuration {
|
|
20
|
+
weeks?: number
|
|
21
|
+
days?: number
|
|
22
|
+
hours?: number
|
|
23
|
+
minutes?: number
|
|
24
|
+
seconds?: number
|
|
25
|
+
|
|
26
|
+
constructor(duration: string | CalendarDuration) {
|
|
27
|
+
if (typeof duration === 'string') {
|
|
28
|
+
validateDuration(duration)
|
|
29
|
+
|
|
30
|
+
const negativeMultiplier = duration.startsWith('-') ? -1 : 1
|
|
31
|
+
const findParts = /(\d+)([WDHMS])/g
|
|
32
|
+
const parts = duration.matchAll(findParts)
|
|
33
|
+
for (const part of parts) {
|
|
34
|
+
const value = parseInt(part[1], 10)
|
|
35
|
+
const name = part[2] as DurationUnit
|
|
36
|
+
switch (name) {
|
|
37
|
+
case 'W':
|
|
38
|
+
this.weeks = value * negativeMultiplier
|
|
39
|
+
break
|
|
40
|
+
case 'D':
|
|
41
|
+
this.days = value * negativeMultiplier
|
|
42
|
+
break
|
|
43
|
+
case 'H':
|
|
44
|
+
this.hours = value * negativeMultiplier
|
|
45
|
+
break
|
|
46
|
+
case 'M':
|
|
47
|
+
this.minutes = value * negativeMultiplier
|
|
48
|
+
break
|
|
49
|
+
case 'S':
|
|
50
|
+
this.seconds = value * negativeMultiplier
|
|
51
|
+
break
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
this.weeks = duration.weeks
|
|
56
|
+
this.days = duration.days
|
|
57
|
+
this.hours = duration.hours
|
|
58
|
+
this.minutes = duration.minutes
|
|
59
|
+
this.seconds = duration.seconds
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Get the length of this duration object in seconds.
|
|
65
|
+
* @returns The total number of seconds represented by this duration.
|
|
66
|
+
*/
|
|
67
|
+
inSeconds(): number {
|
|
68
|
+
return (
|
|
69
|
+
(this.weeks ?? 0) * ONE_WEEK_SECONDS +
|
|
70
|
+
(this.days ?? 0) * ONE_DAY_SECONDS +
|
|
71
|
+
(this.hours ?? 0) * ONE_HOUR_SECONDS +
|
|
72
|
+
(this.minutes ?? 0) * ONE_MINUTE_SECONDS +
|
|
73
|
+
(this.seconds ?? 0)
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Get the length of this duration object in milliseconds.
|
|
79
|
+
*
|
|
80
|
+
* Note that this will always return whole seconds, as durations do not
|
|
81
|
+
* support milliseconds.
|
|
82
|
+
* @returns The total number of milliseconds represented by this duration.
|
|
83
|
+
*/
|
|
84
|
+
inMilliseconds(): number {
|
|
85
|
+
return this.inSeconds() * ONE_SECOND_MS
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Get the duration string that represents this duration.
|
|
90
|
+
* @returns A duration string in the format `P[n]W` for weeks, or `P[n]DT[n]H[n]M[n]S` for days, hours, minutes and seconds. Prefixed with a `-` if negative.
|
|
91
|
+
*/
|
|
92
|
+
getValue(): string {
|
|
93
|
+
return formatDurationString(
|
|
94
|
+
this.weeks,
|
|
95
|
+
this.days,
|
|
96
|
+
this.hours,
|
|
97
|
+
this.minutes,
|
|
98
|
+
this.seconds
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Get the floor the duration to the nearest of a given unit.
|
|
104
|
+
* @param unit The unit to floor to.
|
|
105
|
+
* @returns The duration with units smaller than `unit` removed.
|
|
106
|
+
* @example
|
|
107
|
+
* const duration = new CalendarDuration("1W2D5H30M")
|
|
108
|
+
* const days = duration.floor("D") // Floor to days
|
|
109
|
+
* console.log(days.getValue()) // "1W2D"
|
|
110
|
+
*/
|
|
111
|
+
floor(unit: DurationUnit): CalendarDuration {
|
|
112
|
+
const result = new CalendarDuration(this)
|
|
113
|
+
/* eslint-disable no-fallthrough */
|
|
114
|
+
switch (unit) {
|
|
115
|
+
case 'W':
|
|
116
|
+
result.days = undefined
|
|
117
|
+
case 'D':
|
|
118
|
+
result.hours = undefined
|
|
119
|
+
case 'H':
|
|
120
|
+
result.minutes = undefined
|
|
121
|
+
case 'M':
|
|
122
|
+
result.seconds = undefined
|
|
123
|
+
}
|
|
124
|
+
/* eslint-enable no-fallthrough */
|
|
125
|
+
return result
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Create a duration from a number of seconds.
|
|
130
|
+
* @param seconds How many seconds the duration should represent.
|
|
131
|
+
* @returns A {@link CalendarDuration} representing the specified number of seconds.
|
|
132
|
+
* @throws {Error} If seconds is NaN.
|
|
133
|
+
*/
|
|
134
|
+
static fromSeconds(seconds: number): CalendarDuration {
|
|
135
|
+
return new CalendarDuration(secondsToDurationString(seconds))
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Create a duration from a number of days.
|
|
140
|
+
* @param days How many days the duration should represent.
|
|
141
|
+
* @returns A {@link CalendarDuration} representing the specified number of days.
|
|
142
|
+
* @throws {Error} If days is NaN.
|
|
143
|
+
*/
|
|
144
|
+
static fromDays(days: number): CalendarDuration {
|
|
145
|
+
return new CalendarDuration(daysToDurationString(days))
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Create a duration from a number of weeks.
|
|
150
|
+
* @param weeks How many weeks the duration should represent.
|
|
151
|
+
* @returns A {@link CalendarDuration} representing the specified number of weeks.
|
|
152
|
+
* @throws {Error} If weeks is NaN.
|
|
153
|
+
*/
|
|
154
|
+
static fromWeeks(weeks: number): CalendarDuration {
|
|
155
|
+
return new CalendarDuration(weeksToDurationString(weeks))
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Creates a duration from the difference between two dates.
|
|
160
|
+
* @param start The start date or time.
|
|
161
|
+
* @param end The end date or time.
|
|
162
|
+
* @returns A {@link CalendarDuration} representing the difference between the two dates.
|
|
163
|
+
* @throws {Error} If the start date and end date have different types.
|
|
164
|
+
*/
|
|
165
|
+
static fromDifference(
|
|
166
|
+
start: Date | CalendarDateOrTime,
|
|
167
|
+
end: Date | CalendarDateOrTime
|
|
168
|
+
): CalendarDuration {
|
|
169
|
+
if (isDateObject(start)) {
|
|
170
|
+
// Date
|
|
171
|
+
if (isDateObject(end)) {
|
|
172
|
+
const differenceSeconds =
|
|
173
|
+
(end.getTime() - start.getTime()) / ONE_SECOND_MS
|
|
174
|
+
return CalendarDuration.fromSeconds(differenceSeconds)
|
|
175
|
+
}
|
|
176
|
+
} else if (start.isFullDay()) {
|
|
177
|
+
// CalendarDate
|
|
178
|
+
if (isCalendarDateOrTime(end) && end.isFullDay()) {
|
|
179
|
+
const startMs = start.getDate().getTime()
|
|
180
|
+
const endMs = end.getDate().getTime()
|
|
181
|
+
const differenceDays = (endMs - startMs) / ONE_DAY_MS
|
|
182
|
+
return CalendarDuration.fromDays(differenceDays)
|
|
183
|
+
}
|
|
184
|
+
} else {
|
|
185
|
+
// CalendarDateTime
|
|
186
|
+
if (isCalendarDateOrTime(end) && !end.isFullDay()) {
|
|
187
|
+
const startMs = start.getDate().getTime()
|
|
188
|
+
const endMs = end.getDate().getTime()
|
|
189
|
+
const differenceSeconds = (endMs - startMs) / ONE_SECOND_MS
|
|
190
|
+
return CalendarDuration.fromSeconds(differenceSeconds)
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
throw new Error('Start and end dates must be of the same type')
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Convert time units to a duration string.
|
|
200
|
+
*
|
|
201
|
+
* A duration is considered negative if any unit is less than 0.
|
|
202
|
+
* @param weeks How many weeks the duration should represent.
|
|
203
|
+
* @param days The days part of the duration.
|
|
204
|
+
* @param hours The hours part of the duration.
|
|
205
|
+
* @param minutes The minutes part of the duration.
|
|
206
|
+
* @param seconds The seconds part of the duration.
|
|
207
|
+
* @returns A string representing the duration in the format `P[n]W` or `P[n]DT[n]H[n]M[n]S` where values may be omitted if 0, prefixed with `-` if negative.
|
|
208
|
+
* @throws {Error} If weeks are combined with other values.
|
|
209
|
+
* @throws {Error} If any unit is NaN.
|
|
210
|
+
*/
|
|
211
|
+
export function formatDurationString(
|
|
212
|
+
weeks: number | undefined,
|
|
213
|
+
days: number | undefined,
|
|
214
|
+
hours: number | undefined,
|
|
215
|
+
minutes: number | undefined,
|
|
216
|
+
seconds: number | undefined,
|
|
217
|
+
): string {
|
|
218
|
+
let prefix = ''
|
|
219
|
+
let durationString = prefix + 'P'
|
|
220
|
+
let hasTime = false
|
|
221
|
+
let isEmpty = true
|
|
222
|
+
|
|
223
|
+
const appendUnit = (time: number | undefined, unit: string) => {
|
|
224
|
+
if (time === undefined) return
|
|
225
|
+
if (isNaN(time)) {
|
|
226
|
+
throw new Error(`${unit} must not be NaN`)
|
|
227
|
+
}
|
|
228
|
+
if (time < 0) prefix = '-'
|
|
229
|
+
const absTime = Math.abs(Math.floor(time))
|
|
230
|
+
durationString += absTime + unit.charAt(0)
|
|
231
|
+
isEmpty = false
|
|
232
|
+
}
|
|
233
|
+
const appendTimeUnit = (time: number | undefined, unit: string) => {
|
|
234
|
+
if (time === undefined) return
|
|
235
|
+
if (!hasTime) {
|
|
236
|
+
durationString += 'T'
|
|
237
|
+
hasTime = true
|
|
238
|
+
}
|
|
239
|
+
appendUnit(time, unit)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Add units
|
|
243
|
+
if (weeks !== undefined) {
|
|
244
|
+
if (days !== undefined ||
|
|
245
|
+
hours !== undefined ||
|
|
246
|
+
minutes !== undefined ||
|
|
247
|
+
seconds !== undefined) {
|
|
248
|
+
throw new Error('Cannot combine weeks with other units in duration string')
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
appendUnit(weeks, 'Weeks')
|
|
252
|
+
}
|
|
253
|
+
appendUnit(days, 'Days')
|
|
254
|
+
appendTimeUnit(hours, 'Hours')
|
|
255
|
+
if (minutes !== undefined) {
|
|
256
|
+
appendTimeUnit(minutes, 'Minutes')
|
|
257
|
+
} else if (hours !== undefined && seconds !== undefined) {
|
|
258
|
+
appendTimeUnit(0, 'Minutes')
|
|
259
|
+
}
|
|
260
|
+
appendTimeUnit(seconds, 'Seconds')
|
|
261
|
+
|
|
262
|
+
if (isEmpty) {
|
|
263
|
+
throw new Error('Duration string must not be empty')
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return prefix + durationString
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Convert a number of seconds to a duration string.
|
|
271
|
+
* @param seconds How many seconds the duration should represent.
|
|
272
|
+
* @returns A string representing the duration in the format `PT[n]H[n]M[n]S` where values may be omitted if 0, prefixed with `-` if negative.
|
|
273
|
+
* @throws {Error} If seconds is NaN.
|
|
274
|
+
*/
|
|
275
|
+
export function secondsToDurationString(seconds: number): string {
|
|
276
|
+
if (isNaN(seconds)) throw new Error('Seconds must not be NaN')
|
|
277
|
+
seconds = Math.floor(seconds)
|
|
278
|
+
|
|
279
|
+
const absSeconds = Math.abs(seconds)
|
|
280
|
+
const minutes = Math.floor(absSeconds / 60)
|
|
281
|
+
const hours = Math.floor(minutes / 60)
|
|
282
|
+
let secondsValue: number | undefined = seconds % 60
|
|
283
|
+
let minutesValue: number | undefined = minutes % 60
|
|
284
|
+
let hoursValue: number | undefined = hours
|
|
285
|
+
|
|
286
|
+
if (hoursValue === 0) hoursValue = undefined
|
|
287
|
+
if (minutesValue === 0) minutesValue = undefined
|
|
288
|
+
if (secondsValue === 0 && !(minutesValue === undefined && hoursValue === undefined)) secondsValue = undefined
|
|
289
|
+
|
|
290
|
+
return formatDurationString(
|
|
291
|
+
undefined,
|
|
292
|
+
undefined,
|
|
293
|
+
hoursValue,
|
|
294
|
+
minutesValue,
|
|
295
|
+
secondsValue,
|
|
296
|
+
)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Convert a number of days to a duration string.
|
|
301
|
+
* @param days How many days the duration should represent.
|
|
302
|
+
* @returns A string representing the duration in the format `P[n]D`, prefixed with `-` if negative.
|
|
303
|
+
* @throws {Error} If days is NaN.
|
|
304
|
+
*/
|
|
305
|
+
export function daysToDurationString(days: number): string {
|
|
306
|
+
return formatDurationString(
|
|
307
|
+
undefined,
|
|
308
|
+
days,
|
|
309
|
+
undefined,
|
|
310
|
+
undefined,
|
|
311
|
+
undefined
|
|
312
|
+
)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Convert a number of weeks to a duration string.
|
|
317
|
+
* @param weeks How many weeks the duration should represent.
|
|
318
|
+
* @returns A string representing the duration in the format `P[n]W`, prefixed with `-` if negative.
|
|
319
|
+
* @throws {Error} If weeks is NaN.
|
|
320
|
+
*/
|
|
321
|
+
export function weeksToDurationString(weeks: number): string {
|
|
322
|
+
return formatDurationString(
|
|
323
|
+
weeks,
|
|
324
|
+
undefined,
|
|
325
|
+
undefined,
|
|
326
|
+
undefined,
|
|
327
|
+
undefined
|
|
328
|
+
)
|
|
329
|
+
}
|
package/src/index.ts
CHANGED
package/src/property/Property.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { CalendarDateOrTime } from '
|
|
1
|
+
import { CalendarDateOrTime, CalendarDate, CalendarDateTime, isCalendarDateOrTime } from '../date'
|
|
2
|
+
import { CalendarDuration } from '../duration'
|
|
2
3
|
import {
|
|
3
4
|
escapePropertyParameterValue,
|
|
4
5
|
escapeTextPropertyValue,
|
|
@@ -16,7 +17,7 @@ import {
|
|
|
16
17
|
RsvpExpectation,
|
|
17
18
|
} from './parameter'
|
|
18
19
|
import { validateCalendarUserAddress, validateContentType } from './validate'
|
|
19
|
-
import { AllowedValueType, getDefaultValueType } from './valueType'
|
|
20
|
+
import { AllowedValueType, KnownValueType, getDefaultValueType } from './valueType'
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
23
|
* Represents a property of a calendar component as described by RFC 5545 in
|
|
@@ -55,13 +56,72 @@ export class Property {
|
|
|
55
56
|
}
|
|
56
57
|
|
|
57
58
|
static fromDate(name: string, value: CalendarDateOrTime): Property {
|
|
59
|
+
const valueType = value.isFullDay() ? 'DATE' : 'DATE-TIME'
|
|
60
|
+
let properties: { [k: string]: string | string[] } | undefined = undefined
|
|
61
|
+
if (getDefaultValueType(name) !== valueType) {
|
|
62
|
+
properties = { VALUE: valueType }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return new Property(
|
|
66
|
+
name,
|
|
67
|
+
value.getValue(),
|
|
68
|
+
properties,
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static fromDuration(name: string, value: CalendarDuration): Property {
|
|
58
73
|
return new Property(
|
|
59
74
|
name,
|
|
60
75
|
value.getValue(),
|
|
61
|
-
|
|
76
|
+
{ VALUE: 'CALENDARDATE' },
|
|
62
77
|
)
|
|
63
78
|
}
|
|
64
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Get the value of this property, converted into the appropriate class if possible.
|
|
82
|
+
* @returns The value as a string or appropriate class.
|
|
83
|
+
*/
|
|
84
|
+
getValue(): string | CalendarDateOrTime | CalendarDuration {
|
|
85
|
+
const valueType = this.getValueType()
|
|
86
|
+
switch (valueType) {
|
|
87
|
+
case 'DATE':
|
|
88
|
+
return new CalendarDate(this.value)
|
|
89
|
+
case 'DATE-TIME':
|
|
90
|
+
return new CalendarDateTime(this.value)
|
|
91
|
+
case 'DURATION':
|
|
92
|
+
return new CalendarDuration(this.value)
|
|
93
|
+
default:
|
|
94
|
+
return this.value
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Set the value of this property and change the value type if a class is used.
|
|
100
|
+
* @param value The new value as a string or class.
|
|
101
|
+
*/
|
|
102
|
+
setValue(value: string | CalendarDateOrTime | CalendarDuration) {
|
|
103
|
+
const valueTypeBefore = this.getExplicitValueType()
|
|
104
|
+
if (typeof value === 'string') {
|
|
105
|
+
// Set string
|
|
106
|
+
this.value = value
|
|
107
|
+
} else if (isCalendarDateOrTime(value)) {
|
|
108
|
+
// Set date
|
|
109
|
+
this.value = value.getValue()
|
|
110
|
+
this.setValueType(value.isFullDay() ? 'DATE' : 'DATE-TIME')
|
|
111
|
+
} else {
|
|
112
|
+
// Set duration
|
|
113
|
+
this.value = value.getValue()
|
|
114
|
+
this.setValueType('DURATION')
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Remove value type if set to default, unless it was already set
|
|
118
|
+
const valueType = this.getExplicitValueType()
|
|
119
|
+
const defaultValueType = this.getDefaultValueType()
|
|
120
|
+
if (valueType === defaultValueType && valueType !== valueTypeBefore) {
|
|
121
|
+
this.removeValueType()
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
65
125
|
setParameter(name: string, value: string | string[]) {
|
|
66
126
|
this._parameters.set(
|
|
67
127
|
name.toUpperCase(),
|
|
@@ -148,8 +208,23 @@ export class Property {
|
|
|
148
208
|
* @returns The value type of this property.
|
|
149
209
|
*/
|
|
150
210
|
getValueType(): AllowedValueType {
|
|
151
|
-
|
|
152
|
-
|
|
211
|
+
return this.getExplicitValueType() ?? this.getDefaultValueType()
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Get the value type of this property.
|
|
216
|
+
* @returns The value type of this property, or `undefined` if unset.
|
|
217
|
+
*/
|
|
218
|
+
getExplicitValueType(): AllowedValueType | undefined {
|
|
219
|
+
return this.getParameter('VALUE')?.[0].toUpperCase()
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Get the default value type for this property.
|
|
224
|
+
* @returns The default value type based on the name of this property.
|
|
225
|
+
*/
|
|
226
|
+
getDefaultValueType(): KnownValueType {
|
|
227
|
+
return getDefaultValueType(this.name)
|
|
153
228
|
}
|
|
154
229
|
|
|
155
230
|
/**
|
package/src/property/validate.ts
CHANGED