@giveback007/util-lib 0.25.4 → 1.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/README.md +7 -7
- package/dist/@types.d.ts +40 -42
- package/dist/@types.js +2 -2
- package/dist/array.d.ts +66 -66
- package/dist/array.js +128 -128
- package/dist/array.js.map +1 -1
- package/dist/clone.d.ts +2 -2
- package/dist/clone.js +44 -45
- package/dist/clone.js.map +1 -1
- package/dist/equality.d.ts +1 -1
- package/dist/equality.js +81 -82
- package/dist/equality.js.map +1 -1
- package/dist/general.d.ts +44 -37
- package/dist/general.js +172 -139
- package/dist/general.js.map +1 -1
- package/dist/index.d.ts +11 -11
- package/dist/index.js +27 -23
- package/dist/index.js.map +1 -1
- package/dist/iterate.d.ts +44 -44
- package/dist/iterate.js +43 -44
- package/dist/iterate.js.map +1 -1
- package/dist/number.d.ts +33 -32
- package/dist/number.js +57 -55
- package/dist/number.js.map +1 -1
- package/dist/object.d.ts +45 -49
- package/dist/object.js +82 -92
- package/dist/object.js.map +1 -1
- package/dist/string.d.ts +3 -4
- package/dist/string.js +8 -18
- package/dist/string.js.map +1 -1
- package/dist/test.d.ts +40 -41
- package/dist/test.js +65 -67
- package/dist/test.js.map +1 -1
- package/dist/time.d.ts +87 -93
- package/dist/time.js +230 -174
- package/dist/time.js.map +1 -1
- package/package.json +33 -45
- package/src/@types/types.d.ts +34 -0
- package/src/@types.ts +67 -67
- package/src/array.ts +175 -175
- package/src/clone.ts +34 -35
- package/src/equality.ts +80 -80
- package/src/general.ts +192 -152
- package/src/index.ts +11 -11
- package/src/iterate.ts +86 -86
- package/src/number.ts +64 -62
- package/src/object.ts +109 -123
- package/src/string.ts +6 -20
- package/src/test.ts +71 -74
- package/src/time.ts +268 -219
- package/dist/node/file-systems.d.ts +0 -1
- package/dist/node/file-systems.js +0 -16
- package/dist/node/file-systems.js.map +0 -1
- package/dist/node/index.d.ts +0 -1
- package/dist/node/index.js +0 -14
- package/dist/node/index.js.map +0 -1
- package/src/node/file-systems.ts +0 -16
- package/src/node/index.ts +0 -1
package/src/test.ts
CHANGED
|
@@ -1,74 +1,71 @@
|
|
|
1
|
-
import { equal, JsType } from '.';
|
|
2
|
-
import { JsTypeFind, objVals, type } from '.';
|
|
3
|
-
|
|
4
|
-
/** Check if `x` is equal to any of the values in the array. */
|
|
5
|
-
export const equalAny = (x: any, equals: any[]) => !!equals.find((y) => equal(x, y));
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Checks if object has the key, made as a function for type transfer.
|
|
9
|
-
*
|
|
10
|
-
* Uses `obj.hasOwnProperty(key)` instead of `key in obj`
|
|
11
|
-
*
|
|
12
|
-
* https://stackoverflow.com/questions/13632999/if-key-in-object-or-ifobject-hasownpropertykey
|
|
13
|
-
*/
|
|
14
|
-
export const hasKey = <
|
|
15
|
-
K extends (string | number)
|
|
16
|
-
>(obj: any, key: K): obj is { [P in K]: any } =>
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
/** Checks if object has keys from an array of keys, made as a function for type transfer */
|
|
20
|
-
export function hasKeys<
|
|
21
|
-
|
|
22
|
-
>(obj: any, keys: K[]): obj is { [P in K]: any } {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* The function will test if the type of the first
|
|
33
|
-
* argument equals testType. Argument testType is a string
|
|
34
|
-
* representing a javascript type.
|
|
35
|
-
*
|
|
36
|
-
* @param val value to be tested
|
|
37
|
-
* @param testType to check if typeof val === testType
|
|
38
|
-
* @example
|
|
39
|
-
* ```js
|
|
40
|
-
* isType([], 'array') //=> true
|
|
41
|
-
* isType(null, 'undefined') //=> false
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
|
-
export const isType = <T extends JsType>
|
|
45
|
-
|
|
46
|
-
): val is JsTypeFind<T> => type(val) === testType;
|
|
47
|
-
|
|
48
|
-
/** any of the values in the first "example" return `true`
|
|
49
|
-
* @example
|
|
50
|
-
* ```js
|
|
51
|
-
* nullOrEmpty(null | undefined | '' | [ ] | { }) //=> true
|
|
52
|
-
* nullOrEmpty([1, 2] | { key: 'value' } | true) //=> false
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
export function nullOrEmpty(x: any): boolean
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
export const isValidEmail = (email: string) =>
|
|
74
|
-
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email)
|
|
1
|
+
import { equal, JsType } from '.';
|
|
2
|
+
import { JsTypeFind, objVals, type } from '.';
|
|
3
|
+
|
|
4
|
+
/** Check if `x` is equal to any of the values in the array. */
|
|
5
|
+
export const equalAny = (x: any, equals: any[]) => !!equals.find((y) => equal(x, y));
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Checks if object has the key, made as a function for type transfer.
|
|
9
|
+
*
|
|
10
|
+
* Uses `obj.hasOwnProperty(key)` instead of `key in obj`
|
|
11
|
+
*
|
|
12
|
+
* https://stackoverflow.com/questions/13632999/if-key-in-object-or-ifobject-hasownpropertykey
|
|
13
|
+
*/
|
|
14
|
+
export const hasKey = <
|
|
15
|
+
K extends (string | number)
|
|
16
|
+
>(obj: any, key: K): obj is { [P in K]: any } =>
|
|
17
|
+
isType(obj, 'object') && obj.hasOwnProperty(key);
|
|
18
|
+
|
|
19
|
+
/** Checks if object has keys from an array of keys, made as a function for type transfer */
|
|
20
|
+
export function hasKeys<
|
|
21
|
+
K extends (string | number)
|
|
22
|
+
>(obj: any, keys: K[]): obj is { [P in K]: any } {
|
|
23
|
+
if (!isType(obj, 'object')) return false;
|
|
24
|
+
|
|
25
|
+
for (const key of keys)
|
|
26
|
+
if (!obj.hasOwnProperty(key)) return false;
|
|
27
|
+
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The function will test if the type of the first
|
|
33
|
+
* argument equals testType. Argument testType is a string
|
|
34
|
+
* representing a javascript type.
|
|
35
|
+
*
|
|
36
|
+
* @param val value to be tested
|
|
37
|
+
* @param testType to check if typeof val === testType
|
|
38
|
+
* @example
|
|
39
|
+
* ```js
|
|
40
|
+
* isType([], 'array') //=> true
|
|
41
|
+
* isType(null, 'undefined') //=> false
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export const isType = <T extends JsType>(
|
|
45
|
+
val: any, testType: T
|
|
46
|
+
): val is JsTypeFind<T> => type(val) === testType;
|
|
47
|
+
|
|
48
|
+
/** any of the values in the first "example" return `true`
|
|
49
|
+
* @example
|
|
50
|
+
* ```js
|
|
51
|
+
* nullOrEmpty(null | undefined | '' | [ ] | { }) //=> true
|
|
52
|
+
* nullOrEmpty([1, 2] | { key: 'value' } | true) //=> false
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export function nullOrEmpty(x: any): boolean {
|
|
56
|
+
// null || undefined
|
|
57
|
+
if (nonVal(x)) return true;
|
|
58
|
+
|
|
59
|
+
// (string || array).length === 0
|
|
60
|
+
if (isType(x, 'string') || isType(x, 'array')) return !x.length;
|
|
61
|
+
|
|
62
|
+
// object // { key: 'val' } => false, { } => true
|
|
63
|
+
if (isType(x, 'object')) return !objVals(x).length;
|
|
64
|
+
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** val `is` (null || undefined) */
|
|
69
|
+
export const nonVal = <T>(
|
|
70
|
+
x: T | null | undefined
|
|
71
|
+
): x is (undefined | null) => x === null || x === undefined;
|
package/src/time.ts
CHANGED
|
@@ -1,219 +1,268 @@
|
|
|
1
|
-
import { isType, MsTime } from '.';
|
|
2
|
-
|
|
3
|
-
export const msTime: MsTime = {
|
|
4
|
-
s: 1000,
|
|
5
|
-
m: 60000,
|
|
6
|
-
h: 3600000,
|
|
7
|
-
d: 86400000,
|
|
8
|
-
w: 604800000,
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export const weekTuple = ['
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Converts Date to time of day.
|
|
15
|
-
* Good for use in
|
|
16
|
-
*
|
|
17
|
-
* If no parameter `dt = new Date()`.
|
|
18
|
-
* @param dt instance of Date obj
|
|
19
|
-
* @returns `hh:mm:ss:ms`
|
|
20
|
-
* @example "15:07:56:150"
|
|
21
|
-
*/
|
|
22
|
-
export function dateTimeToString(dt = new Date())
|
|
23
|
-
{
|
|
24
|
-
const h = ('0' + dt.getHours()).slice(-2);
|
|
25
|
-
const m = ('0' + dt.getMinutes()).slice(-2);
|
|
26
|
-
const s = ('0' + dt.getSeconds()).slice(-2);
|
|
27
|
-
const ms = ('00' + dt.getMilliseconds()).slice(-3);
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
*/
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
if (
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
if (
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
1
|
+
import { isType, MsTime } from '.';
|
|
2
|
+
|
|
3
|
+
export const msTime: MsTime = {
|
|
4
|
+
s: 1000,
|
|
5
|
+
m: 60000,
|
|
6
|
+
h: 3600000,
|
|
7
|
+
d: 86400000,
|
|
8
|
+
w: 604800000,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const weekTuple = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] as const;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Converts Date to time of day.
|
|
15
|
+
* Good for use in logging.
|
|
16
|
+
*
|
|
17
|
+
* If no parameter `dt = new Date()`.
|
|
18
|
+
* @param dt instance of Date obj
|
|
19
|
+
* @returns `hh:mm:ss:ms`
|
|
20
|
+
* @example "15:07:56:150"
|
|
21
|
+
*/
|
|
22
|
+
export function dateTimeToString(dt = new Date())
|
|
23
|
+
{
|
|
24
|
+
const h = ('0' + dt.getHours()).slice(-2);
|
|
25
|
+
const m = ('0' + dt.getMinutes()).slice(-2);
|
|
26
|
+
const s = ('0' + dt.getSeconds()).slice(-2);
|
|
27
|
+
const ms = ('00' + dt.getMilliseconds()).slice(-3);
|
|
28
|
+
|
|
29
|
+
return `${h}:${m}:${s}:${ms}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Gives the 'start' and 'end' of a day.
|
|
33
|
+
*
|
|
34
|
+
* Start of day is the first ms of the day
|
|
35
|
+
*
|
|
36
|
+
* End of day is the last ms of the day
|
|
37
|
+
*/
|
|
38
|
+
export function getDayStartEnd(t: number | Date, type: 'unix' | 'local' = 'local') {
|
|
39
|
+
if (isType(t, 'number')) t = new Date(t);
|
|
40
|
+
|
|
41
|
+
const y = t.getFullYear();
|
|
42
|
+
const m = t.getMonth();
|
|
43
|
+
const d = t.getDate();
|
|
44
|
+
|
|
45
|
+
let start = new Date(y, m, d);
|
|
46
|
+
let end = new Date(y, m, d, 23, 59, 59, 999);
|
|
47
|
+
|
|
48
|
+
if (type === 'unix') {
|
|
49
|
+
const tzOffset = time.min(start.getTimezoneOffset());
|
|
50
|
+
|
|
51
|
+
start = new Date(start.getTime() + tzOffset);
|
|
52
|
+
end = new Date(end.getTime() + tzOffset);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return { start, end };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function weekStartEnd(t: number | Date, weekStart: 'sun' | 'mon' = 'sun', type: 'unix' | 'local' = 'local') {
|
|
59
|
+
if (isType(t, 'number')) t = new Date(t);
|
|
60
|
+
|
|
61
|
+
const wd = t.getDay();
|
|
62
|
+
|
|
63
|
+
const startOffset = wd * -1 + (weekStart === 'mon' ? 1 : 0);
|
|
64
|
+
const endOffset = 6 + startOffset;
|
|
65
|
+
|
|
66
|
+
const s = new Date(t.getTime() + time.dys(startOffset));
|
|
67
|
+
const e = new Date(t.getTime() + time.dys(endOffset));
|
|
68
|
+
|
|
69
|
+
let start = new Date(s.getFullYear(), s.getMonth(), s.getDate());
|
|
70
|
+
let end = new Date(e.getFullYear(), e.getMonth(), e.getDate(), 23, 59, 59, 999);
|
|
71
|
+
|
|
72
|
+
if (type === 'unix') {
|
|
73
|
+
const tzOffset = time.min(start.getTimezoneOffset());
|
|
74
|
+
|
|
75
|
+
start = new Date(start.getTime() + tzOffset);
|
|
76
|
+
end = new Date(end.getTime() + tzOffset);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return { start, end };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function monthStartEnd(t: number | Date, type: 'unix' | 'local' = 'local') {
|
|
83
|
+
if (isType(t, 'number')) t = new Date(t);
|
|
84
|
+
|
|
85
|
+
const y = t.getFullYear();
|
|
86
|
+
const m = t.getMonth();
|
|
87
|
+
|
|
88
|
+
let start = new Date(y, m, 1);
|
|
89
|
+
let end = new Date(y, m, 0, 23, 59, 59, 999);
|
|
90
|
+
|
|
91
|
+
if (type === 'unix') {
|
|
92
|
+
const tzOffset = time.min(start.getTimezoneOffset());
|
|
93
|
+
|
|
94
|
+
start = new Date(start.getTime() + tzOffset);
|
|
95
|
+
end = new Date(end.getTime() + tzOffset);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return { start, end };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function yearStartEnd(t: number | Date, type: 'unix' | 'local' = 'local') {
|
|
102
|
+
if (isType(t, 'number')) t = new Date(t);
|
|
103
|
+
|
|
104
|
+
const y = t.getFullYear();
|
|
105
|
+
|
|
106
|
+
let start = new Date(y, 0, 1);
|
|
107
|
+
let end = new Date(y, 12, 0, 23, 59, 59, 999);
|
|
108
|
+
|
|
109
|
+
if (type === 'unix') {
|
|
110
|
+
const tzOffset = time.min(start.getTimezoneOffset());
|
|
111
|
+
|
|
112
|
+
start = new Date(start.getTime() + tzOffset);
|
|
113
|
+
end = new Date(end.getTime() + tzOffset);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return { start, end };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export const time = {
|
|
120
|
+
/** Seconds(n) -> to ms */
|
|
121
|
+
sec: (n: num) => 1_000 * n,
|
|
122
|
+
/** Minutes(n) -> to ms */
|
|
123
|
+
min: (n: num) => 60_000 * n,
|
|
124
|
+
/** Hours(n) -> to ms */
|
|
125
|
+
hrs: (n: num) => 3_600_000 * n,
|
|
126
|
+
/** Days(n) -> to ms */
|
|
127
|
+
dys: (n: num) => 86_400_000 * n,
|
|
128
|
+
/** Weeks(n) -> to ms */
|
|
129
|
+
wks: (n: num) => 604_800_000 * n,
|
|
130
|
+
|
|
131
|
+
/** Count time elapsed since: `from` until `to`, `to` def to `Date.now()` */
|
|
132
|
+
since: (from: num, to: num = Date.now()) => to - from,
|
|
133
|
+
/** Count down to: `target` time from `from`, `from` def to `Date.now()` */
|
|
134
|
+
until: (target: num, from: num = Date.now()) => target - from,
|
|
135
|
+
|
|
136
|
+
future: {
|
|
137
|
+
/** fnc(n) -> add ms to now */
|
|
138
|
+
ms: (n: num) => Date.now() + n,
|
|
139
|
+
/** fnc(n) -> add sec to now */
|
|
140
|
+
sec: (n: num) => Date.now() + n * msTime.s,
|
|
141
|
+
/** fnc(n) -> add min to now */
|
|
142
|
+
min: (n: num) => Date.now() + n * msTime.m,
|
|
143
|
+
},
|
|
144
|
+
|
|
145
|
+
msTo: {
|
|
146
|
+
/** fnc(n) -> from ms to num of seconds */
|
|
147
|
+
sec: (ms: num) => ms / msTime.s,
|
|
148
|
+
/** fnc(n) -> from ms to num of minutes */
|
|
149
|
+
min: (ms: num) => ms / msTime.m,
|
|
150
|
+
/** fnc(n) -> from ms to num of hours */
|
|
151
|
+
hrs: (ms: num) => ms / msTime.h,
|
|
152
|
+
/** fnc(n) -> from ms to num of days */
|
|
153
|
+
dys: (ms: num) => ms / msTime.d,
|
|
154
|
+
/** fnc(n) -> from ms to num of weeks */
|
|
155
|
+
wks: (ms: num) => ms / msTime.w,
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
get now() { return Date.now(); }
|
|
159
|
+
} as const;
|
|
160
|
+
|
|
161
|
+
export const humanizedTime = (date: AnyDate) => {
|
|
162
|
+
const dt = parseDate(date);
|
|
163
|
+
if (!dt) return null;
|
|
164
|
+
const dif = time.since(dt.getTime());
|
|
165
|
+
|
|
166
|
+
/** p for plural */
|
|
167
|
+
const p = (n: num) => n > 1 ? 's' : '';
|
|
168
|
+
|
|
169
|
+
if (dif < 0) {
|
|
170
|
+
return dt.toLocaleString();
|
|
171
|
+
} else if (dif < time.min(1)) {
|
|
172
|
+
const s = Math.ceil(time.msTo.sec(dif)) || 1;
|
|
173
|
+
return `${s} second${p(s)} ago`
|
|
174
|
+
} else if (dif < time.hrs(1)) {
|
|
175
|
+
const m = Math.floor(time.msTo.min(dif));
|
|
176
|
+
return `${m} minute${p(m)} ago`
|
|
177
|
+
} else if (dif < time.hrs(24) && dt.getDate() === new Date().getDate()) {
|
|
178
|
+
const h = Math.floor(time.msTo.hrs(dif));
|
|
179
|
+
const m = Math.floor(time.msTo.min(dif - time.hrs(h)))
|
|
180
|
+
return `${h} hour${p(h)} ${m > 1 ? `${m} minute${p(m)} ` : ''}ago`
|
|
181
|
+
} else {
|
|
182
|
+
return dt.toLocaleString();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Intl.supportedValuesOf('timeZone');
|
|
187
|
+
/** A Date substitute, to make working with time easier and more versatile */
|
|
188
|
+
export function getTime(
|
|
189
|
+
date: TimeObj | PartialTimeObj | TimeArr,
|
|
190
|
+
ianaTimeZone?: str
|
|
191
|
+
) {
|
|
192
|
+
const dt = parseDate(date);
|
|
193
|
+
if (!dt) throw new Error('Invalid Date');
|
|
194
|
+
|
|
195
|
+
ianaTimeZone = ianaTimeZone || Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
196
|
+
const tzOffsetMin = timeZoneOffsetMin(ianaTimeZone, dt);
|
|
197
|
+
|
|
198
|
+
const localISO = toLocalISOString(dt, tzOffsetMin);
|
|
199
|
+
const finalDateObj = new Date(localISO);
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
tzOffsetMin, // this flips from the JS output, to align with the ISO output (- when js would be +)
|
|
203
|
+
localISO,
|
|
204
|
+
isoStr: finalDateObj.toISOString(),
|
|
205
|
+
Date: finalDateObj,
|
|
206
|
+
epochMs: finalDateObj.getTime(),
|
|
207
|
+
ianaTZ: ianaTimeZone,
|
|
208
|
+
timeObj: timeObj(dt),
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function timeZoneOffsetMin(
|
|
213
|
+
ianaTimeZone: string,
|
|
214
|
+
date = new Date(),
|
|
215
|
+
) {
|
|
216
|
+
const local = new Date(date);
|
|
217
|
+
local.setMilliseconds(0);
|
|
218
|
+
local.setSeconds(0);
|
|
219
|
+
const tzTime = local.toLocaleString('US-en', { timeZone: ianaTimeZone });
|
|
220
|
+
const tzDt = new Date(tzTime);
|
|
221
|
+
|
|
222
|
+
const offsetMs = -local.getTimezoneOffset() * 60_000;
|
|
223
|
+
const utcTime = tzDt.getTime() + offsetMs;
|
|
224
|
+
|
|
225
|
+
return (utcTime - local.getTime()) / 60_000
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export const timeObj = (dt: Date): TimeObj => ({
|
|
229
|
+
y: dt.getFullYear(),
|
|
230
|
+
m: dt.getMonth() + 1,
|
|
231
|
+
d: dt.getDate(),
|
|
232
|
+
h: dt.getHours(),
|
|
233
|
+
min: dt.getMinutes(),
|
|
234
|
+
sec: dt.getSeconds(),
|
|
235
|
+
ms: dt.getMilliseconds(),
|
|
236
|
+
wDay: weekTuple[dt.getDay()]!
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
export function parseDate(d: AnyDate) {
|
|
240
|
+
const dt =
|
|
241
|
+
Array.isArray(d) ?
|
|
242
|
+
new Date(d[0], (d[1] || 1) - 1, d[2] || 1, d[3] || 0, d[4] || 0, d[5] || 0, d[6] || 0)
|
|
243
|
+
:
|
|
244
|
+
isType(d, 'object') && !(d instanceof Date) ?
|
|
245
|
+
new Date(d.y, (d?.m || 1) - 1, d.d || 1, d.h || 0, d.min || 0, d.sec || 0, d.ms || 0)
|
|
246
|
+
:
|
|
247
|
+
new Date(d);
|
|
248
|
+
|
|
249
|
+
if (isNaN(dt as any)) {
|
|
250
|
+
console.error(new Error(`Invalid Date: ${isType(d, 'string') ? `"${d}"` : JSON.stringify(d)}`));
|
|
251
|
+
return null
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return dt;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function toLocalISOString(
|
|
258
|
+
date: Date,
|
|
259
|
+
tzOffsetMin = -date.getTimezoneOffset()
|
|
260
|
+
) {
|
|
261
|
+
const sign = tzOffsetMin >= 0 ? '+' : '-';
|
|
262
|
+
const diffHours = String(Math.floor(Math.abs(tzOffsetMin) / 60)).padStart(2, '0');
|
|
263
|
+
const diffMinutes = String(Math.abs(tzOffsetMin) % 60).padStart(2, '0');
|
|
264
|
+
|
|
265
|
+
return date.toLocaleString('lt').replace(' ', 'T') // or use: "sv-SE" (same effect)
|
|
266
|
+
+ '.' + (date.getMilliseconds() + '').padStart(3, '0')
|
|
267
|
+
+ sign + diffHours + ':' + diffMinutes;
|
|
268
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const deleteFile: (path: string) => void;
|