@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
|
@@ -3,14 +3,15 @@ import { Sequence } from '../seq/seq';
|
|
|
3
3
|
import { END } from '../types';
|
|
4
4
|
import { LocalTime } from './localTime';
|
|
5
5
|
const m31 = new Set([1, 3, 5, 7, 8, 10, 12]);
|
|
6
|
+
/* eslint-disable no-dupe-class-members */
|
|
6
7
|
/**
|
|
7
8
|
* @experimental
|
|
8
9
|
*/
|
|
9
10
|
export class LocalDate {
|
|
10
|
-
constructor(year, month, day) {
|
|
11
|
-
this
|
|
12
|
-
this
|
|
13
|
-
this
|
|
11
|
+
constructor($year, $month, $day) {
|
|
12
|
+
this.$year = $year;
|
|
13
|
+
this.$month = $month;
|
|
14
|
+
this.$day = $day;
|
|
14
15
|
}
|
|
15
16
|
static create(year, month, day) {
|
|
16
17
|
return new LocalDate(year, month, day);
|
|
@@ -114,9 +115,34 @@ export class LocalDate {
|
|
|
114
115
|
static rangeInclString(minIncl, maxIncl, step = 1, stepUnit = 'day') {
|
|
115
116
|
return LocalDate.range(minIncl, LocalDate.of(maxIncl).add(1, stepUnit), step, stepUnit).map(ld => ld.toString());
|
|
116
117
|
}
|
|
118
|
+
get(unit) {
|
|
119
|
+
return unit === 'year' ? this.$year : unit === 'month' ? this.$month : this.$day;
|
|
120
|
+
}
|
|
121
|
+
set(unit, v, mutate = false) {
|
|
122
|
+
const t = mutate ? this : this.clone();
|
|
123
|
+
if (unit === 'year') {
|
|
124
|
+
t.$year = v;
|
|
125
|
+
}
|
|
126
|
+
else if (unit === 'month') {
|
|
127
|
+
t.$month = v;
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
t.$day = v;
|
|
131
|
+
}
|
|
132
|
+
return t;
|
|
133
|
+
}
|
|
134
|
+
year(v) {
|
|
135
|
+
return v === undefined ? this.$year : this.set('year', v);
|
|
136
|
+
}
|
|
137
|
+
month(v) {
|
|
138
|
+
return v === undefined ? this.$month : this.set('month', v);
|
|
139
|
+
}
|
|
140
|
+
day(v) {
|
|
141
|
+
return v === undefined ? this.$day : this.set('day', v);
|
|
142
|
+
}
|
|
117
143
|
isSame(d) {
|
|
118
144
|
d = LocalDate.of(d);
|
|
119
|
-
return this
|
|
145
|
+
return this.$day === d.$day && this.$month === d.$month && this.$year === d.$year;
|
|
120
146
|
}
|
|
121
147
|
isBefore(d) {
|
|
122
148
|
return this.cmp(d) === -1;
|
|
@@ -146,17 +172,17 @@ export class LocalDate {
|
|
|
146
172
|
*/
|
|
147
173
|
cmp(d) {
|
|
148
174
|
d = LocalDate.of(d);
|
|
149
|
-
if (this
|
|
175
|
+
if (this.$year < d.$year)
|
|
150
176
|
return -1;
|
|
151
|
-
if (this
|
|
177
|
+
if (this.$year > d.$year)
|
|
152
178
|
return 1;
|
|
153
|
-
if (this
|
|
179
|
+
if (this.$month < d.$month)
|
|
154
180
|
return -1;
|
|
155
|
-
if (this
|
|
181
|
+
if (this.$month > d.$month)
|
|
156
182
|
return 1;
|
|
157
|
-
if (this
|
|
183
|
+
if (this.$day < d.$day)
|
|
158
184
|
return -1;
|
|
159
|
-
if (this
|
|
185
|
+
if (this.$day > d.$day)
|
|
160
186
|
return 1;
|
|
161
187
|
return 0;
|
|
162
188
|
}
|
|
@@ -174,82 +200,82 @@ export class LocalDate {
|
|
|
174
200
|
diff(d, unit) {
|
|
175
201
|
d = LocalDate.of(d);
|
|
176
202
|
if (unit === 'year') {
|
|
177
|
-
return this
|
|
203
|
+
return this.$year - d.$year;
|
|
178
204
|
}
|
|
179
205
|
if (unit === 'month') {
|
|
180
|
-
return (this
|
|
206
|
+
return (this.$year - d.$year) * 12 + (this.$month - d.$month);
|
|
181
207
|
}
|
|
182
208
|
// unit is 'day'
|
|
183
|
-
let days = this
|
|
184
|
-
if (d
|
|
185
|
-
for (let year = d
|
|
209
|
+
let days = this.$day - d.$day;
|
|
210
|
+
if (d.$year < this.$year) {
|
|
211
|
+
for (let year = d.$year; year < this.$year; year++) {
|
|
186
212
|
days += LocalDate.getYearLength(year);
|
|
187
213
|
}
|
|
188
214
|
}
|
|
189
|
-
else if (this
|
|
190
|
-
for (let year = this
|
|
215
|
+
else if (this.$year < d.$year) {
|
|
216
|
+
for (let year = this.$year; year < d.$year; year++) {
|
|
191
217
|
days -= LocalDate.getYearLength(year);
|
|
192
218
|
}
|
|
193
219
|
}
|
|
194
|
-
if (d
|
|
195
|
-
for (let month = d
|
|
196
|
-
days += LocalDate.getMonthLength(this
|
|
220
|
+
if (d.$month < this.$month) {
|
|
221
|
+
for (let month = d.$month; month < this.$month; month++) {
|
|
222
|
+
days += LocalDate.getMonthLength(this.$year, month);
|
|
197
223
|
}
|
|
198
224
|
}
|
|
199
|
-
else if (this
|
|
200
|
-
for (let month = this
|
|
201
|
-
days -= LocalDate.getMonthLength(d
|
|
225
|
+
else if (this.$month < d.$month) {
|
|
226
|
+
for (let month = this.$month; month < d.$month; month++) {
|
|
227
|
+
days -= LocalDate.getMonthLength(d.$year, month);
|
|
202
228
|
}
|
|
203
229
|
}
|
|
204
230
|
return days;
|
|
205
231
|
}
|
|
206
232
|
add(num, unit, mutate = false) {
|
|
207
|
-
let { day, month, year } = this;
|
|
233
|
+
let { $day, $month, $year } = this;
|
|
208
234
|
if (unit === 'day') {
|
|
209
|
-
day += num;
|
|
235
|
+
$day += num;
|
|
210
236
|
}
|
|
211
237
|
else if (unit === 'month') {
|
|
212
|
-
month += num;
|
|
238
|
+
$month += num;
|
|
213
239
|
}
|
|
214
240
|
else if (unit === 'year') {
|
|
215
|
-
year += num;
|
|
241
|
+
$year += num;
|
|
216
242
|
}
|
|
217
243
|
// check day overflow
|
|
218
|
-
let monLen = LocalDate.getMonthLength(year, month);
|
|
219
|
-
while (day > monLen) {
|
|
220
|
-
day -= monLen;
|
|
221
|
-
month += 1;
|
|
222
|
-
if (month > 12) {
|
|
223
|
-
year += 1;
|
|
224
|
-
month -= 12;
|
|
244
|
+
let monLen = LocalDate.getMonthLength($year, $month);
|
|
245
|
+
while ($day > monLen) {
|
|
246
|
+
$day -= monLen;
|
|
247
|
+
$month += 1;
|
|
248
|
+
if ($month > 12) {
|
|
249
|
+
$year += 1;
|
|
250
|
+
$month -= 12;
|
|
225
251
|
}
|
|
226
|
-
monLen = LocalDate.getMonthLength(year, month);
|
|
252
|
+
monLen = LocalDate.getMonthLength($year, $month);
|
|
227
253
|
}
|
|
228
|
-
while (day < 1) {
|
|
229
|
-
day += monLen;
|
|
230
|
-
month -= 1;
|
|
231
|
-
if (month < 1) {
|
|
232
|
-
year -= 1;
|
|
233
|
-
month += 12;
|
|
254
|
+
while ($day < 1) {
|
|
255
|
+
$day += monLen;
|
|
256
|
+
$month -= 1;
|
|
257
|
+
if ($month < 1) {
|
|
258
|
+
$year -= 1;
|
|
259
|
+
$month += 12;
|
|
234
260
|
}
|
|
235
|
-
monLen = LocalDate.getMonthLength(year, month);
|
|
261
|
+
monLen = LocalDate.getMonthLength($year, $month);
|
|
236
262
|
}
|
|
237
263
|
// check month overflow
|
|
238
|
-
while (month > 12) {
|
|
239
|
-
year += 1;
|
|
240
|
-
month -= 12;
|
|
264
|
+
while ($month > 12) {
|
|
265
|
+
$year += 1;
|
|
266
|
+
$month -= 12;
|
|
241
267
|
}
|
|
242
|
-
while (month < 1) {
|
|
243
|
-
year -= 1;
|
|
244
|
-
month += 12;
|
|
268
|
+
while ($month < 1) {
|
|
269
|
+
$year -= 1;
|
|
270
|
+
$month += 12;
|
|
245
271
|
}
|
|
246
272
|
if (mutate) {
|
|
247
|
-
this
|
|
248
|
-
this
|
|
249
|
-
this
|
|
273
|
+
this.$year = $year;
|
|
274
|
+
this.$month = $month;
|
|
275
|
+
this.$day = $day;
|
|
250
276
|
return this;
|
|
251
277
|
}
|
|
252
|
-
return new LocalDate(year, month, day);
|
|
278
|
+
return new LocalDate($year, $month, $day);
|
|
253
279
|
}
|
|
254
280
|
subtract(num, unit, mutate = false) {
|
|
255
281
|
return this.add(-num, unit, mutate);
|
|
@@ -258,17 +284,17 @@ export class LocalDate {
|
|
|
258
284
|
if (unit === 'day')
|
|
259
285
|
return this;
|
|
260
286
|
if (unit === 'month')
|
|
261
|
-
return LocalDate.create(this
|
|
287
|
+
return LocalDate.create(this.$year, this.$month, 1);
|
|
262
288
|
// year
|
|
263
|
-
return LocalDate.create(this
|
|
289
|
+
return LocalDate.create(this.$year, 1, 1);
|
|
264
290
|
}
|
|
265
291
|
endOf(unit) {
|
|
266
292
|
if (unit === 'day')
|
|
267
293
|
return this;
|
|
268
294
|
if (unit === 'month')
|
|
269
|
-
return LocalDate.create(this
|
|
295
|
+
return LocalDate.create(this.$year, this.$month, LocalDate.getMonthLength(this.$year, this.$month));
|
|
270
296
|
// year
|
|
271
|
-
return LocalDate.create(this
|
|
297
|
+
return LocalDate.create(this.$year, 12, 31);
|
|
272
298
|
}
|
|
273
299
|
static getYearLength(year) {
|
|
274
300
|
return this.isLeapYear(year) ? 366 : 365;
|
|
@@ -286,7 +312,7 @@ export class LocalDate {
|
|
|
286
312
|
return year % 400 === 0;
|
|
287
313
|
}
|
|
288
314
|
clone() {
|
|
289
|
-
return new LocalDate(this
|
|
315
|
+
return new LocalDate(this.$year, this.$month, this.$day);
|
|
290
316
|
}
|
|
291
317
|
/**
|
|
292
318
|
* Converts LocalDate into instance of Date.
|
|
@@ -295,7 +321,7 @@ export class LocalDate {
|
|
|
295
321
|
* Timezone will match local timezone.
|
|
296
322
|
*/
|
|
297
323
|
toDate() {
|
|
298
|
-
return new Date(this
|
|
324
|
+
return new Date(this.$year, this.$month - 1, this.$day);
|
|
299
325
|
}
|
|
300
326
|
toLocalTime() {
|
|
301
327
|
return LocalTime.of(this.toDate());
|
|
@@ -305,16 +331,16 @@ export class LocalDate {
|
|
|
305
331
|
}
|
|
306
332
|
toString() {
|
|
307
333
|
return [
|
|
308
|
-
String(this
|
|
309
|
-
String(this
|
|
310
|
-
String(this
|
|
334
|
+
String(this.$year).padStart(4, '0'),
|
|
335
|
+
String(this.$month).padStart(2, '0'),
|
|
336
|
+
String(this.$day).padStart(2, '0'),
|
|
311
337
|
].join('-');
|
|
312
338
|
}
|
|
313
339
|
toStringCompact() {
|
|
314
340
|
return [
|
|
315
|
-
String(this
|
|
316
|
-
String(this
|
|
317
|
-
String(this
|
|
341
|
+
String(this.$year).padStart(4, '0'),
|
|
342
|
+
String(this.$month).padStart(2, '0'),
|
|
343
|
+
String(this.$day).padStart(2, '0'),
|
|
318
344
|
].join('');
|
|
319
345
|
}
|
|
320
346
|
// May be not optimal, as LocalTime better suits it
|
|
@@ -2,18 +2,6 @@ import { _assert } from '../error/assert';
|
|
|
2
2
|
import { _ms } from '../time/time.util';
|
|
3
3
|
import { LocalDate } from './localDate';
|
|
4
4
|
/* eslint-disable no-dupe-class-members */
|
|
5
|
-
// Design choices:
|
|
6
|
-
// No milliseconds
|
|
7
|
-
// No timezone support, ISO8601 is parsed as LocalDateTime, discarding Timezone information
|
|
8
|
-
// Formats as unix timestamp, ISO8601 or "pretty string"
|
|
9
|
-
// toString and .toJSON formats as unix timestamp
|
|
10
|
-
// No "unixMillis", just pure unixtimestamp
|
|
11
|
-
// .valueOf returns unix timestamp (no millis)
|
|
12
|
-
// Prevents dayjs(undefined) being dayjs.now()
|
|
13
|
-
// Validates on parse, throws if invalid. Doesn't allow invalid objects
|
|
14
|
-
// No arbitrary .format('') (which is slow to parse) vs well-defined (opinionated) formats
|
|
15
|
-
// Separate LocalTime, powered by native js Date, and LocalDate - a smaller functionality class when
|
|
16
|
-
// you only need "whole dates", no time, no timezone worry
|
|
17
5
|
/**
|
|
18
6
|
* @experimental
|
|
19
7
|
*/
|
|
@@ -127,7 +115,7 @@ export class LocalTime {
|
|
|
127
115
|
month(v) {
|
|
128
116
|
return v === undefined ? this.get('month') : this.set('month', v);
|
|
129
117
|
}
|
|
130
|
-
|
|
118
|
+
day(v) {
|
|
131
119
|
return v === undefined ? this.get('day') : this.set('day', v);
|
|
132
120
|
}
|
|
133
121
|
hour(v) {
|
package/dist-esm/error/try.js
CHANGED
|
@@ -4,11 +4,17 @@ import { AppError } from './app.error';
|
|
|
4
4
|
* Allows to write shorter code that avoids `try/catch`.
|
|
5
5
|
* Useful e.g. in unit tests.
|
|
6
6
|
*
|
|
7
|
-
* Similar to
|
|
7
|
+
* Similar to pTry, but for sync functions.
|
|
8
8
|
*
|
|
9
9
|
* For convenience, second argument type is non-optional,
|
|
10
10
|
* so you can use it without `!`. But you SHOULD always check `if (err)` first!
|
|
11
11
|
*
|
|
12
|
+
* ERR is typed as Error, not `unknown`. While unknown would be more correct,
|
|
13
|
+
* according to recent TypeScript, Error gives more developer convenience.
|
|
14
|
+
* In our code we NEVER throw non-errors.
|
|
15
|
+
* Only possibility of non-error is in the 3rd-party library code, in these cases it
|
|
16
|
+
* can be manually cast to `unknown` for extra safety.
|
|
17
|
+
*
|
|
12
18
|
* @example
|
|
13
19
|
*
|
|
14
20
|
* const [err, v] = _try(() => someFunction())
|
|
@@ -44,7 +50,7 @@ export async function pTry(promise) {
|
|
|
44
50
|
*/
|
|
45
51
|
export class UnexpectedPassError extends AppError {
|
|
46
52
|
constructor() {
|
|
47
|
-
super('
|
|
53
|
+
super('expected error was not thrown');
|
|
48
54
|
}
|
|
49
55
|
}
|
|
50
56
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _since, _stringifyAny } from '../index';
|
|
1
|
+
import { _anyToError, _since, _stringifyAny } from '../index';
|
|
2
2
|
/**
|
|
3
3
|
* Decorates a function with "try/catch", so it'll never reject/throw.
|
|
4
4
|
* Only applies to async functions (or, turns sync function into async).
|
|
@@ -27,7 +27,7 @@ export function _tryCatch(fn, opt = {}) {
|
|
|
27
27
|
}
|
|
28
28
|
if (onError) {
|
|
29
29
|
try {
|
|
30
|
-
return await onError(err); // eslint-disable-line @typescript-eslint/return-await
|
|
30
|
+
return await onError(_anyToError(err)); // eslint-disable-line @typescript-eslint/return-await
|
|
31
31
|
}
|
|
32
32
|
catch (_a) { }
|
|
33
33
|
}
|
package/dist-esm/index.js
CHANGED
|
@@ -42,7 +42,6 @@ export * from './promise/pProps';
|
|
|
42
42
|
import { pRetry, pRetryFn } from './promise/pRetry';
|
|
43
43
|
export * from './promise/pState';
|
|
44
44
|
import { pTimeout, pTimeoutFn } from './promise/pTimeout';
|
|
45
|
-
export * from './promise/pTuple';
|
|
46
45
|
export * from './string/case';
|
|
47
46
|
export * from './string/json.util';
|
|
48
47
|
export * from './string/string.util';
|