renusify 3.1.3 → 3.1.5

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.
@@ -1,82 +1,143 @@
1
1
  export default class DateTime {
2
- constructor($r, langs) {
3
- this.$r = $r
4
- this.format = {
5
- narrow: {
6
- weekday: 'narrow'
7
- },
8
- day: {
9
- day: 'numeric'
10
- },
11
- month: {
12
- month: 'short'
13
- },
14
- year: {
15
- year: 'numeric'
16
- },
17
- short: {
18
- year: 'numeric', month: 'short', day: 'numeric'
19
- },
20
- medium: {
21
- year: 'numeric', month: 'numeric', day: 'numeric'
22
- },
23
- weekday: {
24
- weekday: 'short'
25
- },
26
- long: {
27
- year: 'numeric',
28
- month: 'numeric',
29
- day: 'numeric',
30
- weekday: 'short',
31
- hour: 'numeric',
32
- minute: 'numeric',
33
- second: 'numeric'
34
- }
35
- }
2
+ constructor($r, langs) {
3
+ this.$r = $r;
4
+ this.format = {
5
+ narrow: {
6
+ weekday: "narrow",
7
+ },
8
+ day: {
9
+ day: "numeric",
10
+ },
11
+ month: {
12
+ month: "short",
13
+ },
14
+ year: {
15
+ year: "numeric",
16
+ },
17
+ short: {
18
+ year: "numeric",
19
+ month: "short",
20
+ day: "numeric",
21
+ },
22
+ medium: {
23
+ year: "numeric",
24
+ month: "numeric",
25
+ day: "numeric",
26
+ },
27
+ weekday: {
28
+ weekday: "short",
29
+ },
30
+ long: {
31
+ year: "numeric",
32
+ month: "numeric",
33
+ day: "numeric",
34
+ weekday: "short",
35
+ hour: "numeric",
36
+ minute: "numeric",
37
+ second: "numeric",
38
+ },
39
+ "date-latin": {
40
+ month: "numeric",
41
+ day: "numeric",
42
+ numberingSystem: "latn",
43
+ },
44
+ };
45
+
46
+ this.langs = langs;
47
+ }
36
48
 
37
- this.langs = langs;
49
+ set_lang(name, lang) {
50
+ if (!lang["localizeName"]) {
51
+ console.error(
52
+ "new Lang Must contain localizeName and timeZone (en: {\n" +
53
+ " first_day:0,\n" +
54
+ " time_zone_offset:-480,//Standard timezone offset minutes -07:00\n" +
55
+ " localizeName: 'en-US',\n" +
56
+ " timeZone: 'America/Los_Angeles'\n" +
57
+ " })",
58
+ );
59
+ return;
38
60
  }
61
+ this.langs[name] = lang;
62
+ }
39
63
 
40
- set_lang(name, lang) {
41
- if (!lang['localizeName']) {
42
- console.error('new Lang Must contain localizeName and timeZone (en: {\n' +
43
- ' first_day:0,\n' +
44
- ' time_zone_offset:-480,//Standard timezone offset minutes -07:00\n' +
45
- ' localizeName: \'en-US\',\n' +
46
- ' timeZone: \'America/Los_Angeles\'\n' +
47
- ' })')
48
- return
49
- }
50
- this.langs[name] = lang
51
- };
64
+ set_format(value) {
65
+ Object.assign(this.format, this.format, value);
66
+ }
52
67
 
53
- set_format(value) {
54
- Object.assign(this.format, this.format, value)
55
- };
68
+ formatLocal(value, type = "long", local = null) {
69
+ if (local === null) {
70
+ local = this.$r.lang;
71
+ }
72
+ if (!(local in this.langs)) {
73
+ console.warn(
74
+ "set local:" +
75
+ local +
76
+ ' with this.$dateTime.set_lang("' +
77
+ local +
78
+ '",{\n' +
79
+ ' localizeName: "en-US",\n' +
80
+ " first_day:0\n" +
81
+ " })",
82
+ );
83
+ this.langs[local] = {
84
+ localizeName: local,
85
+ };
86
+ }
56
87
 
57
- formatLocal(value, type = 'long', local = null) {
58
- if (local === null) {
59
- local = this.$r.lang
60
- }
61
- if (!(local in this.langs)) {
62
- console.warn('set local:' + local + ' with this.$dateTime.set_lang("' + local + '",{\n' +
63
- ' localizeName: "en-US",\n' +
64
- ' first_day:0\n' +
65
- ' })')
66
- this.langs[local] = {
67
- localizeName: local
68
- }
69
- }
70
- let opt = Object.assign({}, this.format[type], this.langs[local]);
71
- if (!opt.hour12) {
72
- opt.hour12 = false;
73
- }
74
- try {
75
- return new Intl.DateTimeFormat(this.langs[local].localizeName, opt).format(value);
76
- } catch (e) {
77
- console.error(e, value)
78
- return value
88
+ let opt = Object.assign({}, this.format[type], this.langs[local]);
89
+ if (!opt.hour12) {
90
+ opt.hour12 = false;
91
+ }
92
+ try {
93
+ return new Intl.DateTimeFormat(
94
+ this.langs[local].localizeName,
95
+ opt,
96
+ ).format(value);
97
+ } catch (e) {
98
+ console.error(e, value);
99
+ return value;
100
+ }
101
+ }
102
+
103
+ add_days(date, days) {
104
+ const newDate = new Date(date);
105
+ newDate.setDate(newDate.getDate() + days);
106
+ return newDate;
107
+ }
108
+
109
+ add_months(date, months) {
110
+ let currentDate = date;
111
+ for (let i = 0; i < months; i++) {
112
+ const formattedDate = this.formatLocal(currentDate, "date-latin");
113
+
114
+ let [month, day]=formattedDate.split("/");
115
+
116
+ let nextDate = currentDate;
117
+ let targetMonth = null;
118
+
119
+ while (true) {
120
+ nextDate = this.add_days(nextDate, 1);
121
+ const nextFormatted = this.formatLocal(nextDate, "date-latin");
122
+ let [month2, day2]= nextFormatted.split("/");
123
+
124
+ if (month2 !== month) {
125
+ if (targetMonth === null) {
126
+ targetMonth = month2;
127
+ }
128
+
129
+ if (day === day2) {
130
+ currentDate = nextDate;
131
+ break;
132
+ }
133
+
134
+ if (month2 !== targetMonth) {
135
+ currentDate = this.add_days(nextDate, -1);
136
+ break;
137
+ }
79
138
  }
80
- };
139
+ }
140
+ }
141
+ return currentDate;
142
+ }
81
143
  }
82
-