@naturalcycles/js-lib 14.95.1 → 14.97.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/datetime/localDate.d.ts +18 -10
- package/dist/datetime/localDate.js +91 -65
- package/dist/datetime/localTime.d.ts +10 -10
- package/dist/datetime/localTime.js +1 -13
- package/dist/error/try.d.ts +9 -3
- package/dist/error/try.js +8 -2
- package/dist/error/tryCatch.d.ts +1 -1
- package/dist/error/tryCatch.js +1 -1
- package/dist/index.d.ts +2 -3
- package/dist/index.js +0 -1
- package/dist/types.d.ts +11 -3
- package/dist-esm/datetime/localDate.js +91 -65
- package/dist-esm/datetime/localTime.js +1 -13
- package/dist-esm/error/try.js +8 -2
- package/dist-esm/error/tryCatch.js +2 -2
- package/dist-esm/index.js +0 -1
- package/package.json +1 -1
- package/src/datetime/localDate.ts +107 -71
- package/src/datetime/localTime.ts +11 -23
- package/src/error/try.ts +10 -4
- package/src/error/tryCatch.ts +3 -3
- package/src/index.ts +6 -3
- package/src/types.ts +13 -3
- package/dist/promise/pTuple.d.ts +0 -7
- package/dist/promise/pTuple.js +0 -13
- package/dist-esm/promise/pTuple.js +0 -9
- package/src/promise/pTuple.ts +0 -11
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Sequence } from '../seq/seq';
|
|
2
|
-
import {
|
|
2
|
+
import { IsoDateString, UnixTimestampNumber } from '../types';
|
|
3
3
|
import { LocalTime } from './localTime';
|
|
4
4
|
export declare type LocalDateUnit = 'year' | 'month' | 'day';
|
|
5
5
|
export declare type Inclusiveness = '()' | '[]' | '[)' | '(]';
|
|
@@ -8,9 +8,9 @@ export declare type LocalDateConfig = LocalDate | string;
|
|
|
8
8
|
* @experimental
|
|
9
9
|
*/
|
|
10
10
|
export declare class LocalDate {
|
|
11
|
-
year
|
|
12
|
-
month
|
|
13
|
-
day
|
|
11
|
+
private $year;
|
|
12
|
+
private $month;
|
|
13
|
+
private $day;
|
|
14
14
|
private constructor();
|
|
15
15
|
static create(year: number, month: number, day: number): LocalDate;
|
|
16
16
|
/**
|
|
@@ -35,9 +35,17 @@ export declare class LocalDate {
|
|
|
35
35
|
static latest(items: LocalDate[]): LocalDate;
|
|
36
36
|
static range(minIncl: LocalDateConfig, maxExcl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
|
|
37
37
|
static rangeSeq(minIncl: LocalDateConfig, maxExcl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): Sequence<LocalDate>;
|
|
38
|
-
static rangeString(minIncl: LocalDateConfig, maxExcl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit):
|
|
38
|
+
static rangeString(minIncl: LocalDateConfig, maxExcl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): IsoDateString[];
|
|
39
39
|
static rangeIncl(minIncl: LocalDateConfig, maxIncl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
|
|
40
|
-
static rangeInclString(minIncl: LocalDateConfig, maxIncl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit):
|
|
40
|
+
static rangeInclString(minIncl: LocalDateConfig, maxIncl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): IsoDateString[];
|
|
41
|
+
get(unit: LocalDateUnit): number;
|
|
42
|
+
set(unit: LocalDateUnit, v: number, mutate?: boolean): LocalDate;
|
|
43
|
+
year(): number;
|
|
44
|
+
year(v: number): LocalDate;
|
|
45
|
+
month(): number;
|
|
46
|
+
month(v: number): LocalDate;
|
|
47
|
+
day(): number;
|
|
48
|
+
day(v: number): LocalDate;
|
|
41
49
|
isSame(d: LocalDateConfig): boolean;
|
|
42
50
|
isBefore(d: LocalDateConfig): boolean;
|
|
43
51
|
isSameOrBefore(d: LocalDateConfig): boolean;
|
|
@@ -76,12 +84,12 @@ export declare class LocalDate {
|
|
|
76
84
|
*/
|
|
77
85
|
toDate(): Date;
|
|
78
86
|
toLocalTime(): LocalTime;
|
|
79
|
-
toISODate():
|
|
80
|
-
toString():
|
|
87
|
+
toISODate(): IsoDateString;
|
|
88
|
+
toString(): IsoDateString;
|
|
81
89
|
toStringCompact(): string;
|
|
82
|
-
unix():
|
|
90
|
+
unix(): UnixTimestampNumber;
|
|
83
91
|
unixMillis(): number;
|
|
84
|
-
toJSON():
|
|
92
|
+
toJSON(): IsoDateString;
|
|
85
93
|
}
|
|
86
94
|
/**
|
|
87
95
|
* Shortcut wrapper around `LocalDate.parse` / `LocalDate.today`
|
|
@@ -6,14 +6,15 @@ const seq_1 = require("../seq/seq");
|
|
|
6
6
|
const types_1 = require("../types");
|
|
7
7
|
const localTime_1 = require("./localTime");
|
|
8
8
|
const m31 = new Set([1, 3, 5, 7, 8, 10, 12]);
|
|
9
|
+
/* eslint-disable no-dupe-class-members */
|
|
9
10
|
/**
|
|
10
11
|
* @experimental
|
|
11
12
|
*/
|
|
12
13
|
class LocalDate {
|
|
13
|
-
constructor(year, month, day) {
|
|
14
|
-
this
|
|
15
|
-
this
|
|
16
|
-
this
|
|
14
|
+
constructor($year, $month, $day) {
|
|
15
|
+
this.$year = $year;
|
|
16
|
+
this.$month = $month;
|
|
17
|
+
this.$day = $day;
|
|
17
18
|
}
|
|
18
19
|
static create(year, month, day) {
|
|
19
20
|
return new LocalDate(year, month, day);
|
|
@@ -117,9 +118,34 @@ class LocalDate {
|
|
|
117
118
|
static rangeInclString(minIncl, maxIncl, step = 1, stepUnit = 'day') {
|
|
118
119
|
return LocalDate.range(minIncl, LocalDate.of(maxIncl).add(1, stepUnit), step, stepUnit).map(ld => ld.toString());
|
|
119
120
|
}
|
|
121
|
+
get(unit) {
|
|
122
|
+
return unit === 'year' ? this.$year : unit === 'month' ? this.$month : this.$day;
|
|
123
|
+
}
|
|
124
|
+
set(unit, v, mutate = false) {
|
|
125
|
+
const t = mutate ? this : this.clone();
|
|
126
|
+
if (unit === 'year') {
|
|
127
|
+
t.$year = v;
|
|
128
|
+
}
|
|
129
|
+
else if (unit === 'month') {
|
|
130
|
+
t.$month = v;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
t.$day = v;
|
|
134
|
+
}
|
|
135
|
+
return t;
|
|
136
|
+
}
|
|
137
|
+
year(v) {
|
|
138
|
+
return v === undefined ? this.$year : this.set('year', v);
|
|
139
|
+
}
|
|
140
|
+
month(v) {
|
|
141
|
+
return v === undefined ? this.$month : this.set('month', v);
|
|
142
|
+
}
|
|
143
|
+
day(v) {
|
|
144
|
+
return v === undefined ? this.$day : this.set('day', v);
|
|
145
|
+
}
|
|
120
146
|
isSame(d) {
|
|
121
147
|
d = LocalDate.of(d);
|
|
122
|
-
return this
|
|
148
|
+
return this.$day === d.$day && this.$month === d.$month && this.$year === d.$year;
|
|
123
149
|
}
|
|
124
150
|
isBefore(d) {
|
|
125
151
|
return this.cmp(d) === -1;
|
|
@@ -149,17 +175,17 @@ class LocalDate {
|
|
|
149
175
|
*/
|
|
150
176
|
cmp(d) {
|
|
151
177
|
d = LocalDate.of(d);
|
|
152
|
-
if (this
|
|
178
|
+
if (this.$year < d.$year)
|
|
153
179
|
return -1;
|
|
154
|
-
if (this
|
|
180
|
+
if (this.$year > d.$year)
|
|
155
181
|
return 1;
|
|
156
|
-
if (this
|
|
182
|
+
if (this.$month < d.$month)
|
|
157
183
|
return -1;
|
|
158
|
-
if (this
|
|
184
|
+
if (this.$month > d.$month)
|
|
159
185
|
return 1;
|
|
160
|
-
if (this
|
|
186
|
+
if (this.$day < d.$day)
|
|
161
187
|
return -1;
|
|
162
|
-
if (this
|
|
188
|
+
if (this.$day > d.$day)
|
|
163
189
|
return 1;
|
|
164
190
|
return 0;
|
|
165
191
|
}
|
|
@@ -177,82 +203,82 @@ class LocalDate {
|
|
|
177
203
|
diff(d, unit) {
|
|
178
204
|
d = LocalDate.of(d);
|
|
179
205
|
if (unit === 'year') {
|
|
180
|
-
return this
|
|
206
|
+
return this.$year - d.$year;
|
|
181
207
|
}
|
|
182
208
|
if (unit === 'month') {
|
|
183
|
-
return (this
|
|
209
|
+
return (this.$year - d.$year) * 12 + (this.$month - d.$month);
|
|
184
210
|
}
|
|
185
211
|
// unit is 'day'
|
|
186
|
-
let days = this
|
|
187
|
-
if (d
|
|
188
|
-
for (let year = d
|
|
212
|
+
let days = this.$day - d.$day;
|
|
213
|
+
if (d.$year < this.$year) {
|
|
214
|
+
for (let year = d.$year; year < this.$year; year++) {
|
|
189
215
|
days += LocalDate.getYearLength(year);
|
|
190
216
|
}
|
|
191
217
|
}
|
|
192
|
-
else if (this
|
|
193
|
-
for (let year = this
|
|
218
|
+
else if (this.$year < d.$year) {
|
|
219
|
+
for (let year = this.$year; year < d.$year; year++) {
|
|
194
220
|
days -= LocalDate.getYearLength(year);
|
|
195
221
|
}
|
|
196
222
|
}
|
|
197
|
-
if (d
|
|
198
|
-
for (let month = d
|
|
199
|
-
days += LocalDate.getMonthLength(this
|
|
223
|
+
if (d.$month < this.$month) {
|
|
224
|
+
for (let month = d.$month; month < this.$month; month++) {
|
|
225
|
+
days += LocalDate.getMonthLength(this.$year, month);
|
|
200
226
|
}
|
|
201
227
|
}
|
|
202
|
-
else if (this
|
|
203
|
-
for (let month = this
|
|
204
|
-
days -= LocalDate.getMonthLength(d
|
|
228
|
+
else if (this.$month < d.$month) {
|
|
229
|
+
for (let month = this.$month; month < d.$month; month++) {
|
|
230
|
+
days -= LocalDate.getMonthLength(d.$year, month);
|
|
205
231
|
}
|
|
206
232
|
}
|
|
207
233
|
return days;
|
|
208
234
|
}
|
|
209
235
|
add(num, unit, mutate = false) {
|
|
210
|
-
let { day, month, year } = this;
|
|
236
|
+
let { $day, $month, $year } = this;
|
|
211
237
|
if (unit === 'day') {
|
|
212
|
-
day += num;
|
|
238
|
+
$day += num;
|
|
213
239
|
}
|
|
214
240
|
else if (unit === 'month') {
|
|
215
|
-
month += num;
|
|
241
|
+
$month += num;
|
|
216
242
|
}
|
|
217
243
|
else if (unit === 'year') {
|
|
218
|
-
year += num;
|
|
244
|
+
$year += num;
|
|
219
245
|
}
|
|
220
246
|
// check day overflow
|
|
221
|
-
let monLen = LocalDate.getMonthLength(year, month);
|
|
222
|
-
while (day > monLen) {
|
|
223
|
-
day -= monLen;
|
|
224
|
-
month += 1;
|
|
225
|
-
if (month > 12) {
|
|
226
|
-
year += 1;
|
|
227
|
-
month -= 12;
|
|
247
|
+
let monLen = LocalDate.getMonthLength($year, $month);
|
|
248
|
+
while ($day > monLen) {
|
|
249
|
+
$day -= monLen;
|
|
250
|
+
$month += 1;
|
|
251
|
+
if ($month > 12) {
|
|
252
|
+
$year += 1;
|
|
253
|
+
$month -= 12;
|
|
228
254
|
}
|
|
229
|
-
monLen = LocalDate.getMonthLength(year, month);
|
|
255
|
+
monLen = LocalDate.getMonthLength($year, $month);
|
|
230
256
|
}
|
|
231
|
-
while (day < 1) {
|
|
232
|
-
day += monLen;
|
|
233
|
-
month -= 1;
|
|
234
|
-
if (month < 1) {
|
|
235
|
-
year -= 1;
|
|
236
|
-
month += 12;
|
|
257
|
+
while ($day < 1) {
|
|
258
|
+
$day += monLen;
|
|
259
|
+
$month -= 1;
|
|
260
|
+
if ($month < 1) {
|
|
261
|
+
$year -= 1;
|
|
262
|
+
$month += 12;
|
|
237
263
|
}
|
|
238
|
-
monLen = LocalDate.getMonthLength(year, month);
|
|
264
|
+
monLen = LocalDate.getMonthLength($year, $month);
|
|
239
265
|
}
|
|
240
266
|
// check month overflow
|
|
241
|
-
while (month > 12) {
|
|
242
|
-
year += 1;
|
|
243
|
-
month -= 12;
|
|
267
|
+
while ($month > 12) {
|
|
268
|
+
$year += 1;
|
|
269
|
+
$month -= 12;
|
|
244
270
|
}
|
|
245
|
-
while (month < 1) {
|
|
246
|
-
year -= 1;
|
|
247
|
-
month += 12;
|
|
271
|
+
while ($month < 1) {
|
|
272
|
+
$year -= 1;
|
|
273
|
+
$month += 12;
|
|
248
274
|
}
|
|
249
275
|
if (mutate) {
|
|
250
|
-
this
|
|
251
|
-
this
|
|
252
|
-
this
|
|
276
|
+
this.$year = $year;
|
|
277
|
+
this.$month = $month;
|
|
278
|
+
this.$day = $day;
|
|
253
279
|
return this;
|
|
254
280
|
}
|
|
255
|
-
return new LocalDate(year, month, day);
|
|
281
|
+
return new LocalDate($year, $month, $day);
|
|
256
282
|
}
|
|
257
283
|
subtract(num, unit, mutate = false) {
|
|
258
284
|
return this.add(-num, unit, mutate);
|
|
@@ -261,17 +287,17 @@ class LocalDate {
|
|
|
261
287
|
if (unit === 'day')
|
|
262
288
|
return this;
|
|
263
289
|
if (unit === 'month')
|
|
264
|
-
return LocalDate.create(this
|
|
290
|
+
return LocalDate.create(this.$year, this.$month, 1);
|
|
265
291
|
// year
|
|
266
|
-
return LocalDate.create(this
|
|
292
|
+
return LocalDate.create(this.$year, 1, 1);
|
|
267
293
|
}
|
|
268
294
|
endOf(unit) {
|
|
269
295
|
if (unit === 'day')
|
|
270
296
|
return this;
|
|
271
297
|
if (unit === 'month')
|
|
272
|
-
return LocalDate.create(this
|
|
298
|
+
return LocalDate.create(this.$year, this.$month, LocalDate.getMonthLength(this.$year, this.$month));
|
|
273
299
|
// year
|
|
274
|
-
return LocalDate.create(this
|
|
300
|
+
return LocalDate.create(this.$year, 12, 31);
|
|
275
301
|
}
|
|
276
302
|
static getYearLength(year) {
|
|
277
303
|
return this.isLeapYear(year) ? 366 : 365;
|
|
@@ -289,7 +315,7 @@ class LocalDate {
|
|
|
289
315
|
return year % 400 === 0;
|
|
290
316
|
}
|
|
291
317
|
clone() {
|
|
292
|
-
return new LocalDate(this
|
|
318
|
+
return new LocalDate(this.$year, this.$month, this.$day);
|
|
293
319
|
}
|
|
294
320
|
/**
|
|
295
321
|
* Converts LocalDate into instance of Date.
|
|
@@ -298,7 +324,7 @@ class LocalDate {
|
|
|
298
324
|
* Timezone will match local timezone.
|
|
299
325
|
*/
|
|
300
326
|
toDate() {
|
|
301
|
-
return new Date(this
|
|
327
|
+
return new Date(this.$year, this.$month - 1, this.$day);
|
|
302
328
|
}
|
|
303
329
|
toLocalTime() {
|
|
304
330
|
return localTime_1.LocalTime.of(this.toDate());
|
|
@@ -308,16 +334,16 @@ class LocalDate {
|
|
|
308
334
|
}
|
|
309
335
|
toString() {
|
|
310
336
|
return [
|
|
311
|
-
String(this
|
|
312
|
-
String(this
|
|
313
|
-
String(this
|
|
337
|
+
String(this.$year).padStart(4, '0'),
|
|
338
|
+
String(this.$month).padStart(2, '0'),
|
|
339
|
+
String(this.$day).padStart(2, '0'),
|
|
314
340
|
].join('-');
|
|
315
341
|
}
|
|
316
342
|
toStringCompact() {
|
|
317
343
|
return [
|
|
318
|
-
String(this
|
|
319
|
-
String(this
|
|
320
|
-
String(this
|
|
344
|
+
String(this.$year).padStart(4, '0'),
|
|
345
|
+
String(this.$month).padStart(2, '0'),
|
|
346
|
+
String(this.$day).padStart(2, '0'),
|
|
321
347
|
].join('');
|
|
322
348
|
}
|
|
323
349
|
// May be not optimal, as LocalTime better suits it
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IsoDateString, IsoDateTimeString, UnixTimestampNumber } from '../types';
|
|
2
2
|
import { Inclusiveness, LocalDate } from './localDate';
|
|
3
3
|
export declare type LocalTimeUnit = 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second';
|
|
4
|
-
export declare type LocalTimeConfig = LocalTime | Date |
|
|
4
|
+
export declare type LocalTimeConfig = LocalTime | Date | IsoDateTimeString | UnixTimestampNumber;
|
|
5
5
|
export interface LocalTimeComponents {
|
|
6
6
|
year: number;
|
|
7
7
|
month: number;
|
|
@@ -40,8 +40,8 @@ export declare class LocalTime {
|
|
|
40
40
|
year(v: number): LocalTime;
|
|
41
41
|
month(): number;
|
|
42
42
|
month(v: number): LocalTime;
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
day(): number;
|
|
44
|
+
day(v: number): LocalTime;
|
|
45
45
|
hour(): number;
|
|
46
46
|
hour(v: number): LocalTime;
|
|
47
47
|
minute(): number;
|
|
@@ -75,19 +75,19 @@ export declare class LocalTime {
|
|
|
75
75
|
fromNow(now?: LocalTimeConfig): string;
|
|
76
76
|
getDate(): Date;
|
|
77
77
|
clone(): LocalTime;
|
|
78
|
-
unix():
|
|
78
|
+
unix(): UnixTimestampNumber;
|
|
79
79
|
unixMillis(): number;
|
|
80
|
-
valueOf():
|
|
80
|
+
valueOf(): UnixTimestampNumber;
|
|
81
81
|
toLocalDate(): LocalDate;
|
|
82
|
-
toPretty(seconds?: boolean):
|
|
82
|
+
toPretty(seconds?: boolean): IsoDateTimeString;
|
|
83
83
|
/**
|
|
84
84
|
* Returns e.g: `1984-06-21T17:56:21`, only the date part of DateTime
|
|
85
85
|
*/
|
|
86
|
-
toISODateTime():
|
|
86
|
+
toISODateTime(): IsoDateTimeString;
|
|
87
87
|
/**
|
|
88
88
|
* Returns e.g: `1984-06-21`, only the date part of DateTime
|
|
89
89
|
*/
|
|
90
|
-
toISODate():
|
|
90
|
+
toISODate(): IsoDateString;
|
|
91
91
|
/**
|
|
92
92
|
* Returns e.g: `17:03:15` (or `17:03` with seconds=false)
|
|
93
93
|
*/
|
|
@@ -97,7 +97,7 @@ export declare class LocalTime {
|
|
|
97
97
|
*/
|
|
98
98
|
toStringCompact(seconds?: boolean): string;
|
|
99
99
|
toString(): string;
|
|
100
|
-
toJSON():
|
|
100
|
+
toJSON(): UnixTimestampNumber;
|
|
101
101
|
}
|
|
102
102
|
/**
|
|
103
103
|
* Shortcut wrapper around `LocalDate.parse` / `LocalDate.today`
|
|
@@ -5,18 +5,6 @@ const assert_1 = require("../error/assert");
|
|
|
5
5
|
const time_util_1 = require("../time/time.util");
|
|
6
6
|
const localDate_1 = require("./localDate");
|
|
7
7
|
/* eslint-disable no-dupe-class-members */
|
|
8
|
-
// Design choices:
|
|
9
|
-
// No milliseconds
|
|
10
|
-
// No timezone support, ISO8601 is parsed as LocalDateTime, discarding Timezone information
|
|
11
|
-
// Formats as unix timestamp, ISO8601 or "pretty string"
|
|
12
|
-
// toString and .toJSON formats as unix timestamp
|
|
13
|
-
// No "unixMillis", just pure unixtimestamp
|
|
14
|
-
// .valueOf returns unix timestamp (no millis)
|
|
15
|
-
// Prevents dayjs(undefined) being dayjs.now()
|
|
16
|
-
// Validates on parse, throws if invalid. Doesn't allow invalid objects
|
|
17
|
-
// No arbitrary .format('') (which is slow to parse) vs well-defined (opinionated) formats
|
|
18
|
-
// Separate LocalTime, powered by native js Date, and LocalDate - a smaller functionality class when
|
|
19
|
-
// you only need "whole dates", no time, no timezone worry
|
|
20
8
|
/**
|
|
21
9
|
* @experimental
|
|
22
10
|
*/
|
|
@@ -130,7 +118,7 @@ class LocalTime {
|
|
|
130
118
|
month(v) {
|
|
131
119
|
return v === undefined ? this.get('month') : this.set('month', v);
|
|
132
120
|
}
|
|
133
|
-
|
|
121
|
+
day(v) {
|
|
134
122
|
return v === undefined ? this.get('day') : this.set('day', v);
|
|
135
123
|
}
|
|
136
124
|
hour(v) {
|
package/dist/error/try.d.ts
CHANGED
|
@@ -5,25 +5,31 @@ import { AppError } from './app.error';
|
|
|
5
5
|
* Allows to write shorter code that avoids `try/catch`.
|
|
6
6
|
* Useful e.g. in unit tests.
|
|
7
7
|
*
|
|
8
|
-
* Similar to
|
|
8
|
+
* Similar to pTry, but for sync functions.
|
|
9
9
|
*
|
|
10
10
|
* For convenience, second argument type is non-optional,
|
|
11
11
|
* so you can use it without `!`. But you SHOULD always check `if (err)` first!
|
|
12
12
|
*
|
|
13
|
+
* ERR is typed as Error, not `unknown`. While unknown would be more correct,
|
|
14
|
+
* according to recent TypeScript, Error gives more developer convenience.
|
|
15
|
+
* In our code we NEVER throw non-errors.
|
|
16
|
+
* Only possibility of non-error is in the 3rd-party library code, in these cases it
|
|
17
|
+
* can be manually cast to `unknown` for extra safety.
|
|
18
|
+
*
|
|
13
19
|
* @example
|
|
14
20
|
*
|
|
15
21
|
* const [err, v] = _try(() => someFunction())
|
|
16
22
|
* if (err) ...do something...
|
|
17
23
|
* v // go ahead and use v
|
|
18
24
|
*/
|
|
19
|
-
export declare function _try<ERR =
|
|
25
|
+
export declare function _try<ERR = Error, RETURN = void>(fn: () => RETURN): [err: ERR | null, value: RETURN];
|
|
20
26
|
/**
|
|
21
27
|
* Like _try, but for Promises.
|
|
22
28
|
*
|
|
23
29
|
* Also, intentionally types second return item as non-optional,
|
|
24
30
|
* but you should check for `err` presense first!
|
|
25
31
|
*/
|
|
26
|
-
export declare function pTry<ERR =
|
|
32
|
+
export declare function pTry<ERR = Error, RETURN = void>(promise: Promise<RETURN>): Promise<[err: ERR | null, value: Awaited<RETURN>]>;
|
|
27
33
|
/**
|
|
28
34
|
* It is thrown when Error was expected, but didn't happen
|
|
29
35
|
* ("pass" happened instead).
|
package/dist/error/try.js
CHANGED
|
@@ -7,11 +7,17 @@ const app_error_1 = require("./app.error");
|
|
|
7
7
|
* Allows to write shorter code that avoids `try/catch`.
|
|
8
8
|
* Useful e.g. in unit tests.
|
|
9
9
|
*
|
|
10
|
-
* Similar to
|
|
10
|
+
* Similar to pTry, but for sync functions.
|
|
11
11
|
*
|
|
12
12
|
* For convenience, second argument type is non-optional,
|
|
13
13
|
* so you can use it without `!`. But you SHOULD always check `if (err)` first!
|
|
14
14
|
*
|
|
15
|
+
* ERR is typed as Error, not `unknown`. While unknown would be more correct,
|
|
16
|
+
* according to recent TypeScript, Error gives more developer convenience.
|
|
17
|
+
* In our code we NEVER throw non-errors.
|
|
18
|
+
* Only possibility of non-error is in the 3rd-party library code, in these cases it
|
|
19
|
+
* can be manually cast to `unknown` for extra safety.
|
|
20
|
+
*
|
|
15
21
|
* @example
|
|
16
22
|
*
|
|
17
23
|
* const [err, v] = _try(() => someFunction())
|
|
@@ -49,7 +55,7 @@ exports.pTry = pTry;
|
|
|
49
55
|
*/
|
|
50
56
|
class UnexpectedPassError extends app_error_1.AppError {
|
|
51
57
|
constructor() {
|
|
52
|
-
super('
|
|
58
|
+
super('expected error was not thrown');
|
|
53
59
|
}
|
|
54
60
|
}
|
|
55
61
|
exports.UnexpectedPassError = UnexpectedPassError;
|
package/dist/error/tryCatch.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export interface TryCatchOptions {
|
|
|
5
5
|
* The value returned from the function will be returned from the wrapped method (!).
|
|
6
6
|
* onError function may be asynchronous.
|
|
7
7
|
*/
|
|
8
|
-
onError?: (err:
|
|
8
|
+
onError?: (err: Error) => any;
|
|
9
9
|
/**
|
|
10
10
|
* @default false
|
|
11
11
|
*/
|
package/dist/error/tryCatch.js
CHANGED
|
@@ -30,7 +30,7 @@ function _tryCatch(fn, opt = {}) {
|
|
|
30
30
|
}
|
|
31
31
|
if (onError) {
|
|
32
32
|
try {
|
|
33
|
-
return await onError(err); // eslint-disable-line @typescript-eslint/return-await
|
|
33
|
+
return await onError((0, index_1._anyToError)(err)); // eslint-disable-line @typescript-eslint/return-await
|
|
34
34
|
}
|
|
35
35
|
catch { }
|
|
36
36
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -45,14 +45,13 @@ export * from './promise/pProps';
|
|
|
45
45
|
import { pRetry, pRetryFn, PRetryOptions } from './promise/pRetry';
|
|
46
46
|
export * from './promise/pState';
|
|
47
47
|
import { pTimeout, pTimeoutFn, PTimeoutOptions } from './promise/pTimeout';
|
|
48
|
-
export * from './promise/pTuple';
|
|
49
48
|
export * from './string/case';
|
|
50
49
|
export * from './string/json.util';
|
|
51
50
|
export * from './string/string.util';
|
|
52
51
|
import { JsonStringifyFunction, StringifyAnyOptions, _stringifyAny } from './string/stringifyAny';
|
|
53
52
|
export * from './time/time.util';
|
|
54
53
|
import { Class, ConditionalExcept, ConditionalPick, Merge, Promisable, ReadonlyDeep, Simplify } from './typeFest';
|
|
55
|
-
import { AsyncMapper, AsyncPredicate, BaseDBEntity, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, Saved, Unsaved, BatchResult, InstanceId, IsoDate,
|
|
54
|
+
import { AsyncMapper, AsyncPredicate, BaseDBEntity, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, Saved, Unsaved, BatchResult, InstanceId, IsoDate, IsoDateString, IsoDateTimeString, KeyValueTuple, Mapper, ObjectMapper, ObjectPredicate, Predicate, PromiseMap, AnyObject, AnyFunction, Reviver, SavedDBEntity, StringMap, UnixTimestampNumber, UnixTimestamp, Integer, ValueOf, ValuesOf, AbortableMapper, AbortableAsyncPredicate, AbortableAsyncMapper, AbortablePredicate, END, SKIP, _noop, _objectKeys, _passNothingPredicate, _passthroughMapper, _passthroughPredicate, _passUndefinedMapper, _stringMapEntries, _stringMapValues } from './types';
|
|
56
55
|
export * from './unit/size.util';
|
|
57
56
|
import { is } from './vendor/is';
|
|
58
57
|
import { CommonLogLevel, CommonLogFunction, CommonLogger, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, CommonLogWithLevelFunction, commonLoggerCreate } from './log/commonLogger';
|
|
@@ -67,5 +66,5 @@ export * from './datetime/dateInterval';
|
|
|
67
66
|
import { LocalDateConfig, LocalDateUnit, Inclusiveness } from './datetime/localDate';
|
|
68
67
|
import { LocalTimeConfig, LocalTimeUnit, LocalTimeComponents } from './datetime/localTime';
|
|
69
68
|
import { DateIntervalConfig, DateIntervalString } from './datetime/dateInterval';
|
|
70
|
-
export type { DateIntervalConfig, DateIntervalString, LocalDateConfig, LocalDateUnit, Inclusiveness, LocalTimeConfig, LocalTimeUnit, LocalTimeComponents, AbortableMapper, AbortablePredicate, AbortableAsyncPredicate, AbortableAsyncMapper, PQueueCfg, MemoCache, AsyncMemoCache, PromiseDecoratorCfg, PromiseDecoratorResp, ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse, Admin401ErrorData, Admin403ErrorData, StringMap, PromiseMap, AnyObject, AnyFunction, ValuesOf, ValueOf, KeyValueTuple, ObjectMapper, ObjectPredicate, InstanceId, IsoDate,
|
|
69
|
+
export type { DateIntervalConfig, DateIntervalString, LocalDateConfig, LocalDateUnit, Inclusiveness, LocalTimeConfig, LocalTimeUnit, LocalTimeComponents, AbortableMapper, AbortablePredicate, AbortableAsyncPredicate, AbortableAsyncMapper, PQueueCfg, MemoCache, AsyncMemoCache, PromiseDecoratorCfg, PromiseDecoratorResp, ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse, Admin401ErrorData, Admin403ErrorData, StringMap, PromiseMap, AnyObject, AnyFunction, ValuesOf, ValueOf, KeyValueTuple, ObjectMapper, ObjectPredicate, InstanceId, IsoDate, IsoDateString, IsoDateTimeString, Reviver, PMapOptions, Mapper, AsyncMapper, Predicate, AsyncPredicate, BatchResult, DeferredPromise, PRetryOptions, PTimeoutOptions, TryCatchOptions, StringifyAnyOptions, JsonStringifyFunction, Merge, ReadonlyDeep, Promisable, Simplify, ConditionalPick, ConditionalExcept, Class, UnixTimestampNumber, UnixTimestamp, Integer, BaseDBEntity, SavedDBEntity, Saved, Unsaved, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, JsonSchema, JsonSchemaAny, JsonSchemaOneOf, JsonSchemaAllOf, JsonSchemaAnyOf, JsonSchemaNot, JsonSchemaRef, JsonSchemaConst, JsonSchemaEnum, JsonSchemaString, JsonSchemaNumber, JsonSchemaBoolean, JsonSchemaNull, JsonSchemaRootObject, JsonSchemaObject, JsonSchemaArray, JsonSchemaTuple, JsonSchemaBuilder, CommonLogLevel, CommonLogWithLevelFunction, CommonLogFunction, CommonLogger, };
|
|
71
70
|
export { is, _createPromiseDecorator, _stringMapValues, _stringMapEntries, _objectKeys, pMap, _passthroughMapper, _passUndefinedMapper, _passthroughPredicate, _passNothingPredicate, _noop, ErrorMode, pDefer, AggregatedError, pRetry, pRetryFn, pTimeout, pTimeoutFn, _tryCatch, _TryCatch, _stringifyAny, jsonSchema, JsonSchemaAnyBuilder, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, commonLoggerCreate, PQueue, END, SKIP, };
|
package/dist/index.js
CHANGED
|
@@ -59,7 +59,6 @@ tslib_1.__exportStar(require("./promise/pState"), exports);
|
|
|
59
59
|
const pTimeout_1 = require("./promise/pTimeout");
|
|
60
60
|
Object.defineProperty(exports, "pTimeout", { enumerable: true, get: function () { return pTimeout_1.pTimeout; } });
|
|
61
61
|
Object.defineProperty(exports, "pTimeoutFn", { enumerable: true, get: function () { return pTimeout_1.pTimeoutFn; } });
|
|
62
|
-
tslib_1.__exportStar(require("./promise/pTuple"), exports);
|
|
63
62
|
tslib_1.__exportStar(require("./string/case"), exports);
|
|
64
63
|
tslib_1.__exportStar(require("./string/json.util"), exports);
|
|
65
64
|
tslib_1.__exportStar(require("./string/string.util"), exports);
|
package/dist/types.d.ts
CHANGED
|
@@ -111,18 +111,26 @@ export interface InstanceId {
|
|
|
111
111
|
*
|
|
112
112
|
* @example '2019-06-21'
|
|
113
113
|
*/
|
|
114
|
+
export declare type IsoDateString = string;
|
|
115
|
+
/**
|
|
116
|
+
* @deprecated use IsoDateString
|
|
117
|
+
*/
|
|
114
118
|
export declare type IsoDate = string;
|
|
115
119
|
/**
|
|
116
120
|
* Interface explicitly states that the value is an ISO DateTime string (with time).
|
|
117
121
|
*
|
|
118
122
|
* @example '2019-06-21T05:21:73Z'
|
|
119
123
|
*/
|
|
120
|
-
export declare type
|
|
124
|
+
export declare type IsoDateTimeString = string;
|
|
121
125
|
/**
|
|
122
126
|
* Interface explicitly states that the value is a Unix timestamp (in seconds).
|
|
123
127
|
*
|
|
124
128
|
* @example 1628945450
|
|
125
129
|
*/
|
|
130
|
+
export declare type UnixTimestampNumber = number;
|
|
131
|
+
/**
|
|
132
|
+
* @deprecated use UnixTimestampNumber
|
|
133
|
+
*/
|
|
126
134
|
export declare type UnixTimestamp = number;
|
|
127
135
|
/**
|
|
128
136
|
* Same as `number`, but with semantic meaning that it's an Integer.
|
|
@@ -136,11 +144,11 @@ export interface SavedDBEntity<ID = string> {
|
|
|
136
144
|
/**
|
|
137
145
|
* unixTimestamp of when the entity was first created (in the DB).
|
|
138
146
|
*/
|
|
139
|
-
created:
|
|
147
|
+
created: UnixTimestampNumber;
|
|
140
148
|
/**
|
|
141
149
|
* unixTimestamp of when the entity was last updated (in the DB).
|
|
142
150
|
*/
|
|
143
|
-
updated:
|
|
151
|
+
updated: UnixTimestampNumber;
|
|
144
152
|
}
|
|
145
153
|
/**
|
|
146
154
|
* Base interface for any Entity that can be saved to DB.
|