mybase 1.1.22 → 1.1.23
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.
- package/package.json +1 -1
- package/ts/models/Unixtime.js +18 -1
- package/ts/models/Unixtime.test.ts +4 -0
- package/ts/models/Unixtime.ts +21 -1
package/package.json
CHANGED
package/ts/models/Unixtime.js
CHANGED
|
@@ -35,9 +35,15 @@ class Unixtime {
|
|
|
35
35
|
// set the value
|
|
36
36
|
this.value = new Date(value);
|
|
37
37
|
}
|
|
38
|
-
else {
|
|
38
|
+
else if (value instanceof Date) {
|
|
39
39
|
this.value = value;
|
|
40
40
|
}
|
|
41
|
+
else {
|
|
42
|
+
throw new Error('Invalid value');
|
|
43
|
+
}
|
|
44
|
+
if (isNaN(this.value.getTime())) {
|
|
45
|
+
throw new Error('Invalid date');
|
|
46
|
+
}
|
|
41
47
|
}
|
|
42
48
|
addTimespan(timespan) {
|
|
43
49
|
this.value = new Date(this.value.getTime() + timespan.miliseconds);
|
|
@@ -93,6 +99,9 @@ class Unixtime {
|
|
|
93
99
|
(hh > 9 ? '' : '0') + hh
|
|
94
100
|
].join('');
|
|
95
101
|
}
|
|
102
|
+
getDay() {
|
|
103
|
+
return this.value.getDay();
|
|
104
|
+
}
|
|
96
105
|
static from(value) {
|
|
97
106
|
return new Unixtime(value);
|
|
98
107
|
}
|
|
@@ -114,6 +123,14 @@ class Unixtime {
|
|
|
114
123
|
elapsedMinutes() {
|
|
115
124
|
return Math.round(this.elapsedSeconds() / 60);
|
|
116
125
|
}
|
|
126
|
+
static try(value) {
|
|
127
|
+
try {
|
|
128
|
+
return new Unixtime(value);
|
|
129
|
+
}
|
|
130
|
+
catch (e) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
117
134
|
}
|
|
118
135
|
exports.Unixtime = Unixtime;
|
|
119
136
|
Unixtime.EMPTY = new Unixtime(new Date(0));
|
|
@@ -7,6 +7,10 @@ describe('Unixtime', () => {
|
|
|
7
7
|
expect(unixtime.toDate()).toEqual(date);
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
+
it('create invalid date should throw error', () => {
|
|
11
|
+
expect(() => new Unixtime('invalid date')).toThrow();
|
|
12
|
+
})
|
|
13
|
+
|
|
10
14
|
it('should create Unixtime object from string', () => {
|
|
11
15
|
const dateString = '2022-01-01T00:00:00Z';
|
|
12
16
|
const date = new Date(dateString);
|
package/ts/models/Unixtime.ts
CHANGED
|
@@ -42,8 +42,16 @@ export class Unixtime {
|
|
|
42
42
|
value = Math.round(value)
|
|
43
43
|
// set the value
|
|
44
44
|
this.value = new Date(value)
|
|
45
|
-
}
|
|
45
|
+
}
|
|
46
|
+
else if (value instanceof Date) {
|
|
46
47
|
this.value = value
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
throw new Error('Invalid value')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (isNaN(this.value.getTime())) {
|
|
54
|
+
throw new Error('Invalid date')
|
|
47
55
|
}
|
|
48
56
|
}
|
|
49
57
|
|
|
@@ -113,6 +121,10 @@ export class Unixtime {
|
|
|
113
121
|
}
|
|
114
122
|
|
|
115
123
|
|
|
124
|
+
public getDay() : number {
|
|
125
|
+
return this.value.getDay()
|
|
126
|
+
}
|
|
127
|
+
|
|
116
128
|
static from(value: Date | string | number) {
|
|
117
129
|
return new Unixtime(value)
|
|
118
130
|
}
|
|
@@ -141,4 +153,12 @@ export class Unixtime {
|
|
|
141
153
|
return Math.round(this.elapsedSeconds() / 60)
|
|
142
154
|
}
|
|
143
155
|
|
|
156
|
+
|
|
157
|
+
static try(value: Date | string | number): Unixtime | null {
|
|
158
|
+
try {
|
|
159
|
+
return new Unixtime(value)
|
|
160
|
+
} catch (e) {
|
|
161
|
+
return null
|
|
162
|
+
}
|
|
163
|
+
}
|
|
144
164
|
}
|