forked from dhis2-club-tanzania/dhis2-period
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperiod.ts
87 lines (71 loc) · 1.64 KB
/
period.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
import { PeriodInstance } from '../utilities/period-instance.utility';
import { PeriodType } from './period-type';
/**
* @description
* Period class offers capabilities to get periods for different period types
*/
export class Period {
private _calendarId: string;
private _periodType: PeriodType;
private _type: string;
private _year: number;
private _preferences: any;
private _periods: any[];
private _currentYear: number;
constructor() {
this._calendarId = 'gregorian';
this._periodType = new PeriodType();
if (!this._periodType) {
throw new Error('Could not instantiate period type');
}
}
/**
* Set period type
* @param {string} type
*/
setType(type: string) {
if (!this._periodType.isValid(type)) {
throw new Error('Not a valid period type');
}
this._type = type;
return this;
}
setYear(year: number) {
this._year = year;
return this;
}
setCalendar(calendarId: string) {
this._calendarId = calendarId;
return this;
}
setPreferences(preferences: any) {
this._preferences = preferences;
return this;
}
get() {
if (this._type) {
const periodInstance: PeriodInstance = new PeriodInstance(
this._calendarId,
this._type,
this._preferences,
this._year
);
this._periods = periodInstance.get();
this._year = periodInstance.year();
this._currentYear = periodInstance.currentYear();
}
return this;
}
type() {
return this._type;
}
list() {
return this._periods;
}
year() {
return this._year;
}
currentYear() {
return this._currentYear;
}
}