mybase 1.2.2 → 2.0.0
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/dist/funcs/Geoip2Paths.js.map +1 -0
- package/dist/funcs/MaxRuntimeHours.js.map +1 -0
- package/dist/funcs/asJSON.js.map +1 -0
- package/dist/funcs/deepCopy.js.map +1 -0
- package/dist/funcs/ensureFolder.js.map +1 -0
- package/dist/funcs/fileCacheIsValid.js.map +1 -0
- package/dist/funcs/getMysql1.js.map +1 -0
- package/dist/funcs/getMysql2.js.map +1 -0
- package/dist/funcs/getWeekNumber.js.map +1 -0
- package/dist/funcs/hash_sha512.js.map +1 -0
- package/dist/funcs/initMysql2Pool.js.map +1 -0
- package/dist/funcs/int2ip.js.map +1 -0
- package/dist/funcs/ip2int.js.map +1 -0
- package/dist/funcs/isLANIp.d.ts +2 -0
- package/{ts → dist}/funcs/isLANIp.js +6 -6
- package/dist/funcs/isLANIp.js.map +1 -0
- package/dist/funcs/isLocal.js.map +1 -0
- package/dist/funcs/isLoopbackIP.d.ts +2 -0
- package/{ts → dist}/funcs/isLoopbackIP.js +3 -3
- package/dist/funcs/isLoopbackIP.js.map +1 -0
- package/dist/funcs/knexConnection.js.map +1 -0
- package/dist/funcs/promiseTimeout.js.map +1 -0
- package/dist/funcs/randomIP.js.map +1 -0
- package/dist/funcs/randomIP6.js.map +1 -0
- package/dist/funcs/randomString.js.map +1 -0
- package/dist/funcs/randomTCPPort.js.map +1 -0
- package/dist/funcs/randomUTFString.js.map +1 -0
- package/dist/funcs/utcnow.js.map +1 -0
- package/dist/funcs/validEmail.js.map +1 -0
- package/dist/funcs/validIp.js.map +1 -0
- package/dist/funcs/vaultFill.js.map +1 -0
- package/dist/funcs/vaultRead.js.map +1 -0
- package/dist/funcs/wait.js.map +1 -0
- package/dist/global.js.map +1 -0
- package/dist/index.d.ts +38 -0
- package/{ts → dist}/index.js +35 -27
- package/dist/index.js.map +1 -0
- package/dist/ip6addr.d.ts +69 -0
- package/dist/ip6addr.js +752 -0
- package/dist/ip6addr.js.map +1 -0
- package/dist/models/DateIterator.js.map +1 -0
- package/{ts → dist}/models/IPAddress.d.ts +2 -1
- package/{ts → dist}/models/IPAddress.js +11 -12
- package/dist/models/IPAddress.js.map +1 -0
- package/dist/models/Interfaces.js.map +1 -0
- package/dist/models/OTPGenerator.js.map +1 -0
- package/dist/models/Timespan.js.map +1 -0
- package/dist/models/Unixtime.js.map +1 -0
- package/{mybase.d.ts → dist/mybase.d.ts} +1 -20
- package/{mybase.js → dist/mybase.js} +1 -56
- package/dist/mybase.js.map +1 -0
- package/{ts → dist}/types.d.ts +10 -0
- package/dist/types.js.map +1 -0
- package/package.json +22 -9
- package/ip6addr.d.ts +0 -61
- package/jest/isLANIp.test.js +0 -36
- package/jest/isLoopbackIP.test.js +0 -31
- package/jest.config.js +0 -13
- package/mybase.test.ts +0 -647
- package/mybase.ts +0 -397
- package/ts/funcs/Geoip2Paths.test.ts +0 -11
- package/ts/funcs/Geoip2Paths.ts +0 -28
- package/ts/funcs/MaxRuntimeHours.ts +0 -7
- package/ts/funcs/asJSON.test.js +0 -72
- package/ts/funcs/asJSON.ts +0 -9
- package/ts/funcs/deepCopy.test.ts +0 -108
- package/ts/funcs/deepCopy.ts +0 -34
- package/ts/funcs/ensureFolder.test.ts +0 -79
- package/ts/funcs/ensureFolder.ts +0 -21
- package/ts/funcs/fileCacheIsValid.ts +0 -13
- package/ts/funcs/getMysql1.ts +0 -41
- package/ts/funcs/getMysql2.ts +0 -42
- package/ts/funcs/getWeekNumber.test.ts +0 -9
- package/ts/funcs/getWeekNumber.ts +0 -31
- package/ts/funcs/hash_sha512.ts +0 -6
- package/ts/funcs/initMysql2Pool.ts +0 -11
- package/ts/funcs/int2ip.test.js +0 -33
- package/ts/funcs/int2ip.ts +0 -9
- package/ts/funcs/ip2int.test.js +0 -30
- package/ts/funcs/ip2int.ts +0 -10
- package/ts/funcs/isLANIp.d.ts +0 -2
- package/ts/funcs/isLANIp.test.ts +0 -58
- package/ts/funcs/isLANIp.ts +0 -34
- package/ts/funcs/isLocal.test.ts +0 -16
- package/ts/funcs/isLocal.ts +0 -5
- package/ts/funcs/isLoopbackIP.d.ts +0 -2
- package/ts/funcs/isLoopbackIP.test.ts +0 -72
- package/ts/funcs/isLoopbackIP.ts +0 -30
- package/ts/funcs/knexConnection.ts +0 -42
- package/ts/funcs/promiseTimeout.test.js +0 -104
- package/ts/funcs/promiseTimeout.ts +0 -19
- package/ts/funcs/randomIP.test.ts +0 -16
- package/ts/funcs/randomIP.ts +0 -10
- package/ts/funcs/randomIP6.test.js +0 -18
- package/ts/funcs/randomIP6.ts +0 -12
- package/ts/funcs/randomString.test.ts +0 -15
- package/ts/funcs/randomString.ts +0 -5
- package/ts/funcs/randomTCPPort.test.ts +0 -62
- package/ts/funcs/randomTCPPort.ts +0 -58
- package/ts/funcs/randomUTFString.test.ts +0 -44
- package/ts/funcs/randomUTFString.ts +0 -33
- package/ts/funcs/utcnow.ts +0 -5
- package/ts/funcs/validEmail.test.js +0 -31
- package/ts/funcs/validEmail.ts +0 -11
- package/ts/funcs/validIp.test.ts +0 -33
- package/ts/funcs/validIp.ts +0 -5
- package/ts/funcs/vaultFill.ts +0 -89
- package/ts/funcs/vaultRead.ts +0 -50
- package/ts/funcs/wait.ts +0 -7
- package/ts/global.ts +0 -10
- package/ts/index.d.ts +0 -35
- package/ts/index.test.ts +0 -7
- package/ts/index.ts +0 -39
- package/ts/models/DateIterator.test.ts +0 -149
- package/ts/models/DateIterator.ts +0 -80
- package/ts/models/IPAddress.test.ts +0 -393
- package/ts/models/IPAddress.ts +0 -272
- package/ts/models/Interfaces.ts +0 -58
- package/ts/models/OTPGenerator.test.ts +0 -42
- package/ts/models/OTPGenerator.ts +0 -43
- package/ts/models/Timespan.ts +0 -80
- package/ts/models/Unixtime.test.ts +0 -168
- package/ts/models/Unixtime.ts +0 -208
- package/ts/types.ts +0 -42
- package/tsconfig.jest.json +0 -11
- package/tsconfig.json +0 -37
- package/types/third-party.d.ts +0 -21
- /package/{ts → dist}/funcs/Geoip2Paths.d.ts +0 -0
- /package/{ts → dist}/funcs/Geoip2Paths.js +0 -0
- /package/{ts → dist}/funcs/MaxRuntimeHours.d.ts +0 -0
- /package/{ts → dist}/funcs/MaxRuntimeHours.js +0 -0
- /package/{ts → dist}/funcs/asJSON.d.ts +0 -0
- /package/{ts → dist}/funcs/asJSON.js +0 -0
- /package/{ts → dist}/funcs/deepCopy.d.ts +0 -0
- /package/{ts → dist}/funcs/deepCopy.js +0 -0
- /package/{ts → dist}/funcs/ensureFolder.d.ts +0 -0
- /package/{ts → dist}/funcs/ensureFolder.js +0 -0
- /package/{ts → dist}/funcs/fileCacheIsValid.d.ts +0 -0
- /package/{ts → dist}/funcs/fileCacheIsValid.js +0 -0
- /package/{ts → dist}/funcs/getMysql1.d.ts +0 -0
- /package/{ts → dist}/funcs/getMysql1.js +0 -0
- /package/{ts → dist}/funcs/getMysql2.d.ts +0 -0
- /package/{ts → dist}/funcs/getMysql2.js +0 -0
- /package/{ts → dist}/funcs/getWeekNumber.d.ts +0 -0
- /package/{ts → dist}/funcs/getWeekNumber.js +0 -0
- /package/{ts → dist}/funcs/hash_sha512.d.ts +0 -0
- /package/{ts → dist}/funcs/hash_sha512.js +0 -0
- /package/{ts → dist}/funcs/initMysql2Pool.d.ts +0 -0
- /package/{ts → dist}/funcs/initMysql2Pool.js +0 -0
- /package/{ts → dist}/funcs/int2ip.d.ts +0 -0
- /package/{ts → dist}/funcs/int2ip.js +0 -0
- /package/{ts → dist}/funcs/ip2int.d.ts +0 -0
- /package/{ts → dist}/funcs/ip2int.js +0 -0
- /package/{ts → dist}/funcs/isLocal.d.ts +0 -0
- /package/{ts → dist}/funcs/isLocal.js +0 -0
- /package/{ts → dist}/funcs/knexConnection.d.ts +0 -0
- /package/{ts → dist}/funcs/knexConnection.js +0 -0
- /package/{ts → dist}/funcs/promiseTimeout.d.ts +0 -0
- /package/{ts → dist}/funcs/promiseTimeout.js +0 -0
- /package/{ts → dist}/funcs/randomIP.d.ts +0 -0
- /package/{ts → dist}/funcs/randomIP.js +0 -0
- /package/{ts → dist}/funcs/randomIP6.d.ts +0 -0
- /package/{ts → dist}/funcs/randomIP6.js +0 -0
- /package/{ts → dist}/funcs/randomString.d.ts +0 -0
- /package/{ts → dist}/funcs/randomString.js +0 -0
- /package/{ts → dist}/funcs/randomTCPPort.d.ts +0 -0
- /package/{ts → dist}/funcs/randomTCPPort.js +0 -0
- /package/{ts → dist}/funcs/randomUTFString.d.ts +0 -0
- /package/{ts → dist}/funcs/randomUTFString.js +0 -0
- /package/{ts → dist}/funcs/utcnow.d.ts +0 -0
- /package/{ts → dist}/funcs/utcnow.js +0 -0
- /package/{ts → dist}/funcs/validEmail.d.ts +0 -0
- /package/{ts → dist}/funcs/validEmail.js +0 -0
- /package/{ts → dist}/funcs/validIp.d.ts +0 -0
- /package/{ts → dist}/funcs/validIp.js +0 -0
- /package/{ts → dist}/funcs/vaultFill.d.ts +0 -0
- /package/{ts → dist}/funcs/vaultFill.js +0 -0
- /package/{ts → dist}/funcs/vaultRead.d.ts +0 -0
- /package/{ts → dist}/funcs/vaultRead.js +0 -0
- /package/{ts → dist}/funcs/wait.d.ts +0 -0
- /package/{ts → dist}/funcs/wait.js +0 -0
- /package/{ts → dist}/global.d.ts +0 -0
- /package/{ts → dist}/global.js +0 -0
- /package/{ts → dist}/models/DateIterator.d.ts +0 -0
- /package/{ts → dist}/models/DateIterator.js +0 -0
- /package/{ts → dist}/models/Interfaces.d.ts +0 -0
- /package/{ts → dist}/models/Interfaces.js +0 -0
- /package/{ts → dist}/models/OTPGenerator.d.ts +0 -0
- /package/{ts → dist}/models/OTPGenerator.js +0 -0
- /package/{ts → dist}/models/Timespan.d.ts +0 -0
- /package/{ts → dist}/models/Timespan.js +0 -0
- /package/{ts → dist}/models/Unixtime.d.ts +0 -0
- /package/{ts → dist}/models/Unixtime.js +0 -0
- /package/{ts → dist}/types.js +0 -0
package/ts/index.d.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
export * from "./funcs/getWeekNumber";
|
|
2
|
-
export * from "./funcs/Geoip2Paths";
|
|
3
|
-
export * from "./funcs/isLocal";
|
|
4
|
-
export * from "./funcs/randomString";
|
|
5
|
-
export * from "./funcs/utcnow";
|
|
6
|
-
export * from "./funcs/validIp";
|
|
7
|
-
export * from "./funcs/wait";
|
|
8
|
-
export * from "./funcs/fileCacheIsValid";
|
|
9
|
-
export * from "./funcs/hash_sha512";
|
|
10
|
-
export * from "./funcs/vaultRead";
|
|
11
|
-
export * from "./funcs/vaultFill";
|
|
12
|
-
export * from "./funcs/asJSON";
|
|
13
|
-
export * from "./funcs/promiseTimeout";
|
|
14
|
-
export * from "./funcs/randomIP";
|
|
15
|
-
export * from "./funcs/ip2int";
|
|
16
|
-
export * from "./funcs/int2ip";
|
|
17
|
-
export * from "./funcs/randomIP6";
|
|
18
|
-
export * from "./funcs/randomTCPPort";
|
|
19
|
-
export * from "./funcs/validEmail";
|
|
20
|
-
export * from "./funcs/isLoopbackIP";
|
|
21
|
-
export * from "./funcs/isLANIp";
|
|
22
|
-
export * from "./funcs/getMysql1";
|
|
23
|
-
export * from "./funcs/getMysql2";
|
|
24
|
-
export * from "./funcs/initMysql2Pool";
|
|
25
|
-
export * from "./funcs/randomUTFString";
|
|
26
|
-
export * from "./funcs/MaxRuntimeHours";
|
|
27
|
-
export * from "./funcs/ensureFolder";
|
|
28
|
-
export * from "./funcs/knexConnection";
|
|
29
|
-
export * from "./funcs/deepCopy";
|
|
30
|
-
export * from "./models/Unixtime";
|
|
31
|
-
export * from "./models/Timespan";
|
|
32
|
-
export * from "./models/IPAddress";
|
|
33
|
-
export * from "./models/OTPGenerator";
|
|
34
|
-
export * from "./models/Interfaces";
|
|
35
|
-
export * from "./models/DateIterator";
|
package/ts/index.test.ts
DELETED
package/ts/index.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
export * from "./funcs/getWeekNumber"
|
|
2
|
-
export * from "./funcs/Geoip2Paths";
|
|
3
|
-
export * from "./funcs/isLocal"
|
|
4
|
-
export * from "./funcs/randomString"
|
|
5
|
-
export * from "./funcs/utcnow"
|
|
6
|
-
export * from "./funcs/validIp"
|
|
7
|
-
export * from "./funcs/wait"
|
|
8
|
-
export * from "./funcs/fileCacheIsValid"
|
|
9
|
-
export * from "./funcs/hash_sha512"
|
|
10
|
-
export * from "./funcs/vaultRead"
|
|
11
|
-
export * from "./funcs/vaultFill"
|
|
12
|
-
export * from "./funcs/asJSON"
|
|
13
|
-
export * from "./funcs/promiseTimeout"
|
|
14
|
-
export * from "./funcs/randomIP"
|
|
15
|
-
export * from "./funcs/ip2int"
|
|
16
|
-
export * from "./funcs/int2ip"
|
|
17
|
-
export * from "./funcs/randomIP6"
|
|
18
|
-
export * from "./funcs/randomTCPPort"
|
|
19
|
-
export * from "./funcs/validEmail"
|
|
20
|
-
export * from "./funcs/isLoopbackIP"
|
|
21
|
-
export * from "./funcs/isLANIp"
|
|
22
|
-
export * from "./funcs/getMysql1"
|
|
23
|
-
export * from "./funcs/getMysql2"
|
|
24
|
-
export * from "./funcs/initMysql2Pool"
|
|
25
|
-
export * from "./funcs/randomUTFString"
|
|
26
|
-
export * from "./funcs/MaxRuntimeHours"
|
|
27
|
-
export * from "./funcs/ensureFolder"
|
|
28
|
-
export * from "./funcs/knexConnection"
|
|
29
|
-
export * from "./funcs/deepCopy"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
// models
|
|
34
|
-
export * from "./models/Unixtime"
|
|
35
|
-
export * from "./models/Timespan"
|
|
36
|
-
export * from "./models/IPAddress"
|
|
37
|
-
export * from "./models/OTPGenerator"
|
|
38
|
-
export * from "./models/Interfaces"
|
|
39
|
-
export * from "./models/DateIterator"
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { DateIterator } from './DateIterator';
|
|
2
|
-
import { Unixtime } from './Unixtime';
|
|
3
|
-
|
|
4
|
-
describe('DateIterator', () => {
|
|
5
|
-
const start = () => Unixtime.fromYYYYMMDD('20240110');
|
|
6
|
-
const end = () => Unixtime.fromYYYYMMDD('20240105');
|
|
7
|
-
|
|
8
|
-
it('should iterate backwards day-by-day (end-exclusive) with default separator', () => {
|
|
9
|
-
const it = new DateIterator(start(), end());
|
|
10
|
-
expect(it.toArray()).toEqual([
|
|
11
|
-
'20240110',
|
|
12
|
-
'20240109',
|
|
13
|
-
'20240108',
|
|
14
|
-
'20240107',
|
|
15
|
-
'20240106',
|
|
16
|
-
]);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it('should honor a custom separator', () => {
|
|
20
|
-
const it = new DateIterator(start(), end(), '-');
|
|
21
|
-
expect(it.toArray()).toEqual([
|
|
22
|
-
'2024-01-10',
|
|
23
|
-
'2024-01-09',
|
|
24
|
-
'2024-01-08',
|
|
25
|
-
'2024-01-07',
|
|
26
|
-
'2024-01-06',
|
|
27
|
-
]);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should honor a slash separator', () => {
|
|
31
|
-
const it = new DateIterator(start(), end(), '/');
|
|
32
|
-
expect(it.toArray()).toEqual([
|
|
33
|
-
'2024/01/10',
|
|
34
|
-
'2024/01/09',
|
|
35
|
-
'2024/01/08',
|
|
36
|
-
'2024/01/07',
|
|
37
|
-
'2024/01/06',
|
|
38
|
-
]);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('next() should return null when exhausted', () => {
|
|
42
|
-
const s = Unixtime.fromYYYYMMDD('20240103');
|
|
43
|
-
const e = Unixtime.fromYYYYMMDD('20240101');
|
|
44
|
-
const it = new DateIterator(s, e);
|
|
45
|
-
expect(it.next()).toEqual('20240103');
|
|
46
|
-
expect(it.next()).toEqual('20240102');
|
|
47
|
-
expect(it.next()).toBeNull();
|
|
48
|
-
expect(it.next()).toBeNull();
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('should return empty array when start equals end (end-exclusive)', () => {
|
|
52
|
-
const s = Unixtime.fromYYYYMMDD('20240105');
|
|
53
|
-
const e = Unixtime.fromYYYYMMDD('20240105');
|
|
54
|
-
const it = new DateIterator(s, e);
|
|
55
|
-
expect(it.toArray()).toEqual([]);
|
|
56
|
-
expect(it.next()).toBeNull();
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('should throw when start is older than end', () => {
|
|
60
|
-
const s = Unixtime.fromYYYYMMDD('20240101');
|
|
61
|
-
const e = Unixtime.fromYYYYMMDD('20240110');
|
|
62
|
-
expect(() => new DateIterator(s, e)).toThrow(
|
|
63
|
-
'DateIterator: `start` must be newer than or equal to `end`'
|
|
64
|
-
);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('should not mutate the caller-supplied Unixtime instances', () => {
|
|
68
|
-
const s = Unixtime.fromYYYYMMDD('20240110');
|
|
69
|
-
const e = Unixtime.fromYYYYMMDD('20240105');
|
|
70
|
-
const sBefore = s.toLongUnixtime();
|
|
71
|
-
const eBefore = e.toLongUnixtime();
|
|
72
|
-
const it = new DateIterator(s, e);
|
|
73
|
-
it.toArray();
|
|
74
|
-
expect(s.toLongUnixtime()).toEqual(sBefore);
|
|
75
|
-
expect(e.toLongUnixtime()).toEqual(eBefore);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it('reset() should allow the iterator to be consumed again', () => {
|
|
79
|
-
const it = new DateIterator(start(), end());
|
|
80
|
-
const first = it.toArray();
|
|
81
|
-
expect(it.next()).toBeNull();
|
|
82
|
-
it.reset();
|
|
83
|
-
const second = it.toArray();
|
|
84
|
-
expect(second).toEqual(first);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it('reset() should return the iterator for chaining', () => {
|
|
88
|
-
const it = new DateIterator(start(), end());
|
|
89
|
-
expect(it.reset()).toBe(it);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('should be iterable via for..of', () => {
|
|
93
|
-
const it = new DateIterator(start(), end(), '-');
|
|
94
|
-
const out: string[] = [];
|
|
95
|
-
for (const d of it) out.push(d);
|
|
96
|
-
expect(out).toEqual([
|
|
97
|
-
'2024-01-10',
|
|
98
|
-
'2024-01-09',
|
|
99
|
-
'2024-01-08',
|
|
100
|
-
'2024-01-07',
|
|
101
|
-
'2024-01-06',
|
|
102
|
-
]);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it('for..of should be independent of next()/toArray() cursor', () => {
|
|
106
|
-
const it = new DateIterator(start(), end());
|
|
107
|
-
expect(it.next()).toEqual('20240110');
|
|
108
|
-
const collected: string[] = [];
|
|
109
|
-
for (const d of it) collected.push(d);
|
|
110
|
-
expect(collected).toEqual([
|
|
111
|
-
'20240110',
|
|
112
|
-
'20240109',
|
|
113
|
-
'20240108',
|
|
114
|
-
'20240107',
|
|
115
|
-
'20240106',
|
|
116
|
-
]);
|
|
117
|
-
expect(it.next()).toEqual('20240109');
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('should support spread syntax', () => {
|
|
121
|
-
const it = new DateIterator(
|
|
122
|
-
Unixtime.fromYYYYMMDD('20240103'),
|
|
123
|
-
Unixtime.fromYYYYMMDD('20240101')
|
|
124
|
-
);
|
|
125
|
-
expect([...it]).toEqual(['20240103', '20240102']);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it('should cross month boundaries correctly', () => {
|
|
129
|
-
const s = Unixtime.fromYYYYMMDD('20240302');
|
|
130
|
-
const e = Unixtime.fromYYYYMMDD('20240227');
|
|
131
|
-
const it = new DateIterator(s, e, '-');
|
|
132
|
-
expect(it.toArray()).toEqual([
|
|
133
|
-
'2024-03-02',
|
|
134
|
-
'2024-03-01',
|
|
135
|
-
'2024-02-29',
|
|
136
|
-
'2024-02-28',
|
|
137
|
-
]);
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it('should produce N entries for a span of N+1 days (end-exclusive)', () => {
|
|
141
|
-
const s = Unixtime.fromYYYYMMDD('20240620');
|
|
142
|
-
const e = Unixtime.fromYYYYMMDD('20240610');
|
|
143
|
-
const it = new DateIterator(s, e);
|
|
144
|
-
const arr = it.toArray();
|
|
145
|
-
expect(arr.length).toEqual(10);
|
|
146
|
-
expect(arr[0]).toEqual('20240620');
|
|
147
|
-
expect(arr[arr.length - 1]).toEqual('20240611');
|
|
148
|
-
});
|
|
149
|
-
});
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { Unixtime } from "./Unixtime"
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Iterates day-by-day backwards from `start` toward `end` (end-exclusive),
|
|
5
|
-
* emitting each date as a `yyyy{sep}mm{sep}dd` string.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* const it = new DateIterator(Unixtime.now(), Unixtime.now().addDays(-5))
|
|
9
|
-
* it.next() // "20260417"
|
|
10
|
-
* it.next() // "20260416"
|
|
11
|
-
* // ...
|
|
12
|
-
* it.next() // null
|
|
13
|
-
*
|
|
14
|
-
* @example separator
|
|
15
|
-
* new DateIterator(start, end, "-").toArray()
|
|
16
|
-
* // ["2026-04-17", "2026-04-16", ...]
|
|
17
|
-
*
|
|
18
|
-
* @example for..of
|
|
19
|
-
* for (const d of new DateIterator(start, end, "/")) console.log(d)
|
|
20
|
-
*/
|
|
21
|
-
export class DateIterator implements Iterable<string> {
|
|
22
|
-
private readonly start: Unixtime
|
|
23
|
-
private readonly end: Unixtime
|
|
24
|
-
private readonly separator: string
|
|
25
|
-
private cursor: Unixtime | null
|
|
26
|
-
|
|
27
|
-
constructor(start: Unixtime, end: Unixtime, separator: string = "") {
|
|
28
|
-
if (start.lessThan(end)) {
|
|
29
|
-
throw new Error('DateIterator: `start` must be newer than or equal to `end`')
|
|
30
|
-
}
|
|
31
|
-
// Defensive clones: callers' instances must not be mutated, and
|
|
32
|
-
// Unixtime.addDays() mutates the underlying Date.
|
|
33
|
-
this.start = start.clone()
|
|
34
|
-
this.end = end.clone()
|
|
35
|
-
this.separator = separator
|
|
36
|
-
this.cursor = this.start.clone()
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** Returns the next formatted date going backwards, or `null` when exhausted. */
|
|
40
|
-
next(): string | null {
|
|
41
|
-
if (this.cursor === null) return null
|
|
42
|
-
if (!this.cursor.greaterThan(this.end)) {
|
|
43
|
-
this.cursor = null
|
|
44
|
-
return null
|
|
45
|
-
}
|
|
46
|
-
const value = this.cursor.yyyymmdd(this.separator)
|
|
47
|
-
this.cursor.addDays(-1)
|
|
48
|
-
return value
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** Reset the iterator so it can be consumed again from `start`. */
|
|
52
|
-
reset(): this {
|
|
53
|
-
this.cursor = this.start.clone()
|
|
54
|
-
return this
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** Collect all remaining dates into an array. */
|
|
58
|
-
toArray(): string[] {
|
|
59
|
-
const out: string[] = []
|
|
60
|
-
let d: string | null
|
|
61
|
-
while ((d = this.next()) !== null) out.push(d)
|
|
62
|
-
return out
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
[Symbol.iterator](): Iterator<string> {
|
|
66
|
-
let cursor: Unixtime | null = this.start.clone()
|
|
67
|
-
const end = this.end
|
|
68
|
-
const sep = this.separator
|
|
69
|
-
return {
|
|
70
|
-
next(): IteratorResult<string> {
|
|
71
|
-
if (cursor === null || !cursor.greaterThan(end)) {
|
|
72
|
-
return { value: undefined as unknown as string, done: true }
|
|
73
|
-
}
|
|
74
|
-
const value = cursor.yyyymmdd(sep)
|
|
75
|
-
cursor.addDays(-1)
|
|
76
|
-
return { value, done: false }
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|