/
Schedule.ts
1730 lines (1511 loc) · 52.1 KB
/
Schedule.ts
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Functions as fn } from './Functions';
import { FrequencyValue, FrequencyCheck, FrequencyValueEvery, FrequencyValueOneOf } from './Frequency';
import { Day, DayInput, DurationInput, DayProperty } from './Day';
import { Identifier, IdentifierInput } from './Identifier';
import { DaySpan } from './DaySpan';
import { Constants } from './Constants';
import { Parse } from './Parse';
import { Time, TimeInput } from './Time';
import { Suffix } from './Suffix';
import { ScheduleModifier, ScheduleModifierSpan } from './ScheduleModifier';
import { Units } from './Units';
import { Iterator, IteratorAction } from './Iterator';
// @ts-ignore
import * as moment from 'moment';
/**
* A tuple which identifies an event on the schedule. The tuple contains the
* total span of the event occurrence, the day of the event (could be the start
* day, end day, or any days in between for multi-day events) as well as the
* identifier for the event.
*/
export type ScheduleEventTuple = [DaySpan, Day, IdentifierInput];
/**
* Input given by a user which describes an event schedule.
*
* @typeparam M The type of metadata stored in the schedule.
*/
export interface ScheduleInput<M>
{
/**
* @see [[Schedule.start]]
*/
start?: DayInput;
/**
* @see [[Schedule.end]]
*/
end?: DayInput;
/**
* A shortcut to setting the [[Schedule.start]], [[Schedule.end]],
* [[Schedule.year]], [[Schedule.month]], and [[Schedule.dayOfMonth]].
*/
on?: DayInput;
/**
* @see [[Schedule.times]]
*/
times?: TimeInput[];
/**
* @see [[Schedule.duration]]
*/
duration?: number;
/**
* @see [[Schedule.durationUnit]]
*/
durationUnit?: DurationInput;
/**
* An array of days or identifiers which should be excluded from the schedule.
*
* @see [[Schedule.exclude]]
*/
exclude?: (Day | IdentifierInput)[];
/**
* An array of days or identifiers which should be included in the schedule.
*
* @see [[Schedule.include]]
*/
include?: (Day | IdentifierInput)[];
/**
* An array of days or identifiers which should be canceled in the schedule.
*
* @see [[Schedule.cancel]]
*/
cancel?: (Day | IdentifierInput)[];
/**
* @see [[Schedule.meta]]
*/
meta?: { [identifier: string]: M };
/**
* @see [[Schedule.month]]
*/
month?: FrequencyValue;
/**
* @see [[Schedule.year]]
*/
year?: FrequencyValue;
/**
* @see [[Schedule.week]]
*/
week?: FrequencyValue;
/**
* @see [[Schedule.dayOfWeek]]
*/
dayOfWeek?: FrequencyValue;
/**
* @see [[Schedule.dayOfMonth]]
*/
dayOfMonth?: FrequencyValue;
/**
* @see [[Schedule.lastDayOfMonth]]
*/
lastDayOfMonth?: FrequencyValue;
/**
* @see [[Schedule.dayOfYear]]
*/
dayOfYear?: FrequencyValue;
/**
* @see [[Schedule.weekOfYear]]
*/
weekOfYear?: FrequencyValue;
/**
* @see [[Schedule.weekspanOfYear]]
*/
weekspanOfYear?: FrequencyValue;
/**
* @see [[Schedule.fullWeekOfYear]]
*/
fullWeekOfYear?: FrequencyValue;
/**
* @see [[Schedule.lastWeekspanOfYear]]
*/
lastWeekspanOfYear?: FrequencyValue;
/**
* @see [[Schedule.lastFullWeekOfYear]]
*/
lastFullWeekOfYear?: FrequencyValue;
/**
* @see [[Schedule.weekOfMonth]]
*/
weekOfMonth?: FrequencyValue;
/**
* @see [[Schedule.weekspanOfMonth]]
*/
weekspanOfMonth?: FrequencyValue;
/**
* @see [[Schedule.fullWeekOfMonth]]
*/
fullWeekOfMonth?: FrequencyValue;
/**
* @see [[Schedule.lastWeekspanOfMonth]]
*/
lastWeekspanOfMonth?: FrequencyValue;
/**
* @see [[Schedule.lastFullWeekOfMonth]]
*/
lastFullWeekOfMonth?: FrequencyValue;
/**
* The function to parse metadata with.
*/
parseMeta?: (input: any) => M;
}
/**
* A class which describes when an event occurs over what time and if it repeats.
*
* @typeparam M The type of metadata stored in the schedule.
*/
export class Schedule<M>
{
/**
* The earliest an event can occur in the schedule, or `null` if there are no
* restrictions when the earliest event can occur. This day is inclusive.
*/
public start: Day;
/**
* The latest an event can occur in the schedule, or `null` if there are no
* restrictions when the latest event can occur. This day is inclusive.
*/
public end: Day;
/**
* The length of events in this schedule.
*/
public duration: number;
/**
* The unit which describes the duration of the event.
*/
public durationUnit: DurationInput;
/**
* The times at which the events occur on the days they should. If there are
* no times specified its assumed to be an all day event - potentially over
* multiple days or weeks based on [[Schedule.duration]] and
* [[Schedule.durationUnit]].
*/
public times: Time[];
/**
* The number of days an event in this schedule lasts PAST the starting day.
* If this is a full day event with a duration greater than zero this value
* will be greater than one. If this event occurs at a specific time with a
* given duration that is taken into account and if it passes over into the
* next day this value will be greater than one. This value is used to look
* back in time when trying to figure out what events start or overlap on a
* given day.
*/
public durationInDays: number;
/**
* A set of identifiers which mark what days or times are excluded on the
* schedule. This typically represents the set of event occurrences removed.
*/
public exclude: ScheduleModifier<boolean>;
/**
* A set of identifiers which mark what days or times are included outside
* the normal series of days on the schedule. This typically represents
* an event occurrence which is moved so its added to the exclude and include
* sets.
*/
public include: ScheduleModifier<boolean>;
/**
* A set of identifiers which mark what days, times, weeks, months, etc that
* should have all event occurrences cancelled.
*/
public cancel: ScheduleModifier<boolean>;
/**
* A map of metadata keyed by an identifier. The metadata is placed in
* [[CalendarEvent]].
*/
public meta: ScheduleModifier<M>;
/**
* How frequent the event occurs based on [[Day.dayOfWeek]].
*/
public dayOfWeek: FrequencyCheck;
/**
* How frequent the event occurs based on [[Day.dayOfMonth]].
*/
public dayOfMonth: FrequencyCheck;
/**
* How frequent the event occurs based on [[Day.lastDayOfMonth]].
*/
public lastDayOfMonth: FrequencyCheck;
/**
* How frequent the event occurs based on [[Day.dayOfYear]].
*/
public dayOfYear: FrequencyCheck;
/**
* How frequent the event occurs based on [[Day.month]].
*/
public month: FrequencyCheck;
/**
* How frequent the event occurs based on [[Day.week]].
*/
public week: FrequencyCheck;
/**
* How frequent the event occurs based on [[Day.weekOfYear]].
*/
public weekOfYear: FrequencyCheck;
/**
* How frequent the event occurs based on [[Day.weekspanOfYear]].
*/
public weekspanOfYear: FrequencyCheck;
/**
* How frequent the event occurs based on [[Day.fullWeekOfYear]].
*/
public fullWeekOfYear: FrequencyCheck;
/**
* How frequent the event occurs based on [[Day.lastWeekspanOfYear]].
*/
public lastWeekspanOfYear: FrequencyCheck;
/**
* How frequent the event occurs based on [[Day.lastFullWeekOfYear]].
*/
public lastFullWeekOfYear: FrequencyCheck;
/**
* How frequent the event occurs based on [[Day.weekOfMonth]].
*/
public weekOfMonth: FrequencyCheck;
/**
* How frequent the event occurs based on [[Day.weekspanOfMonth]].
*/
public weekspanOfMonth: FrequencyCheck;
/**
* How frequent the event occurs based on [[Day.fullWeekOfMonth]].
*/
public fullWeekOfMonth: FrequencyCheck;
/**
* How frequent the event occurs based on [[Day.lastWeekspanOfMonth]].
*/
public lastWeekspanOfMonth: FrequencyCheck;
/**
* How frequent the event occurs based on [[Day.lastFullWeekOfMonth]].
*/
public lastFullWeekOfMonth: FrequencyCheck;
/**
* How frequent the event occurs based on [[Day.year]].
*/
public year: FrequencyCheck;
/**
* The array of frequency functions which had valid frequencies.
*
* @see [[FrequencyCheck.given]]
*/
public checks: FrequencyCheck[];
/**
* Creates a schedule based on the given input.
*
* @param input The input which describes the schedule of events.
*/
public constructor(input?: ScheduleInput<M>)
{
this.exclude = new ScheduleModifier<boolean>();
this.include = new ScheduleModifier<boolean>();
this.cancel = new ScheduleModifier<boolean>();
this.meta = new ScheduleModifier<M>();
if (fn.isDefined(input))
{
this.set(input);
}
}
/**
* Sets the schedule with the given input.
*
* @param input The input which describes the schedule of events.
* @param parseMeta A function to use when parsing meta input into the desired type.
* @see [[Parse.schedule]]
*/
public set(input: ScheduleInput<M>,
parseMeta: (input: any) => M = (x => <M>x)): this
{
Parse.schedule<M>(input, fn.coalesce( input.parseMeta, parseMeta ), this);
return this;
}
/**
* Returns the last event time specified or `undefined` if this schedule is
* for an all day event.
*/
public get lastTime(): Time
{
return this.times[ this.times.length - 1 ];
}
/**
* The [[Identifier]] for this schedule. Either [[Identifier.Day]] or
* [[Identifier.Time]].
*/
public get identifierType(): Identifier
{
return this.isFullDay() ? Identifier.Day : Identifier.Time;
}
/**
* Updates the [[Schedule.durationInDays]] variable based on the
* [[Schedule.lastTime]] (if any), the [[Schedule.duration]] and it's
* [[Schedule.durationUnit]].
*/
public updateDurationInDays(): this
{
let start: number = this.lastTime ? this.lastTime.toMilliseconds() : 0;
let duration: number = this.duration * (Constants.DURATION_TO_MILLIS[ this.durationUnit ] || 0);
let exclude: number = Constants.MILLIS_IN_DAY;
let day: number = Constants.MILLIS_IN_DAY;
this.durationInDays = Math.max(0, Math.ceil((start + duration - exclude) / day));
return this;
}
/**
* Updates [[Schedule.checks]] based on the frequencies that were specified
* in the schedule input.
*/
public updateChecks(): this
{
this.checks = Parse.givenFrequency([
this.year,
this.month,
this.week,
this.weekOfYear,
this.fullWeekOfYear,
this.weekspanOfYear,
this.lastFullWeekOfYear,
this.lastWeekspanOfYear,
this.weekOfMonth,
this.weekspanOfMonth,
this.fullWeekOfMonth,
this.lastWeekspanOfMonth,
this.lastFullWeekOfMonth,
this.dayOfWeek,
this.dayOfMonth,
this.lastDayOfMonth,
this.dayOfYear
]);
return this;
}
/**
* Determines whether the given day lies between the earliest and latest
* valid day in the schedule.
*
* @param day The day to test.
* @returns `true` if the day lies in the schedule, otherwise `false`.
* @see [[Schedule.start]]
* @see [[Schedule.end]]
*/
public matchesSpan(day: Day): boolean
{
return (this.start === null || day.isSameOrAfter(this.start)) &&
(this.end === null || day.isBefore(this.end));
}
/**
* Determines whether the given range overlaps with the earliest and latest
* valid days in this schedule (if any).
*
* @param start The first day in the range.
* @param end The last day in the range.
* @returns `true` if the range intersects with the schedule, otherwise `false`.
* @see [[Schedule.start]]
* @see [[Schedule.end]]
*/
public matchesRange(start: Day, end: Day): boolean
{
if (this.start && end.isBefore(this.start))
{
return false;
}
if (this.end && start.isAfter(this.end))
{
return false;
}
return true;
}
/**
* Determines whether the given day is explicitly excluded in the schedule.
*
* @param day The day to test.
* @param lookAtTime lookAtTime If the specific time of the given day should
* be looked at.
* @returns `true` if the day was excluded, otherwise `false`.
*/
public isExcluded(day: Day, lookAtTime: boolean = true): boolean
{
return this.exclude.get( day, false, lookAtTime );
}
/**
* Determines whether the given day is explicitly included in the schedule.
*
* @param day The day to test.
* @param lookAtTime lookAtTime If the specific time of the given day should
* be looked at.
* @returns `true` if the day is NOT explicitly included, otherwise `false`.
*/
public isIncluded(day: Day, lookAtTime: boolean = true): boolean
{
return this.include.get( day, false, lookAtTime );
}
/**
* Determines whether the given day is cancelled in the schedule.
*
* @param day The day to test.
* @param lookAtTime lookAtTime If the specific time of the given day should
* be looked at.
* @returns `true` if the day was cancelled, otherwise `false`.
*/
public isCancelled(day: Day, lookAtTime: boolean = true): boolean
{
return this.cancel.get( day, false, lookAtTime );
}
/**
* Returns the metadata for the given day or `null` if there is none.
*
* @param day The day to return the metadata for.
* @param otherwise The data to return if none exists for the given day.
* @param lookAtTime lookAtTime If the specific time of the given day should
* be looked at.
* @returns The metadata or `null`.
*/
public getMeta(day: Day, otherwise: M = null, lookAtTime: boolean = true): M
{
return this.meta.get( day, otherwise, lookAtTime );
}
/**
* Returns all metadata for the given day or an empty array if there is none.
*
* @param day The day to return the metadata for.
* @returns The array of metadata ordered by priority or an empty array.
*/
public getMetas(day: Day): M[]
{
return this.meta.getAll( day );
}
/**
* Returns whether the events in the schedule are all day long or start at
* specific times. Full day events start at the start of the day and end at
* the start of the next day (if the duration = `1` and durationUnit = 'days').
* Full day events have no times specified and should have a durationUnit of
* either `days` or `weeks`.
*/
public isFullDay(): boolean
{
return this.times.length === 0;
}
/**
* Sets whether this schedule is a full day event if it is not already. If
* this schedule is a full day event and `false` is passed to this function
* a single timed event will be added based on `defaultTime`. If this schedule
* has timed events and `true` is passed to make the schedule full day, the
* timed events are removed from this schedule. If the durationUnit is not the
* expected unit based on the new full day flag - the duration is reset to 1
* and the duration unit is set to the expected unit.
*
* @param fullDay Whether this schedule should represent a full day event or
* timed events.
* @param defaultTime If `fullDay` is `false` and this schedule is currently
* a full day event - this time will be used as the time of the first event.
*/
public setFullDay(fullDay: boolean = true, defaultTime: TimeInput = '08:00'): this
{
if (fullDay !== this.isFullDay())
{
if (fullDay)
{
this.times = [];
if (this.durationUnit !== 'days' && this.durationUnit !== 'day')
{
this.duration = 1;
this.durationUnit = 'days';
}
}
else
{
this.times = [Parse.time( defaultTime )];
if (this.durationUnit !== 'hours' && this.durationUnit !== 'hour')
{
this.duration = 1;
this.durationUnit = 'hours';
}
}
}
return this;
}
/**
* Adjusts the [[Schedule.start]] and [[Schedule.end]] dates specified on this
* schedule if this schedule represents a single event and the `start` and
* `end` are already set or `addSpan` is `true`.
*
* @param addSpan If `true`, the `start` and `end` dates will always be
* adjusted if this schedule is a single event.
*/
public adjustDefinedSpan(addSpan: boolean = false): this
{
let single: DaySpan = this.getSingleEventSpan();
if (single && (addSpan || (this.start && this.end)))
{
this.start = single.start.start();
this.end = single.end.end();
}
return this;
}
/**
* Returns a span of time for a schedule with full day events starting on the
* start of the given day with the desired duration in days or weeks.
*
* @param day The day the span starts on.
* @returns The span of time starting on the given day.
*/
public getFullSpan(day: Day): DaySpan
{
let start: Day = day.start();
let end: Day = start.add( this.duration, this.durationUnit );
return new DaySpan( start, end );
}
/**
* Returns a span of time starting on the given day at the given day with the
* duration specified on this schedule.
*
* @param day The day the span starts on.
* @param time The time of day the span starts.
* @returns The span of time calculated.
*/
public getTimeSpan(day: Day, time: Time): DaySpan
{
let start: Day = day.withTime( time );
let end: Day = start.add( this.duration, this.durationUnit );
return new DaySpan( start, end );
}
/**
* Determines whether the given day is a day on the schedule for the start
* of an event. If an event is more than one day and the day given is not the
* start this may return `false`. This does not test for event instances
* that exist through [[Schedule.include]].
*
* @param day The day to test.
* @returns `true` if the day marks the start of an event on the schedule.
* @see [[Schedule.isIncluded]]
* @see [[Schedule.isFullyExcluded]]
* @see [[Schedule.matchesSpan]]
*/
public matchesDay(day: Day): boolean
{
if (this.isIncluded( day, false ))
{
return true;
}
if (!this.matchesSpan( day ) || this.isFullyExcluded( day ))
{
return false;
}
for (let check of this.checks)
{
if (!check( <number>day[ check.property ] ))
{
return false;
}
}
return true;
}
/**
* Determines whether the given day has events added through
* [[Schedule.include]].
*
* @param day The day to look for included times on.
* @returns `true` if there are included event instances on the given day,
* otherwise `false`.
*/
public hasIncludedTime(day: Day): boolean
{
return !this.iterateIncludeTimes( day ).isEmpty();
}
/**
* Determines whether the given day is fully excluded from the schedule. A
* fully excluded day is one that has a day-wide exclusion, or the schedule
* is not an all-day event and all times in the schedule are specifically
* excluded.
*
* @param day The day to test.*
* @returns `true` if he day is fully excluded, otherwise `false`.
*/
public isFullyExcluded(day: Day): boolean
{
if (this.isExcluded(day, false))
{
return true;
}
if (this.isFullDay())
{
return false;
}
for (let time of this.times)
{
if (!this.isExcluded( day.withTime( time ) ))
{
return false;
}
}
return true;
}
/**
* Finds the next day an event occurs on the schedule given a day to start,
* optionally including it, and a maximum number of days to look ahead.
*
* @param day The day to start to search from.
* @param includeDay If the given day should be included in the search.
* @param lookAhead The maximum number of days to look ahead from the given
* day for event occurrences.
* @returns The next day on the schedule or `null` if none exists.
*/
public nextDay(day: Day, includeDay: boolean = false, lookAhead: number = 366): Day
{
return this.iterateDaycast(day, 1, true, includeDay, lookAhead).first();
}
/**
* Finds the next specified number of days that events occur on the schedule
* given a day to start, optionally including it, and a maximum number of days
* to look ahead.
*
* @param day The day to start to search from.
* @param max The maximum number of days to return in the result.
* @param includeDay If the given day should be included in the search.
* @param lookAhead The maximum number of days to look ahead from the given
* day for event occurrences.
* @returns An array containing the next days on the schedule that events
* start or an empty array if there are none.
*/
public nextDays(day: Day, max: number, includeDay: boolean = false, lookAhead: number = 366): Iterator<Day>
{
return this.iterateDaycast(day, max, true, includeDay, lookAhead);
}
/**
* Finds the previous day an event occurs on the schedule given a day to start,
* optionally including it, and a maximum number of days to look behind.
*
* @param day The day to start to search from.
* @param includeDay If the given day should be included in the search.
* @param lookBack The maximum number of days to look behind from the given
* day for event occurrences.
* @returns The previous day on the schedule or `null` if none exists.
*/
public prevDay(day: Day, includeDay: boolean = false, lookBack: number = 366): Day
{
return this.iterateDaycast(day, 1, false, includeDay, lookBack).first();
}
/**
* Finds the previous specified number of days that events occur on the
* schedule given a day to start, optionally including it, and a maximum
* number of days to look behind.
*
* @param day The day to start to search from.
* @param max The maximum number of days to return in the result.
* @param includeDay If the given day should be included in the search.
* @param lookAhead The maximum number of days to look behind from the given
* day for event occurrences.
* @returns An array containing the previous days on the schedule that events
* start or an empty array if there are none.
*/
public prevDays(day: Day, max: number, includeDay: boolean = false, lookBack: number = 366): Iterator<Day>
{
return this.iterateDaycast(day, max, false, includeDay, lookBack);
}
/**
* Iterates over days that events start in the schedule given a day to start,
* a maximum number of days to find, and a direction to look.
*
* @param day The day to start to search from.
* @param max The maximum number of days to iterate.
* @param next If `true` this searches forward, otherwise `false` is backwards.
* @param includeDay If the given day should be included in the search.
* @param lookup The maximum number of days to look through from the given
* day for event occurrences.
* @returns A new Iterator for the days found in the cast.
* @see [[Schedule.iterateSpans]]
*/
public iterateDaycast(day: Day, max: number, next: boolean, includeDay: boolean = false, lookup: number = 366): Iterator<Day>
{
return new Iterator<Day>(iterator =>
{
let iterated: number = 0;
for (let days = 0; days < lookup; days++)
{
if (!includeDay || days > 0)
{
day = next ? day.next() : day.prev();
}
if (!this.iterateSpans( day, false ).isEmpty())
{
let action: IteratorAction = iterator.act( day );
if (action === IteratorAction.Stop || ++iterated >= max)
{
return;
}
}
}
});
}
/**
* Iterates through the spans (event instances) that start on or covers the
* given day.
*
* @param day The day to look for spans on.
* @param covers If `true` spans which span multiple days will be looked at
* to see if they intersect with the given day, otherwise `false` will
* only look at the given day for the start of events.
* @returns A new Iterator for all the spans found.
*/
public iterateSpans(day: Day, covers: boolean = false): Iterator<DaySpan>
{
return new Iterator<DaySpan>(iterator =>
{
let current: Day = day;
let lookBehind: number = covers ? this.durationInDays : 0;
// If the events start at the end of the day and may last multiple days....
if (this.isFullDay())
{
// If the schedule has events which span multiple days we need to look
// backwards for events that overlap with the given day.
while (lookBehind >= 0)
{
// If the current day matches the schedule rules...
if (this.matchesDay( current ))
{
// Build a DaySpan with the given start day and the schedules duration.
let span: DaySpan = this.getFullSpan( current );
// If that dayspan intersects with the given day, it's a winner!
if (span.matchesDay( day ))
{
switch (iterator.act( span ))
{
case IteratorAction.Stop:
return;
}
}
}
current = current.prev();
lookBehind--;
}
}
// This schedule has events which start at certain times
else
{
// If the schedule has events which span multiple days we need to look
// backwards for events that overlap with the given day.
while (lookBehind >= 0)
{
// If the current day matches the schedule rules...
if (this.matchesDay( current ))
{
// Iterate through each daily occurrence in the schedule...
for (let time of this.times)
{
let span: DaySpan = this.getTimeSpan( current, time );
// If the event intersects with the given day and the occurrence
// has not specifically been excluded...
if (span.matchesDay( day ) && !this.isExcluded( span.start, true ))
{
switch (iterator.act( span ))
{
case IteratorAction.Stop:
return;
}
}
}
}
else
{
// The current day does not match the schedule, however the schedule
// might have moved/random event occurrents on the current day.
// We only want the ones that overlap with the given day.
this.iterateIncludeTimes(current, day).iterate((span, timeIterator) =>
{
switch (iterator.act( span ))
{
case IteratorAction.Stop:
timeIterator.stop();
break;
}
})
if (iterator.action === IteratorAction.Stop)
{
return;
}
}
current = current.prev();
lookBehind--;
}
}
});
}
/**
* Determines if the given day is on the schedule and the time specified on
* the day matches one of the times on the schedule.
*
* @param day The day to test.
* @returns `true` if the day and time match the schedule, otherwise false.
*/
public matchesTime(day: Day): boolean
{
return !!this.iterateSpans( day, true ).first( span => span.start.sameMinute( day ) );
}
/**
* Determines if the given day is covered by this schedule. A schedule can
* specify events that span multiple days - so even though the day does not
* match the starting day of a span - it can be a day that is within the
* schedule.
*
* @param day The day to test.
* @returns `true` if the day is covered by an event on this schedule,
* otherwise `false`.
*/
public coversDay(day: Day): boolean
{
return !this.iterateSpans( day, true ).isEmpty();
}
/**
* Determines if the given timestamp lies in an event occurrence on this
* schedule.
*
* @param day The timestamp to test against the schedule.
* @return `true` if the timestamp lies in an event occurrent start and end
* timestamps, otherwise `false`.
*/
public coversTime(day: Day): boolean
{
return !!this.iterateSpans( day, true ).first( span => span.contains( day ) );
}
/**
* Sets the frequency for the given property. This does not update the
* [[Schedule.checks]] array, the [[Schedule.updateChecks]] function needs
* to be called.
*
* @param property The frequency to update.
* @param frequency The new frequency.
*/
public setFrequency(property: DayProperty, frequency?: FrequencyValue): this
{
this[ property ] = Parse.frequency( frequency, property );
return this;
}
/**