mybase 1.1.34 → 1.1.35
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/jest/isLANIp.test.js +36 -0
- package/jest/isLoopbackIP.test.js +31 -0
- package/jest.config.js +6 -0
- package/package.json +2 -5
- package/ts/funcs/Geoip2Paths.d.ts +7 -0
- package/ts/funcs/Geoip2Paths.js +26 -0
- package/ts/funcs/Geoip2Paths.test.ts +11 -0
- package/ts/funcs/Geoip2Paths.ts +26 -0
- package/ts/funcs/MaxRuntimeHours.d.ts +1 -0
- package/ts/funcs/MaxRuntimeHours.js +11 -0
- package/ts/funcs/MaxRuntimeHours.ts +7 -0
- package/ts/funcs/asJSON.d.ts +1 -0
- package/ts/funcs/asJSON.js +14 -0
- package/ts/funcs/asJSON.test.js +72 -0
- package/ts/funcs/asJSON.ts +6 -0
- package/ts/funcs/ensureFolder.d.ts +3 -0
- package/ts/funcs/ensureFolder.js +50 -0
- package/ts/funcs/ensureFolder.test.ts +79 -0
- package/ts/funcs/ensureFolder.ts +21 -0
- package/ts/funcs/fileCacheIsValid.d.ts +1 -0
- package/ts/funcs/fileCacheIsValid.js +40 -0
- package/ts/funcs/fileCacheIsValid.ts +13 -0
- package/ts/funcs/getMysql1.d.ts +8 -0
- package/ts/funcs/getMysql1.js +62 -0
- package/ts/funcs/getMysql1.ts +41 -0
- package/ts/funcs/getMysql2.d.ts +9 -0
- package/ts/funcs/getMysql2.js +62 -0
- package/ts/funcs/getMysql2.ts +42 -0
- package/ts/funcs/getWeekNumber.d.ts +1 -0
- package/ts/funcs/getWeekNumber.js +36 -0
- package/ts/funcs/getWeekNumber.test.ts +9 -0
- package/ts/funcs/getWeekNumber.ts +31 -0
- package/ts/funcs/hash_sha512.d.ts +1 -0
- package/ts/funcs/hash_sha512.js +11 -0
- package/ts/funcs/hash_sha512.ts +7 -0
- package/ts/funcs/initMysql2Pool.d.ts +3 -0
- package/ts/funcs/initMysql2Pool.js +39 -0
- package/ts/funcs/initMysql2Pool.ts +11 -0
- package/ts/funcs/int2ip.d.ts +1 -0
- package/ts/funcs/int2ip.js +18 -0
- package/ts/funcs/int2ip.test.js +33 -0
- package/ts/funcs/int2ip.ts +9 -0
- package/ts/funcs/ip2int.d.ts +1 -0
- package/ts/funcs/ip2int.js +18 -0
- package/ts/funcs/ip2int.test.js +30 -0
- package/ts/funcs/ip2int.ts +10 -0
- package/ts/funcs/isLANIp.d.ts +4 -0
- package/ts/funcs/isLANIp.js +47 -0
- package/ts/funcs/isLANIp.test.ts +59 -0
- package/ts/funcs/isLANIp.ts +37 -0
- package/ts/funcs/isLocal.d.ts +1 -0
- package/ts/funcs/isLocal.js +12 -0
- package/ts/funcs/isLocal.test.ts +16 -0
- package/ts/funcs/isLocal.ts +5 -0
- package/ts/funcs/isLoopbackIP.d.ts +4 -0
- package/ts/funcs/isLoopbackIP.js +42 -0
- package/ts/funcs/isLoopbackIP.test.ts +72 -0
- package/ts/funcs/isLoopbackIP.ts +32 -0
- package/ts/funcs/promiseTimeout.d.ts +1 -0
- package/ts/funcs/promiseTimeout.js +19 -0
- package/ts/funcs/promiseTimeout.test.js +104 -0
- package/ts/funcs/promiseTimeout.ts +19 -0
- package/ts/funcs/randomIP.d.ts +1 -0
- package/ts/funcs/randomIP.js +11 -0
- package/ts/funcs/randomIP.test.ts +16 -0
- package/ts/funcs/randomIP.ts +10 -0
- package/ts/funcs/randomIP6.d.ts +1 -0
- package/ts/funcs/randomIP6.js +17 -0
- package/ts/funcs/randomIP6.test.js +18 -0
- package/ts/funcs/randomIP6.ts +12 -0
- package/ts/funcs/randomString.d.ts +1 -0
- package/ts/funcs/randomString.js +8 -0
- package/ts/funcs/randomString.test.ts +15 -0
- package/ts/funcs/randomString.ts +5 -0
- package/ts/funcs/randomTCPPort.d.ts +12 -0
- package/ts/funcs/randomTCPPort.js +88 -0
- package/ts/funcs/randomTCPPort.test.ts +62 -0
- package/ts/funcs/randomTCPPort.ts +58 -0
- package/ts/funcs/randomUTFString.d.ts +1 -0
- package/ts/funcs/randomUTFString.js +31 -0
- package/ts/funcs/randomUTFString.test.ts +44 -0
- package/ts/funcs/randomUTFString.ts +33 -0
- package/ts/funcs/utcnow.d.ts +2 -0
- package/ts/funcs/utcnow.js +8 -0
- package/ts/funcs/utcnow.ts +5 -0
- package/ts/funcs/validEmail.d.ts +1 -0
- package/ts/funcs/validEmail.js +21 -0
- package/ts/funcs/validEmail.test.js +31 -0
- package/ts/funcs/validEmail.ts +13 -0
- package/ts/funcs/validIp.d.ts +1 -0
- package/ts/funcs/validIp.js +32 -0
- package/ts/funcs/validIp.test.ts +33 -0
- package/ts/funcs/validIp.ts +5 -0
- package/ts/funcs/vaultFill.d.ts +2 -0
- package/ts/funcs/vaultFill.js +86 -0
- package/ts/funcs/vaultFill.ts +89 -0
- package/ts/funcs/vaultRead.d.ts +2 -0
- package/ts/funcs/vaultRead.js +52 -0
- package/ts/funcs/vaultRead.ts +45 -0
- package/ts/funcs/wait.d.ts +1 -0
- package/ts/funcs/wait.js +10 -0
- package/ts/funcs/wait.ts +7 -0
- package/ts/global.d.ts +1 -0
- package/ts/global.js +16 -0
- package/ts/global.ts +10 -0
- package/ts/index.d.ts +30 -0
- package/ts/index.js +48 -0
- package/ts/index.test.ts +7 -0
- package/ts/index.ts +33 -0
- package/ts/models/IPAddress.d.ts +40 -0
- package/ts/models/IPAddress.js +254 -0
- package/ts/models/IPAddress.test.ts +393 -0
- package/ts/models/IPAddress.ts +271 -0
- package/ts/models/Timespan.d.ts +18 -0
- package/ts/models/Timespan.js +52 -0
- package/ts/models/Timespan.ts +63 -0
- package/ts/models/Unixtime.d.ts +32 -0
- package/ts/models/Unixtime.js +137 -0
- package/ts/models/Unixtime.test.ts +102 -0
- package/ts/models/Unixtime.ts +164 -0
- package/ts/types.d.ts +1 -0
- package/ts/types.js +3 -0
- package/ts/types.ts +1 -0
- package/tsconfig.json +36 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Timespan = void 0;
|
|
4
|
+
class Timespan {
|
|
5
|
+
constructor(ms) {
|
|
6
|
+
this.ms = ms;
|
|
7
|
+
}
|
|
8
|
+
get miliseconds() {
|
|
9
|
+
return this.ms;
|
|
10
|
+
}
|
|
11
|
+
get seconds() {
|
|
12
|
+
return this.ms / 1000;
|
|
13
|
+
}
|
|
14
|
+
get minutes() {
|
|
15
|
+
return this.ms / 1000 / 60;
|
|
16
|
+
}
|
|
17
|
+
get hours() {
|
|
18
|
+
return this.ms / 1000 / 60 / 60;
|
|
19
|
+
}
|
|
20
|
+
get days() {
|
|
21
|
+
return this.ms / 1000 / 60 / 60 / 24;
|
|
22
|
+
}
|
|
23
|
+
get weeks() {
|
|
24
|
+
return this.ms / 1000 / 60 / 60 / 24 / 7;
|
|
25
|
+
}
|
|
26
|
+
get months() {
|
|
27
|
+
return this.ms / 1000 / 60 / 60 / 24 / 30;
|
|
28
|
+
}
|
|
29
|
+
static miliseconds(amount) {
|
|
30
|
+
return new Timespan(amount);
|
|
31
|
+
}
|
|
32
|
+
static seconds(amount) {
|
|
33
|
+
return new Timespan(amount * 1000);
|
|
34
|
+
}
|
|
35
|
+
static minutes(mins) {
|
|
36
|
+
return new Timespan(mins * 60 * 1000);
|
|
37
|
+
}
|
|
38
|
+
static hours(hours) {
|
|
39
|
+
return new Timespan(hours * 60 * 60 * 1000);
|
|
40
|
+
}
|
|
41
|
+
static days(days) {
|
|
42
|
+
return new Timespan(days * 24 * 60 * 60 * 1000);
|
|
43
|
+
}
|
|
44
|
+
static weeks(weeks) {
|
|
45
|
+
return new Timespan(weeks * 7 * 24 * 60 * 60 * 1000);
|
|
46
|
+
}
|
|
47
|
+
static months(months) {
|
|
48
|
+
return new Timespan(months * 30 * 24 * 60 * 60 * 1000);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.Timespan = Timespan;
|
|
52
|
+
//# sourceMappingURL=Timespan.js.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export class Timespan {
|
|
2
|
+
constructor(private readonly ms: number){
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
get miliseconds() : number {
|
|
6
|
+
return this.ms
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
get seconds() : number {
|
|
10
|
+
return this.ms / 1000
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get minutes() : number {
|
|
14
|
+
return this.ms / 1000 / 60
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get hours() : number {
|
|
18
|
+
return this.ms / 1000 / 60 / 60
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get days() : number {
|
|
22
|
+
return this.ms / 1000 / 60 / 60 / 24
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get weeks() : number {
|
|
26
|
+
return this.ms / 1000 / 60 / 60 / 24 / 7
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get months() : number {
|
|
30
|
+
return this.ms / 1000 / 60 / 60 / 24 / 30
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static miliseconds(amount: number): Timespan {
|
|
34
|
+
return new Timespan(amount)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static seconds(amount: number): Timespan {
|
|
38
|
+
return new Timespan(amount * 1000)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static minutes(mins: number): Timespan {
|
|
42
|
+
return new Timespan(mins * 60 * 1000)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static hours(hours: number): Timespan {
|
|
46
|
+
return new Timespan(hours * 60 * 60 * 1000)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static days(days: number): Timespan {
|
|
50
|
+
return new Timespan(days * 24 * 60 * 60 * 1000)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
static weeks(weeks: number): Timespan {
|
|
54
|
+
return new Timespan(weeks * 7 * 24 * 60 * 60 * 1000)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static months(months: number): Timespan {
|
|
58
|
+
return new Timespan(months * 30 * 24 * 60 * 60 * 1000)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Timespan } from "./Timespan";
|
|
2
|
+
export declare class Unixtime {
|
|
3
|
+
private value;
|
|
4
|
+
static EMPTY: Unixtime;
|
|
5
|
+
static NOW(): Unixtime;
|
|
6
|
+
static now(): Unixtime;
|
|
7
|
+
greaterThan(other: Unixtime): boolean;
|
|
8
|
+
isEmpty(): boolean;
|
|
9
|
+
lessThan(other: Unixtime): boolean;
|
|
10
|
+
get elapsed(): Timespan;
|
|
11
|
+
constructor(value: Date | string | number);
|
|
12
|
+
addTimespan(timespan: Timespan): Unixtime;
|
|
13
|
+
subtractTimespan(timespan: Timespan): Unixtime;
|
|
14
|
+
addMiliseconds(miliseconds?: number): Unixtime;
|
|
15
|
+
addSeconds(seconds?: number): Unixtime;
|
|
16
|
+
addMinutes(minutes?: number): Unixtime;
|
|
17
|
+
addHours(hours?: number): Unixtime;
|
|
18
|
+
addDays(days?: number): Unixtime;
|
|
19
|
+
addMonths(months?: number): Unixtime;
|
|
20
|
+
addYears(years?: number): Unixtime;
|
|
21
|
+
yyyymmdd(): string;
|
|
22
|
+
yyymmddhh(): string;
|
|
23
|
+
getDay(): number;
|
|
24
|
+
static from(value: Date | string | number): Unixtime;
|
|
25
|
+
toShortUnixtime(): number;
|
|
26
|
+
toLongUnixtime(): number;
|
|
27
|
+
toDate(): Date;
|
|
28
|
+
toISOString(): string;
|
|
29
|
+
elapsedSeconds(): number;
|
|
30
|
+
elapsedMinutes(): number;
|
|
31
|
+
static try(value: Date | string | number): Unixtime | null;
|
|
32
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Unixtime = void 0;
|
|
4
|
+
const Timespan_1 = require("./Timespan");
|
|
5
|
+
// this will be a class to work with short and long version of unixtime format
|
|
6
|
+
class Unixtime {
|
|
7
|
+
static NOW() {
|
|
8
|
+
return new Unixtime(Date.now());
|
|
9
|
+
}
|
|
10
|
+
static now() {
|
|
11
|
+
return new Unixtime(Date.now());
|
|
12
|
+
}
|
|
13
|
+
greaterThan(other) {
|
|
14
|
+
return this.toLongUnixtime() > other.toLongUnixtime();
|
|
15
|
+
}
|
|
16
|
+
isEmpty() {
|
|
17
|
+
return this.toLongUnixtime() == 0;
|
|
18
|
+
}
|
|
19
|
+
lessThan(other) {
|
|
20
|
+
return this.toLongUnixtime() < other.toLongUnixtime();
|
|
21
|
+
}
|
|
22
|
+
get elapsed() {
|
|
23
|
+
return new Timespan_1.Timespan(Date.now() - this.toLongUnixtime());
|
|
24
|
+
}
|
|
25
|
+
constructor(value) {
|
|
26
|
+
if (typeof value === 'string') {
|
|
27
|
+
this.value = new Date(value);
|
|
28
|
+
}
|
|
29
|
+
else if (typeof value === 'number') {
|
|
30
|
+
// the number might be short or long form(from nodejs Date.now()
|
|
31
|
+
if (value < 10000000000)
|
|
32
|
+
value *= 1000;
|
|
33
|
+
else
|
|
34
|
+
value = Math.round(value);
|
|
35
|
+
// set the value
|
|
36
|
+
this.value = new Date(value);
|
|
37
|
+
}
|
|
38
|
+
else if (value instanceof Date) {
|
|
39
|
+
this.value = value;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
throw new Error('Invalid value');
|
|
43
|
+
}
|
|
44
|
+
if (isNaN(this.value.getTime())) {
|
|
45
|
+
throw new Error('Invalid date');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
addTimespan(timespan) {
|
|
49
|
+
this.value = new Date(this.value.getTime() + timespan.miliseconds);
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
subtractTimespan(timespan) {
|
|
53
|
+
this.value = new Date(this.value.getTime() - timespan.miliseconds);
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
addMiliseconds(miliseconds = 1) {
|
|
57
|
+
this.value = new Date(this.value.getTime() + miliseconds);
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
addSeconds(seconds = 1) {
|
|
61
|
+
this.value = new Date(this.value.getTime() + seconds * 1000);
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
addMinutes(minutes = 1) {
|
|
65
|
+
this.value = new Date(this.value.getTime() + minutes * 60000);
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
addHours(hours = 1) {
|
|
69
|
+
this.value = new Date(this.value.getTime() + hours * 3600000);
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
addDays(days = 1) {
|
|
73
|
+
this.value = new Date(this.value.getTime() + days * 86400000);
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
addMonths(months = 1) {
|
|
77
|
+
this.value.setMonth(this.value.getMonth() + months);
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
addYears(years = 1) {
|
|
81
|
+
this.value.setFullYear(this.value.getFullYear() + years);
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
yyyymmdd() {
|
|
85
|
+
let mm = this.value.getMonth() + 1;
|
|
86
|
+
let dd = this.value.getDate();
|
|
87
|
+
return [this.value.getFullYear(),
|
|
88
|
+
(mm > 9 ? '' : '0') + mm,
|
|
89
|
+
(dd > 9 ? '' : '0') + dd
|
|
90
|
+
].join('');
|
|
91
|
+
}
|
|
92
|
+
yyymmddhh() {
|
|
93
|
+
let mm = this.value.getMonth() + 1;
|
|
94
|
+
let dd = this.value.getDate();
|
|
95
|
+
let hh = this.value.getHours();
|
|
96
|
+
return [this.value.getFullYear(),
|
|
97
|
+
(mm > 9 ? '' : '0') + mm,
|
|
98
|
+
(dd > 9 ? '' : '0') + dd,
|
|
99
|
+
(hh > 9 ? '' : '0') + hh
|
|
100
|
+
].join('');
|
|
101
|
+
}
|
|
102
|
+
getDay() {
|
|
103
|
+
return this.value.getDay();
|
|
104
|
+
}
|
|
105
|
+
static from(value) {
|
|
106
|
+
return new Unixtime(value);
|
|
107
|
+
}
|
|
108
|
+
toShortUnixtime() {
|
|
109
|
+
return Math.round(this.toLongUnixtime() / 1000);
|
|
110
|
+
}
|
|
111
|
+
toLongUnixtime() {
|
|
112
|
+
return this.value.getTime();
|
|
113
|
+
}
|
|
114
|
+
toDate() {
|
|
115
|
+
return this.value;
|
|
116
|
+
}
|
|
117
|
+
toISOString() {
|
|
118
|
+
return this.value.toISOString();
|
|
119
|
+
}
|
|
120
|
+
elapsedSeconds() {
|
|
121
|
+
return Math.round((Date.now() - this.toLongUnixtime()) / 1000);
|
|
122
|
+
}
|
|
123
|
+
elapsedMinutes() {
|
|
124
|
+
return Math.round(this.elapsedSeconds() / 60);
|
|
125
|
+
}
|
|
126
|
+
static try(value) {
|
|
127
|
+
try {
|
|
128
|
+
return new Unixtime(value);
|
|
129
|
+
}
|
|
130
|
+
catch (e) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
exports.Unixtime = Unixtime;
|
|
136
|
+
Unixtime.EMPTY = new Unixtime(new Date(0));
|
|
137
|
+
//# sourceMappingURL=Unixtime.js.map
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Unixtime } from './Unixtime';
|
|
2
|
+
|
|
3
|
+
describe('Unixtime', () => {
|
|
4
|
+
it('should create Unixtime object from Date', () => {
|
|
5
|
+
const date = new Date();
|
|
6
|
+
const unixtime = new Unixtime(date);
|
|
7
|
+
expect(unixtime.toDate()).toEqual(date);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('create invalid date should throw error', () => {
|
|
11
|
+
expect(() => new Unixtime('invalid date')).toThrow();
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should create Unixtime object from string', () => {
|
|
15
|
+
const dateString = '2022-01-01T00:00:00Z';
|
|
16
|
+
const date = new Date(dateString);
|
|
17
|
+
const unixtime = new Unixtime(dateString);
|
|
18
|
+
expect(unixtime.toDate()).toEqual(date);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should create Unixtime object from number (short form)', () => {
|
|
22
|
+
const timestamp = 1640995200; // January 1, 2022 00:00:00 UTC
|
|
23
|
+
const date = new Date(timestamp * 1000);
|
|
24
|
+
const unixtime = new Unixtime(timestamp);
|
|
25
|
+
expect(unixtime.toDate()).toEqual(date);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('should create Unixtime object from number (long form)', () => {
|
|
29
|
+
const timestamp = 1640995200000; // January 1, 2022 00:00:00 UTC
|
|
30
|
+
const date = new Date(timestamp);
|
|
31
|
+
const unixtime = new Unixtime(timestamp);
|
|
32
|
+
expect(unixtime.toDate()).toEqual(date);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should convert Unixtime to short Unixtime', () => {
|
|
36
|
+
const date = new Date();
|
|
37
|
+
const unixtime = new Unixtime(date);
|
|
38
|
+
const shortUnixtime = Math.round(date.getTime() / 1000);
|
|
39
|
+
expect(unixtime.toShortUnixtime()).toEqual(shortUnixtime);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should convert Unixtime to long Unixtime', () => {
|
|
43
|
+
const date = new Date();
|
|
44
|
+
const unixtime = new Unixtime(date);
|
|
45
|
+
expect(unixtime.toLongUnixtime()).toEqual(date.getTime());
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should convert Unixtime to ISO string', () => {
|
|
49
|
+
const date = new Date();
|
|
50
|
+
const unixtime = new Unixtime(date);
|
|
51
|
+
expect(unixtime.toISOString()).toEqual(date.toISOString());
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should add miliseconds to Unixtime', () => {
|
|
55
|
+
const date = new Date();
|
|
56
|
+
const unixtime = new Unixtime(date);
|
|
57
|
+
const addedUnixtime = unixtime.addMiliseconds(1000);
|
|
58
|
+
expect(addedUnixtime.toDate()).toEqual(new Date(date.getTime() + 1000));
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should add seconds to Unixtime', () => {
|
|
62
|
+
const date = new Date();
|
|
63
|
+
const unixtime = new Unixtime(date);
|
|
64
|
+
const addedUnixtime = unixtime.addSeconds(1);
|
|
65
|
+
expect(addedUnixtime.toDate()).toEqual(new Date(date.getTime() + 1000));
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('should add minutes to Unixtime', () => {
|
|
69
|
+
const date = new Date();
|
|
70
|
+
const unixtime = new Unixtime(date);
|
|
71
|
+
const addedUnixtime = unixtime.addMinutes(1);
|
|
72
|
+
expect(addedUnixtime.toDate()).toEqual(new Date(date.getTime() + 60000));
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should add hours to Unixtime', () => {
|
|
76
|
+
const date = new Date();
|
|
77
|
+
const unixtime = new Unixtime(date);
|
|
78
|
+
const addedUnixtime = unixtime.addHours(1);
|
|
79
|
+
expect(addedUnixtime.toDate()).toEqual(new Date(date.getTime() + 3600000));
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('should add days to Unixtime', () => {
|
|
83
|
+
const date = new Date();
|
|
84
|
+
const unixtime = new Unixtime(date);
|
|
85
|
+
const addedUnixtime = unixtime.addDays(1);
|
|
86
|
+
expect(addedUnixtime.toDate()).toEqual(new Date(date.getTime() + 86400000));
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should add months to Unixtime', () => {
|
|
90
|
+
const date = new Date();
|
|
91
|
+
const unixtime = new Unixtime(date);
|
|
92
|
+
const addedUnixtime = unixtime.addMonths(1);
|
|
93
|
+
expect(addedUnixtime.toDate()).toEqual(new Date(date.setMonth(date.getMonth() + 1)));
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('should add years to Unixtime', () => {
|
|
97
|
+
const date = new Date();
|
|
98
|
+
const unixtime = new Unixtime(date);
|
|
99
|
+
const addedUnixtime = unixtime.addYears(1);
|
|
100
|
+
expect(addedUnixtime.toDate()).toEqual(new Date(date.setFullYear(date.getFullYear() + 1)));
|
|
101
|
+
});
|
|
102
|
+
});
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { Timespan } from "./Timespan"
|
|
2
|
+
|
|
3
|
+
// this will be a class to work with short and long version of unixtime format
|
|
4
|
+
export class Unixtime {
|
|
5
|
+
private value: Date
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
static EMPTY = new Unixtime(new Date(0))
|
|
9
|
+
|
|
10
|
+
static NOW() {
|
|
11
|
+
return new Unixtime(Date.now())
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static now() {
|
|
15
|
+
return new Unixtime(Date.now())
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
greaterThan(other: Unixtime) {
|
|
19
|
+
return this.toLongUnixtime() > other.toLongUnixtime()
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
isEmpty() {
|
|
23
|
+
return this.toLongUnixtime() == 0
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
lessThan(other: Unixtime) {
|
|
27
|
+
return this.toLongUnixtime() < other.toLongUnixtime()
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get elapsed():Timespan {
|
|
31
|
+
return new Timespan(Date.now() - this.toLongUnixtime())
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
constructor(value: Date | string | number) {
|
|
36
|
+
if (typeof value === 'string') {
|
|
37
|
+
this.value = new Date(value)
|
|
38
|
+
} else if (typeof value === 'number') {
|
|
39
|
+
// the number might be short or long form(from nodejs Date.now()
|
|
40
|
+
if (value < 10000000000) value *= 1000
|
|
41
|
+
else
|
|
42
|
+
value = Math.round(value)
|
|
43
|
+
// set the value
|
|
44
|
+
this.value = new Date(value)
|
|
45
|
+
}
|
|
46
|
+
else if (value instanceof Date) {
|
|
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')
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public addTimespan(timespan: Timespan): Unixtime {
|
|
59
|
+
this.value = new Date(this.value.getTime() + timespan.miliseconds)
|
|
60
|
+
return this
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public subtractTimespan(timespan: Timespan): Unixtime {
|
|
64
|
+
this.value = new Date(this.value.getTime() - timespan.miliseconds)
|
|
65
|
+
return this
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public addMiliseconds(miliseconds: number = 1): Unixtime {
|
|
69
|
+
this.value = new Date(this.value.getTime() + miliseconds)
|
|
70
|
+
return this
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public addSeconds(seconds: number = 1): Unixtime {
|
|
74
|
+
this.value = new Date(this.value.getTime() + seconds * 1000)
|
|
75
|
+
return this
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public addMinutes(minutes: number = 1): Unixtime {
|
|
79
|
+
this.value = new Date(this.value.getTime() + minutes * 60000)
|
|
80
|
+
return this
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public addHours(hours: number = 1): Unixtime {
|
|
84
|
+
this.value = new Date(this.value.getTime() + hours * 3600000)
|
|
85
|
+
return this
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
public addDays(days: number = 1): Unixtime {
|
|
89
|
+
this.value = new Date(this.value.getTime() + days * 86400000)
|
|
90
|
+
return this
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public addMonths(months: number = 1): Unixtime {
|
|
94
|
+
this.value.setMonth(this.value.getMonth() + months)
|
|
95
|
+
return this
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public addYears(years: number = 1): Unixtime {
|
|
99
|
+
this.value.setFullYear(this.value.getFullYear() + years)
|
|
100
|
+
return this
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public yyyymmdd(): string {
|
|
104
|
+
let mm = this.value.getMonth() + 1
|
|
105
|
+
let dd = this.value.getDate()
|
|
106
|
+
return [this.value.getFullYear(),
|
|
107
|
+
(mm > 9 ? '' : '0') + mm,
|
|
108
|
+
(dd > 9 ? '' : '0') + dd
|
|
109
|
+
].join('')
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
public yyymmddhh(): string {
|
|
113
|
+
let mm = this.value.getMonth() + 1
|
|
114
|
+
let dd = this.value.getDate()
|
|
115
|
+
let hh = this.value.getHours()
|
|
116
|
+
return [this.value.getFullYear(),
|
|
117
|
+
(mm > 9 ? '' : '0') + mm,
|
|
118
|
+
(dd > 9 ? '' : '0') + dd,
|
|
119
|
+
(hh > 9 ? '' : '0') + hh
|
|
120
|
+
].join('')
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
public getDay() : number {
|
|
125
|
+
return this.value.getDay()
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
static from(value: Date | string | number) {
|
|
129
|
+
return new Unixtime(value)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public toShortUnixtime(): number {
|
|
133
|
+
return Math.round(this.toLongUnixtime() / 1000)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
public toLongUnixtime(): number {
|
|
137
|
+
return this.value.getTime()
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public toDate(): Date {
|
|
141
|
+
return this.value
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
public toISOString(): string {
|
|
145
|
+
return this.value.toISOString()
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public elapsedSeconds(): number {
|
|
149
|
+
return Math.round((Date.now() - this.toLongUnixtime()) / 1000)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public elapsedMinutes(): number {
|
|
153
|
+
return Math.round(this.elapsedSeconds() / 60)
|
|
154
|
+
}
|
|
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
|
+
}
|
|
164
|
+
}
|
package/ts/types.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type UnixtimeShort = number;
|
package/ts/types.js
ADDED
package/ts/types.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type UnixtimeShort = number
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2017",
|
|
4
|
+
"experimentalDecorators": true,
|
|
5
|
+
"emitDecoratorMetadata": true,
|
|
6
|
+
"module": "commonjs",
|
|
7
|
+
"moduleResolution" : "Node",
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"forceConsistentCasingInFileNames": true,
|
|
10
|
+
"strict": true,
|
|
11
|
+
"declaration": true, // Generates corresponding .d.ts file
|
|
12
|
+
"noImplicitReturns": true, // Report error when not all code paths in function return a value
|
|
13
|
+
"noUnusedLocals": true, // Report errors on unused locals
|
|
14
|
+
// "noUnusedParameters": true, // Report errors on unused parameters
|
|
15
|
+
"noFallthroughCasesInSwitch": true, // Report errors for fallthrough cases in switch statement
|
|
16
|
+
// "noUncheckedIndexedAccess": true, // Include 'undefined' in index signature results
|
|
17
|
+
"noImplicitAny": true, // Raise error on expressions and declarations with an implied 'any' type
|
|
18
|
+
"strictNullChecks": true, // Enable strict null checking
|
|
19
|
+
"strictFunctionTypes": true, // Ensure function parameter types match
|
|
20
|
+
"strictBindCallApply": true, // Ensure 'bind', 'call', and 'apply' methods match function types
|
|
21
|
+
"strictPropertyInitialization": true, // Ensure non-undefined class properties are initialized in the constructor
|
|
22
|
+
"alwaysStrict": true, // Parse in strict mode and emit "use strict" for each source file
|
|
23
|
+
"sourceMap": true,
|
|
24
|
+
"skipLibCheck": true,
|
|
25
|
+
"baseUrl": ".",
|
|
26
|
+
"paths": {
|
|
27
|
+
"@root/*": ["./*"],
|
|
28
|
+
"@models/*": ["./models/*"],
|
|
29
|
+
"@inc/*": ["./inc/ts/*"]
|
|
30
|
+
},
|
|
31
|
+
"typeRoots": ["./node_modules/@types", "./types/"]
|
|
32
|
+
},
|
|
33
|
+
"exclude": [
|
|
34
|
+
"**/*.test.ts"
|
|
35
|
+
]
|
|
36
|
+
}
|