UNPKG

2.78 kBJavaScriptView Raw
1/**
2 * This class helps us to emulate python's generators, sorta.
3 */
4var IterResult = /** @class */ (function () {
5 function IterResult(method, args) {
6 this.minDate = null;
7 this.maxDate = null;
8 this._result = [];
9 this.total = 0;
10 this.method = method;
11 this.args = args;
12 if (method === 'between') {
13 this.maxDate = args.inc
14 ? args.before
15 : new Date(args.before.getTime() - 1);
16 this.minDate = args.inc ? args.after : new Date(args.after.getTime() + 1);
17 }
18 else if (method === 'before') {
19 this.maxDate = args.inc ? args.dt : new Date(args.dt.getTime() - 1);
20 }
21 else if (method === 'after') {
22 this.minDate = args.inc ? args.dt : new Date(args.dt.getTime() + 1);
23 }
24 }
25 /**
26 * Possibly adds a date into the result.
27 *
28 * @param {Date} date - the date isn't necessarly added to the result
29 * list (if it is too late/too early)
30 * @return {Boolean} true if it makes sense to continue the iteration
31 * false if we're done.
32 */
33 IterResult.prototype.accept = function (date) {
34 ++this.total;
35 var tooEarly = this.minDate && date < this.minDate;
36 var tooLate = this.maxDate && date > this.maxDate;
37 if (this.method === 'between') {
38 if (tooEarly)
39 return true;
40 if (tooLate)
41 return false;
42 }
43 else if (this.method === 'before') {
44 if (tooLate)
45 return false;
46 }
47 else if (this.method === 'after') {
48 if (tooEarly)
49 return true;
50 this.add(date);
51 return false;
52 }
53 return this.add(date);
54 };
55 /**
56 *
57 * @param {Date} date that is part of the result.
58 * @return {Boolean} whether we are interested in more values.
59 */
60 IterResult.prototype.add = function (date) {
61 this._result.push(date);
62 return true;
63 };
64 /**
65 * 'before' and 'after' return only one date, whereas 'all'
66 * and 'between' an array.
67 * @return {Date,Array?}
68 */
69 IterResult.prototype.getValue = function () {
70 var res = this._result;
71 switch (this.method) {
72 case 'all':
73 case 'between':
74 return res;
75 case 'before':
76 case 'after':
77 default:
78 return (res.length ? res[res.length - 1] : null);
79 }
80 };
81 IterResult.prototype.clone = function () {
82 return new IterResult(this.method, this.args);
83 };
84 return IterResult;
85}());
86export default IterResult;
87//# sourceMappingURL=iterresult.js.map
\No newline at end of file