forked from ClickerMonkey/dayspan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dayspan.js
6758 lines (6604 loc) · 259 KB
/
dayspan.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
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
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("moment"));
else if(typeof define === 'function' && define.amd)
define("ds", ["moment"], factory);
else if(typeof exports === 'object')
exports["ds"] = factory(require("moment"));
else
root["ds"] = factory(root["moment"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_0__) {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 1);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_0__;
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
module.exports = __webpack_require__(2);
/***/ }),
/* 2 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
// CONCATENATED MODULE: ./src/Functions.ts
/**
* The class which contains commonly used functions by the library. These
* functions and variables exist in a class so they may be overridden if
* desired.
*/
var Functions = (function () {
function Functions() {
}
/**
* Determines whether the given input is an array.
*
* @param input The variable to test.
* @returns `true` if the variable is an array, otherwise `false`.
*/
Functions.isArray = function (input) {
return input instanceof Array;
};
/**
* Determines whether the two arrays given are stricly equivalent. If the
* arrays are not the same length or contain the same values in the same order
* then `false` is returned.
*
* @param x The first array to test.
* @param y The second array to test.
* @returns `true` if they have the same exact values, otherwise `false`.
*/
Functions.isArrayEquals = function (x, y) {
if (x === y)
return true;
if (x.length !== y.length)
return false;
for (var i = 0; i < x.length; i++) {
if (x[i] !== y[i]) {
return false;
}
}
return true;
};
/**
* Determines whether the given input is a string.
*
* @param input The variable to test.
* @returns `true` if the variable is a string, otherwise `false`.
*/
Functions.isString = function (input) {
return typeof (input) === 'string';
};
/**
* Determines whether the given input is a finite number (a number which is
* not infinite or not the result of a divide-by-zero operation).
*
* @param input The variable to test.
* @returns `true` if the variable is a finite number, otherwise `false`.
*/
Functions.isNumber = function (input) {
return isFinite(input) && typeof input === 'number';
};
/**
* Determines whether the given input is an object and NOT an array.
*
* @param input The variable to test.
* @returns `true` if the variable is a plain object, otherwise `false`.
*/
Functions.isObject = function (input) {
return input !== null && !this.isArray(input) && typeof (input) === 'object';
};
/**
* Determines whether the given input is defined.
*
* @param input The variable to test.
* @return `true` if the variable is defined, otherwise `false`.
*/
Functions.isDefined = function (input) {
return typeof (input) !== 'undefined';
};
/**
* Determines whether the given input is defined and not null.
*
* @param input The variable to test.
* @return `true` if the variable is defined and not null, otherwise `false`.
*/
Functions.isValue = function (input) {
return input !== null && typeof (input) !== 'undefined';
};
/**
* Determines whether the given input appears to be a valid
* [[FrequencyValueEvery]].
*
* ```typescript
* Functions.isFrequencyValueEvery({}); // false
* Functions.isFrequencyValueEvery([]); // false
* Functions.isFrequencyValueEvery([1]); // false
* Functions.isFrequencyValueEvery(null); // false
* Functions.isFrequencyValueEvery({every:2}); // true
* Functions.isFrequencyValueEvery({offset:1}); // false
* Functions.isFrequencyValueEvery({every:2, offset:1}); // true
* ```
*
* @param input The variable to test.
* @returns `true` if the variable appears to be a [[FrequencyValueEvery]],
* otherwise false.
*/
Functions.isFrequencyValueEvery = function (input) {
return this.isObject(input) && this.isNumber(input.every);
};
/**
* Determines whether the given input appears to be a valid
* [[FrequencyValueOneOf]].
*
* ```typescript
* Functions.isFrequencyValueOneOf({}); // false
* Functions.isFrequencyValueOneOf([]); // false
* Functions.isFrequencyValueOneOf([1]); // true
* Functions.isFrequencyValueOneOf(null); // false
* ```
*
* @param input The variable to test.
* @returns `true` if the variable appears to be a [[FrequencyValueOneOf]],
* otherwise false.
*/
Functions.isFrequencyValueOneOf = function (input) {
return this.isArray(input) && input.length > 0;
};
/**
* Returns the first argument which is defined.
*
* ```typescript
* Functions.coalesce(3, 4); // 3
* Functions.coalesce(undefined, 4); // 4
* Functions.coalesce(null, 4); // null
* Functions.coalesce(void 0, void 0, 5); // 5
* ```
*
* @param a The first argument to look at.
* @param b The second argument to look at.
* @returns The first defined argument.
* @see [[Functions.isDefined]]
*/
Functions.coalesce = function (a, b, c) {
return this.isDefined(a) ? a : (this.isDefined(b) ? b : c);
};
/**
* Copies values from `from` object and sets them to the `target` object.
*
* @param target The object to set values to.
* @param from The object to copy value references from.
* @returns The reference to `target`.
*/
Functions.extend = function (target, from) {
for (var prop in from) {
target[prop] = from[prop];
}
return target;
};
/**
* Pads the string `x` up to `length` characters with the given `padding`
* optionally placing the `padding` `before` `x`.
*
* ```typescript
* Functions.pad('hey', 5, '_', false); // 'hey__'
* Functions.pad('hey', 5, '_', true); // '__hey'
* Functions.pad('heyman', 5, '_', true); // 'heyman'
* ```
*
* @param x The string to pad.
* @param length The length to pad to.
* @param padding The string to pad with.
* @param before If the padding should go before the string to pad.
* @returns The padded string if any padding needed be added.
*/
Functions.pad = function (x, length, padding, before) {
while (x.length < length) {
before ? x = padding + x : x = x + padding;
}
return x;
};
/**
* Pads the number `x` up to `length` digits where the padding is `0` and it
* goes before `x`. This function will only return the first `length`
* characters of the padding string representation of the number but can return
* an alternative number of `first` characters.
*
* ```typescript
* Functions.padNumber(29, 3); // '029'
* Functions.padNumber(29, 3, 2); // '02'
* Functions.padNumber(9573, 3); // '957'
* ```
*
* @param x The number to pad with zeros in the beginning.
* @param length The number of digits the number should be padded to.
* @param first The number of digits to return from the start of the string.
* @returns A padded number.
*/
Functions.padNumber = function (x, length, first) {
if (first === void 0) { first = length; }
return this.pad(x + '', length, '0', true).substring(0, first);
};
return Functions;
}());
// CONCATENATED MODULE: ./src/Operation.ts
/**
* An operation that can be performed on a single number.
*/
var Op;
(function (Op) {
/**
* The number is returned unmodified.
*/
Op[Op["NONE"] = 0] = "NONE";
/**
* The number is rounded down to the nearest whole number.
*/
Op[Op["FLOOR"] = 1] = "FLOOR";
/**
* The number is rounded up to the nearest whole number.
*/
Op[Op["CEIL"] = 2] = "CEIL";
/**
* The number is rounded up or down depending on if the fractional value is
* greater than or less than 0.5 respectively.
*/
Op[Op["ROUND"] = 3] = "ROUND";
/**
* The fractional part of the number is dropped.
*/
Op[Op["TRUNCATE"] = 4] = "TRUNCATE";
/**
* The number is rounded up when positive and down when negative. This is
* effectively ceiling the absolute value where the result preserves the sign.
*/
Op[Op["UP"] = 5] = "UP";
/**
* The number is rounded down when positive and up when negative. This is
* effectively floor the absolute value where the result preserves the sign.
*/
Op[Op["DOWN"] = 6] = "DOWN";
})(Op = Op || (Op = {}));
/**
* Performs the requested operation on the given number, optionally taking
* the absolute value of the number before the operation.
*
* @param value The number to operate on.
* @param op The operation to perform.
* @param absolute If the number should be positive before the operation.
* @return The operated result, or the original value if its not a valid number.
*/
function operate(value, op, absolute) {
if (absolute === void 0) { absolute = false; }
if (isFinite(value)) {
if (absolute) {
value = Math.abs(value);
}
switch (op) {
case Op.NONE:
return value;
case Op.FLOOR:
return Math.floor(value);
case Op.CEIL:
return Math.ceil(value);
case Op.ROUND:
return Math.round(value);
case Op.TRUNCATE:
case Op.DOWN:
return value < 0 ? Math.ceil(value) : Math.floor(value);
case Op.UP:
return value < 0 ? Math.floor(value) : Math.ceil(value);
}
}
return value;
}
// CONCATENATED MODULE: ./src/Units.ts
/**
* Units of time that are compromised of 1 or more days for the [[Calendar]] class.
*/
var Units;
(function (Units) {
Units[Units["DAY"] = 0] = "DAY";
Units[Units["WEEK"] = 1] = "WEEK";
Units[Units["MONTH"] = 2] = "MONTH";
Units[Units["YEAR"] = 3] = "YEAR";
})(Units = Units || (Units = {}));
// CONCATENATED MODULE: ./src/Constants.ts
/**
* A class that stores commonly used values.
*/
var Constants = (function () {
function Constants() {
}
/**
* The number of milliseconds in a second.
*/
Constants.MILLIS_IN_SECOND = 1000;
/**
* The number of milliseconds in a minute.
*/
Constants.MILLIS_IN_MINUTE = Constants.MILLIS_IN_SECOND * 60;
/**
* The number of milliseconds in an hour.
*/
Constants.MILLIS_IN_HOUR = Constants.MILLIS_IN_MINUTE * 60;
/**
* The number of milliseconds in a day (not including DST days).
*/
Constants.MILLIS_IN_DAY = Constants.MILLIS_IN_HOUR * 24;
/**
* The number of milliseconds in a week (not including ones that include DST).
*/
Constants.MILLIS_IN_WEEK = Constants.MILLIS_IN_DAY * 7;
/**
* The number of minutes in an hour.
*/
Constants.MINUTES_IN_HOUR = 60;
/**
* The number of minutes in a day (not including DST days).
*/
Constants.MINUTES_IN_DAY = 60 * 24;
/**
* The number of days in a week.
*/
Constants.DAYS_IN_WEEK = 7;
/**
* The number of months in a year.
*/
Constants.MONTHS_IN_YEAR = 12;
/**
* The number of hours in a day (not including DST days).
*/
Constants.HOURS_IN_DAY = 24;
/**
* The first month of the year.
*/
Constants.MONTH_MIN = 0;
/**
* The last month of the year.
*/
Constants.MONTH_MAX = 11;
/**
* The first day of a month.
*/
Constants.DAY_MIN = 1;
/**
* The last day of the longest month.
*/
Constants.DAY_MAX = 31;
/**
* The first hour of the day.
*/
Constants.HOUR_MIN = 0;
/**
* The last hour of the day.
*/
Constants.HOUR_MAX = 23;
/**
* The first minute of the hour.
*/
Constants.MINUTE_MIN = 0;
/**
* The last minute of the hour.
*/
Constants.MINUTE_MAX = 59;
/**
* The first second of the minute.
*/
Constants.SECOND_MIN = 0;
/**
* The last second of the minute.
*/
Constants.SECOND_MAX = 59;
/**
* The first millisecond of the second.
*/
Constants.MILLIS_MIN = 0;
/**
* The last millisecond of the second.
*/
Constants.MILLIS_MAX = 999;
/**
* The first day of the week.
*/
Constants.WEEKDAY_MIN = 0;
/**
* The last day of the week.
*/
Constants.WEEKDAY_MAX = 6;
/**
* The default duration for an event.
*/
Constants.DURATION_DEFAULT = 1;
/**
* The default duration unit for an all day event.
*/
Constants.DURATION_DEFAULT_UNIT_ALL = 'days';
/**
* The default duration unit for an event at a given time.
*/
Constants.DURATION_DEFAULT_UNIT_TIMES = 'hours';
/**
* Computes the duration unit given its for an all day event.
*
* @param all If the event is all day.
* @return The default unit for the event.
*/
Constants.DURATION_DEFAULT_UNIT = function (all) { return all ? Constants.DURATION_DEFAULT_UNIT_ALL :
Constants.DURATION_DEFAULT_UNIT_TIMES; };
/**
* The number of milliseconds for various duration units. These are worse case
* scenario and do not include DST changes.
*/
Constants.DURATION_TO_MILLIS = {
minute: Constants.MILLIS_IN_MINUTE,
minutes: Constants.MILLIS_IN_MINUTE,
hour: Constants.MILLIS_IN_HOUR,
hours: Constants.MILLIS_IN_HOUR,
day: Constants.MILLIS_IN_DAY,
days: Constants.MILLIS_IN_DAY,
week: Constants.MILLIS_IN_WEEK,
weeks: Constants.MILLIS_IN_WEEK,
month: Constants.MILLIS_IN_DAY * Constants.DAY_MAX,
months: Constants.MILLIS_IN_DAY * Constants.DAY_MAX
};
/**
* The maximum estimated number of events per day. This is used to calculate
* [[CalendarEvent.id]] to give each event a unique ID. If you think you will
* have more events than this per day, you can enlarge the value.
*/
Constants.MAX_EVENTS_PER_DAY = 24;
/**
* The day of the week which determines the first week of the year or month.
* By default this day is Thursday.
*/
Constants.WEEK_OF_MONTH_MINIMUM_WEEKDAY = 4;
return Constants;
}());
// CONCATENATED MODULE: ./src/DaySpan.ts
/**
* A class for a range of time between two [[Day]] timestamps.
*/
var DaySpan_DaySpan = (function () {
/**
* Creates a new span of time.
*
* @param start The starting timestamp.
* @param end The ending timestamp.
*/
function DaySpan(start, end) {
this.start = start;
this.end = end;
}
Object.defineProperty(DaySpan.prototype, "isPoint", {
/**
* Whether this span starts and ends on the same timestamp.
*/
get: function () {
return this.start.time === this.end.time;
},
enumerable: true,
configurable: true
});
/**
* Determines whether the given timestamp lies between the start and end
* timestamp.
*
* @param day The timestamp to test.
* @returns True if the day is >= the start and <= the end of this span.
*/
DaySpan.prototype.contains = function (day) {
return day.time >= this.start.time && day.time <= this.end.time;
};
/**
* Compares the given timestamp to this span. If the timestamp is before this
* span then `-1` is returned, if the timestamp is after this span then `1`
* us returned, otherwise `0` is returned when the timestamp is in this span.
*
* @param day The timestamp to compare to.
* @returns `-1`, `0`, or `1` depending on the given timestamp relative to
* this span.
*/
DaySpan.prototype.compareTo = function (day) {
return day.time < this.start.time ? -1 : (day.time > this.end.time ? 1 : 0);
};
/**
* Determines whether the given timestamp is between the start and end
* timestamp or lies on the same day as the start or end timestamp.
*
* @param day The timestamp to test.
* @see [[Day.sameDay]]
*/
DaySpan.prototype.matchesDay = function (day) {
return this.contains(day) || day.sameDay(this.start) || day.sameDay(this.end);
};
/**
* Determines whether the given timestamp is between the start and end
* timestamp or lies on the same week as the start or end timestamp.
*
* @param day The timestamp to test.
* @see [[Day.sameWeek]]
*/
DaySpan.prototype.matchesWeek = function (day) {
return this.contains(day) || day.sameWeek(this.start) || day.sameWeek(this.end);
};
/**
* Determines whether the given timestamp is between the start and end
* timestamp or lies on the same month as the start or end timestamp.
*
* @param day The timestamp to test.
* @see [[Day.sameMonth]]
*/
DaySpan.prototype.matchesMonth = function (day) {
return this.contains(day) || day.sameMonth(this.start) || day.sameMonth(this.end);
};
/**
* Determines whether the given timestamp is between the start and end
* timestamp or lies on the same year as the start or end timestamp.
*
* @param day The timestamp to test.
* @see [[Day.sameYear]]
*/
DaySpan.prototype.matchesYear = function (day) {
return this.contains(day) || day.sameYear(this.start) || day.sameYear(this.end);
};
/**
* Calculates the number of milliseconds between the start and end timestamp.
*
* @param op The operation to perform on the result.
* @param absolute Whether the result should always be positive.
* @returns The time between the start and end timestamp.
* @see [[Day.millisBetween]]
*/
DaySpan.prototype.millis = function (op, absolute) {
if (op === void 0) { op = Op.DOWN; }
if (absolute === void 0) { absolute = true; }
return this.start.millisBetween(this.end, op, absolute);
};
/**
* Calculates the number of seconds between the start and end timestamp.
*
* @param op The operation to perform on the result.
* @param absolute Whether the result should always be positive.
* @returns The time between the start and end timestamp.
* @see [[Day.secondsBetween]]
*/
DaySpan.prototype.seconds = function (op, absolute) {
if (op === void 0) { op = Op.DOWN; }
if (absolute === void 0) { absolute = true; }
return this.start.secondsBetween(this.end, op, absolute);
};
/**
* Calculates the number of minutes between the start and end timestamp.
*
* @param op The operation to perform on the result.
* @param absolute Whether the result should always be positive.
* @returns The time between the start and end timestamp.
* @see [[Day.minutesBetween]]
*/
DaySpan.prototype.minutes = function (op, absolute) {
if (op === void 0) { op = Op.DOWN; }
if (absolute === void 0) { absolute = true; }
return this.start.minutesBetween(this.end, op, absolute);
};
/**
* Calculates the number of hours between the start and end timestamp.
*
* @param op The operation to perform on the result.
* @param absolute Whether the result should always be positive.
* @returns The time between the start and end timestamp.
* @see [[Day.hoursBetween]]
*/
DaySpan.prototype.hours = function (op, absolute) {
if (op === void 0) { op = Op.DOWN; }
if (absolute === void 0) { absolute = true; }
return this.start.hoursBetween(this.end, op, absolute);
};
/**
* Calculates the number of days between the start and end timestamp.
*
* @param op The operation to perform on the result.
* @param absolute Whether the result should always be positive.
* @returns The time between the start and end timestamp.
* @see [[Day.daysBetween]]
*/
DaySpan.prototype.days = function (op, absolute) {
if (op === void 0) { op = Op.DOWN; }
if (absolute === void 0) { absolute = true; }
return this.start.daysBetween(this.end, op, absolute);
};
/**
* Calculates the number of weeks between the start and end timestamp.
*
* @param op The operation to perform on the result.
* @param absolute Whether the result should always be positive.
* @returns The time between the start and end timestamp.
* @see [[Day.weeksBetween]]
*/
DaySpan.prototype.weeks = function (op, absolute) {
if (op === void 0) { op = Op.DOWN; }
if (absolute === void 0) { absolute = true; }
return this.start.weeksBetween(this.end, op, absolute);
};
/**
* Calculates the number of months between the start and end timestamp.
*
* @param op The operation to perform on the result.
* @param absolute Whether the result should always be positive.
* @returns The time between the start and end timestamp.
* @see [[Day.monthsBetween]]
*/
DaySpan.prototype.months = function (op, absolute) {
if (op === void 0) { op = Op.DOWN; }
if (absolute === void 0) { absolute = true; }
return this.start.monthsBetween(this.end, op, absolute);
};
/**
* Calculates the number of years between the start and end timestamp.
*
* @param op The operation to perform on the result.
* @param absolute Whether the result should always be positive.
* @returns The time between the start and end timestamp.
* @see [[Day.yearsBetween]]
*/
DaySpan.prototype.years = function (op, absolute) {
if (op === void 0) { op = Op.DOWN; }
if (absolute === void 0) { absolute = true; }
return this.start.yearsBetween(this.end, op, absolute);
};
/**
* Returns a delta value between 0 and 1 which represents where the
* [[DaySpan.start]] is relative to the given day. The delta value would
* be less than 0 if the start of the event is before the given day.
*
* @param relativeTo The day to find the start delta relative to.
* @return A number between 0 and 1 if the start of this span is in the
* 24-hour period starting at the given timestamp, otherwise the value
* returned may be less than 0 or greater than 1.
*/
DaySpan.prototype.startDelta = function (relativeTo) {
return (this.start.time - relativeTo.time) / Constants.MILLIS_IN_DAY;
};
/**
* Returns a delta value between 0 and 1 which represents where the
* [[DaySpan.end]] is relative to the given day. The delta value would
* be greater than 1 if the end of the event is after the given day.
*
* @param relativeTo The day to find the end delta relative to.
* @return A number between 0 and 1 if the end of this span is in the
* 24-hour period starting at the given timestamp, otherwise the value
* returned may be less than 0 or greater than 1.
*/
DaySpan.prototype.endDelta = function (relativeTo) {
return (this.end.time - relativeTo.time) / Constants.MILLIS_IN_DAY;
};
/**
* Calculates the bounds for span event if it were placed in a rectangle which
* represents a day (24 hour period). By default the returned values are
* between 0 and 1 and can be scaled by the proper rectangle dimensions or the
* rectangle dimensions can be passed to this function.
*
* @param relativeTo The day to find the bounds relative to. If this is not the
* start of the day the returned bounds is relative to the given time.
* @param dayHeight The height of the rectangle of the day.
* @param dayWidth The width of the rectangle of the day.
* @param columnOffset The offset in the rectangle of the day to adjust this
* span by. This also reduces the width of the returned bounds to keep the
* bounds in the rectangle of the day.
* @param clip `true` if the bounds should stay in the day rectangle, `false`
* and the bounds may go outside the rectangle of the day for multi-day
* spans.
* @param offsetX How much to translate the left & right properties by.
* @param offsetY How much to translate the top & bottom properties by.
* @returns The calculated bounds for this span.
*/
DaySpan.prototype.getBounds = function (relativeTo, dayHeight, dayWidth, columnOffset, clip, offsetX, offsetY) {
if (dayHeight === void 0) { dayHeight = 1; }
if (dayWidth === void 0) { dayWidth = 1; }
if (columnOffset === void 0) { columnOffset = 0; }
if (clip === void 0) { clip = true; }
if (offsetX === void 0) { offsetX = 0; }
if (offsetY === void 0) { offsetY = 0; }
var startRaw = this.startDelta(relativeTo);
var endRaw = this.endDelta(relativeTo);
var start = clip ? Math.max(0, startRaw) : startRaw;
var end = clip ? Math.min(1, endRaw) : endRaw;
var left = columnOffset;
var right = dayWidth - left;
var top = start * dayHeight;
var bottom = end * dayHeight;
return {
top: top + offsetY,
bottom: bottom + offsetY,
height: bottom - top,
left: left + offsetX,
right: right + offsetX,
width: right
};
};
/**
* Summarizes this span given an approximate unit of time and a few other
* options. If the start and end are on the same unit, a single value will
* be returned. Otherwise a start and end will be returned with a `delimiter`.
*
* @param type The unit of time this span is for.
* @param dayOfWeek When `true` the weekday of the start and end are included.
* @param short When `true` the short form of weekdays and months will be used.
* @param repeat When `true` the year will be repeated on the start and end
* timestamp even if they are the same year.
* @param contextual When `true` the year will be hidden if it's the current
* year.
* @param delimiter The string to separate the start and end timestamps with.
* @returns The summary of this span.
*/
DaySpan.prototype.summary = function (type, dayOfWeek, short, repeat, contextual, delimiter) {
if (dayOfWeek === void 0) { dayOfWeek = true; }
if (short === void 0) { short = false; }
if (repeat === void 0) { repeat = false; }
if (contextual === void 0) { contextual = true; }
if (delimiter === void 0) { delimiter = ' - '; }
var formats = DaySpan.SUMMARY_FORMATS[type];
var today = Day_Day.today();
var showStartYear = !contextual || !this.start.sameYear(today);
var showEndYear = !contextual || !this.end.sameYear(today);
var start = this.start.format(formats(short, dayOfWeek, showStartYear));
var end = this.end.format(formats(short, dayOfWeek, showEndYear));
var summary = start;
if (start !== end) {
if (!repeat) {
summary = this.start.format(formats(short, dayOfWeek, !this.start.sameYear(this.end)));
}
summary += delimiter;
summary += end;
}
else {
summary = start;
}
return summary;
};
/**
* Determines whether the gven span intersects with this span.
*
* @param span The span to test.
* @returns `true` if the spans intersect, otherwise `false`.
*/
DaySpan.prototype.intersects = function (span) {
return !(this.end.time < span.start.time ||
this.start.time > span.end.time);
};
/**
* Calculates the intersection between this span and the given span. If there
* is no intersection between the two spans then `null` is returned.
*
* @param span The span to calculate the intersection with.
* @returns The intersection or `null` if none exists.
*/
DaySpan.prototype.intersection = function (span) {
var start = this.start.max(span.start);
var end = this.end.min(span.end);
return start.isAfter(end) ? null : new DaySpan(start, end);
};
/**
* Calculates the union between this span and the given span.
*
* @param span The span to calculate the union with.
* @returns The union of the two spans.
*/
DaySpan.prototype.union = function (span) {
var start = this.start.min(span.start);
var end = this.end.max(span.end);
return new DaySpan(start, end);
};
/**
* Returns a point [[DaySpan]] with the same start and end timestamp.
*
* @param day The timestamp which will be the start and end.
* @returns The new instance.
* @see [[DaySpan.isPoint]]
*/
DaySpan.point = function (day) {
return new DaySpan(day, day);
};
/**
* Formatting functions which assist the [[DaySpan.summary]] function.
*/
DaySpan.SUMMARY_FORMATS = (DaySpan__a = {},
DaySpan__a[Units.DAY] = function (short, dayOfWeek, year) {
return (dayOfWeek ? (short ? 'ddd, ' : 'dddd, ') : '') + (short ? 'MMM ' : 'MMMM ') + 'Do' + (year ? ' YYYY' : '');
},
DaySpan__a[Units.WEEK] = function (short, dayOfWeek, year) {
return (dayOfWeek ? (short ? 'ddd, ' : 'dddd, ') : '') + (short ? 'MMM ' : 'MMMM ') + 'Do' + (year ? ' YYYY' : '');
},
DaySpan__a[Units.MONTH] = function (short, dayOfWeek, year) {
return (short ? 'MMM' : 'MMMM') + (year ? ' YYYY' : '');
},
DaySpan__a[Units.YEAR] = function (short, dayOfWeek, year) {
return (year ? 'YYYY' : '');
},
DaySpan__a);
return DaySpan;
}());
var DaySpan__a;
// CONCATENATED MODULE: ./src/Identifier.ts
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
/**
* A class for detecting, parsing, and building identifiers to and from days.
*
* An identifier is a simple value which represents a span of time. It may
* represent an entire year, a quarter (3 months) of a year, a week of a year,
* a month in a year, a specific day of a month of a year, or a specific hour,
* minute, day, and month of a year.
*
* For example:
* - `2018`: The year 2018
* - `201801`: January 2018
* - `2014023`: The 23rd week of 2014
* - `20170311`: March 11th, 2017
* - `201406151651`: June 15th 2016 at 4:51 pm
* - `'0525'`: Year 525 of the first age, Elrond and Elros are born
*/
var Identifier_Identifier = (function () {
function Identifier() {
}
/**
* Determines whether the given identifier is this type.
*
* @param id The identifier to test.
* @returns `true` if the identifier is this type, otherwise `false`.
*/
Identifier.prototype.is = function (id) {
return (id + '').length === this.getLength();
};
/**
* Computes the identifier given values taken from a [[Day]].
*
* @param values The values to compute.
* @returns The computed identifier.
*/
Identifier.prototype.compute = function () {
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
var scales = this.getScales();
var total = 0;
for (var i = 0; i < values.length; i++) {
total += values[i] * scales[i];
}
return this.is(total) ? total : Functions.padNumber(total, this.getLength());
};
/**
* Decomputes the given identifier and returns values which describe a span
* of time.
*
* @param id The identifier to decompute.
* @returns The original values which computed the identifier.
*/
Identifier.prototype.decompute = function (id) {
var scales = this.getScales();
var total = Functions.isNumber(id) ? id : parseInt(id);
var values = [];
for (var i = 0; i < scales.length - 1; i++) {
var curr = scales[i + 0];
var next = scales[i + 1];
var mod = next / curr;
var value = total % mod;
values.push(value);
total = Math.floor(total / mod);
}
values.push(total);
return values;
};
/**
* Finds which identifier type matches the given identifier, if any.
*
* @param id The identifier to find the type of.
* @returns The found identifier type, otherwise `null` if none exists.
*/
Identifier.find = function (id) {