@naturalcycles/js-lib 14.227.0 → 14.229.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.
@@ -145,3 +145,7 @@ export declare function localDateOrUndefined(d?: LocalDateInput | null): LocalDa
145
145
  * Creates a LocalDate from the input, unless it's falsy - then returns LocalDate.today.
146
146
  */
147
147
  export declare function localDateOrToday(d?: LocalDateInput | null): LocalDate;
148
+ /**
149
+ Convenience function to return current today's IsoDateString representation, e.g `2024-06-21`
150
+ */
151
+ export declare function todayIsoDateString(): IsoDateString;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.localDateOrToday = exports.localDateOrUndefined = exports.localDateToday = exports.localDate = exports.localDateRangeIterable = exports.localDateRange = exports.LocalDate = void 0;
3
+ exports.todayIsoDateString = exports.localDateOrToday = exports.localDateOrUndefined = exports.localDateToday = exports.localDate = exports.localDateRangeIterable = exports.localDateRange = exports.LocalDate = void 0;
4
4
  const assert_1 = require("../error/assert");
5
5
  const iterable2_1 = require("../iter/iterable2");
6
6
  const localTime_1 = require("./localTime");
@@ -496,3 +496,11 @@ function localDateOrToday(d) {
496
496
  return d ? LocalDate.of(d) : LocalDate.today();
497
497
  }
498
498
  exports.localDateOrToday = localDateOrToday;
499
+ /**
500
+ Convenience function to return current today's IsoDateString representation, e.g `2024-06-21`
501
+ */
502
+ function todayIsoDateString() {
503
+ // It was benchmarked to be faster than by concatenating individual Date components
504
+ return new Date().toISOString().slice(0, 10);
505
+ }
506
+ exports.todayIsoDateString = todayIsoDateString;
@@ -443,25 +443,8 @@ class LocalTime {
443
443
  return localDate_1.LocalDate.fromDate(this.$date);
444
444
  }
445
445
  toPretty(seconds = true) {
446
- const { year, month, day, hour, minute, second } = this.components();
447
- return ([
448
- String(year).padStart(4, '0'),
449
- String(month).padStart(2, '0'),
450
- String(day).padStart(2, '0'),
451
- ].join('-') +
452
- ' ' +
453
- [
454
- String(hour).padStart(2, '0'),
455
- String(minute).padStart(2, '0'),
456
- seconds && String(second).padStart(2, '0'),
457
- ]
458
- .filter(Boolean)
459
- .join(':'));
460
- // return this.$date
461
- // .toISOString()
462
- // .slice(0, seconds ? 19 : 16)
463
- // .split('T')
464
- // .join(' ')
446
+ const s = this.$date.toISOString();
447
+ return s.slice(0, 10) + ' ' + s.slice(11, seconds ? 19 : 16);
465
448
  }
466
449
  /**
467
450
  * Returns e.g: `1984-06-21T17:56:21`
@@ -473,27 +456,13 @@ class LocalTime {
473
456
  * Returns e.g: `1984-06-21`, only the date part of DateTime
474
457
  */
475
458
  toISODate() {
476
- const { year, month, day } = this.components();
477
- return [
478
- String(year).padStart(4, '0'),
479
- String(month).padStart(2, '0'),
480
- String(day).padStart(2, '0'),
481
- ].join('-');
482
- // return this.$date.toISOString().slice(0, 10)
459
+ return this.$date.toISOString().slice(0, 10);
483
460
  }
484
461
  /**
485
462
  * Returns e.g: `17:03:15` (or `17:03` with seconds=false)
486
463
  */
487
464
  toISOTime(seconds = true) {
488
- // return this.$date.toISOString().slice(11, seconds ? 19 : 16)
489
- const { hour, minute, second } = this.components();
490
- return [
491
- String(hour).padStart(2, '0'),
492
- String(minute).padStart(2, '0'),
493
- seconds && String(second).padStart(2, '0'),
494
- ]
495
- .filter(Boolean)
496
- .join(':');
465
+ return this.$date.toISOString().slice(11, seconds ? 19 : 16);
497
466
  }
498
467
  /**
499
468
  * Returns e.g: `19840621_1705`
@@ -8,6 +8,8 @@ const decorator_util_1 = require("./decorator.util");
8
8
  function _Timeout(opt) {
9
9
  return (target, key, descriptor) => {
10
10
  (0, assert_1._assert)(typeof descriptor.value === 'function', '@_Timeout can be applied only to methods');
11
+ if (!opt.timeout)
12
+ return descriptor;
11
13
  const originalFn = descriptor.value;
12
14
  const keyStr = String(key);
13
15
  descriptor.value = async function (...args) {
@@ -4,6 +4,8 @@ import type { AnyAsyncFunction, NumberOfMilliseconds } from '../types';
4
4
  export interface PTimeoutOptions {
5
5
  /**
6
6
  * Timeout in milliseconds.
7
+ *
8
+ * If 0 is passed - the function would be executed right away, with no timeout.
7
9
  */
8
10
  timeout: NumberOfMilliseconds;
9
11
  /**
@@ -11,6 +11,9 @@ const error_util_1 = require("../error/error.util");
11
11
  */
12
12
  function pTimeoutFn(fn, opt) {
13
13
  opt.name ||= fn.name;
14
+ if (!opt.timeout) {
15
+ return fn;
16
+ }
14
17
  return async function pTimeoutInternalFn(...args) {
15
18
  return await pTimeout(() => fn.apply(this, args), opt);
16
19
  };
@@ -23,6 +26,10 @@ exports.pTimeoutFn = pTimeoutFn;
23
26
  * If the Function rejects - passes this rejection further.
24
27
  */
25
28
  async function pTimeout(fn, opt) {
29
+ if (!opt.timeout) {
30
+ // short-circuit to direct execution if 0 timeout is passed
31
+ return await fn();
32
+ }
26
33
  const { timeout, name = fn.name || 'pTimeout function', onTimeout } = opt;
27
34
  const fakeError = opt.fakeError || new Error('TimeoutError');
28
35
  // eslint-disable-next-line no-async-promise-executor
@@ -486,3 +486,10 @@ export function localDateOrUndefined(d) {
486
486
  export function localDateOrToday(d) {
487
487
  return d ? LocalDate.of(d) : LocalDate.today();
488
488
  }
489
+ /**
490
+ Convenience function to return current today's IsoDateString representation, e.g `2024-06-21`
491
+ */
492
+ export function todayIsoDateString() {
493
+ // It was benchmarked to be faster than by concatenating individual Date components
494
+ return new Date().toISOString().slice(0, 10);
495
+ }
@@ -441,25 +441,8 @@ export class LocalTime {
441
441
  return LocalDate.fromDate(this.$date);
442
442
  }
443
443
  toPretty(seconds = true) {
444
- const { year, month, day, hour, minute, second } = this.components();
445
- return ([
446
- String(year).padStart(4, '0'),
447
- String(month).padStart(2, '0'),
448
- String(day).padStart(2, '0'),
449
- ].join('-') +
450
- ' ' +
451
- [
452
- String(hour).padStart(2, '0'),
453
- String(minute).padStart(2, '0'),
454
- seconds && String(second).padStart(2, '0'),
455
- ]
456
- .filter(Boolean)
457
- .join(':'));
458
- // return this.$date
459
- // .toISOString()
460
- // .slice(0, seconds ? 19 : 16)
461
- // .split('T')
462
- // .join(' ')
444
+ const s = this.$date.toISOString();
445
+ return s.slice(0, 10) + ' ' + s.slice(11, seconds ? 19 : 16);
463
446
  }
464
447
  /**
465
448
  * Returns e.g: `1984-06-21T17:56:21`
@@ -471,27 +454,13 @@ export class LocalTime {
471
454
  * Returns e.g: `1984-06-21`, only the date part of DateTime
472
455
  */
473
456
  toISODate() {
474
- const { year, month, day } = this.components();
475
- return [
476
- String(year).padStart(4, '0'),
477
- String(month).padStart(2, '0'),
478
- String(day).padStart(2, '0'),
479
- ].join('-');
480
- // return this.$date.toISOString().slice(0, 10)
457
+ return this.$date.toISOString().slice(0, 10);
481
458
  }
482
459
  /**
483
460
  * Returns e.g: `17:03:15` (or `17:03` with seconds=false)
484
461
  */
485
462
  toISOTime(seconds = true) {
486
- // return this.$date.toISOString().slice(11, seconds ? 19 : 16)
487
- const { hour, minute, second } = this.components();
488
- return [
489
- String(hour).padStart(2, '0'),
490
- String(minute).padStart(2, '0'),
491
- seconds && String(second).padStart(2, '0'),
492
- ]
493
- .filter(Boolean)
494
- .join(':');
463
+ return this.$date.toISOString().slice(11, seconds ? 19 : 16);
495
464
  }
496
465
  /**
497
466
  * Returns e.g: `19840621_1705`
@@ -5,6 +5,8 @@ import { _getMethodSignature } from './decorator.util';
5
5
  export function _Timeout(opt) {
6
6
  return (target, key, descriptor) => {
7
7
  _assert(typeof descriptor.value === 'function', '@_Timeout can be applied only to methods');
8
+ if (!opt.timeout)
9
+ return descriptor;
8
10
  const originalFn = descriptor.value;
9
11
  const keyStr = String(key);
10
12
  descriptor.value = async function (...args) {
@@ -8,6 +8,9 @@ import { _errorDataAppend, TimeoutError } from '../error/error.util';
8
8
  */
9
9
  export function pTimeoutFn(fn, opt) {
10
10
  opt.name || (opt.name = fn.name);
11
+ if (!opt.timeout) {
12
+ return fn;
13
+ }
11
14
  return async function pTimeoutInternalFn(...args) {
12
15
  return await pTimeout(() => fn.apply(this, args), opt);
13
16
  };
@@ -19,6 +22,10 @@ export function pTimeoutFn(fn, opt) {
19
22
  * If the Function rejects - passes this rejection further.
20
23
  */
21
24
  export async function pTimeout(fn, opt) {
25
+ if (!opt.timeout) {
26
+ // short-circuit to direct execution if 0 timeout is passed
27
+ return await fn();
28
+ }
22
29
  const { timeout, name = fn.name || 'pTimeout function', onTimeout } = opt;
23
30
  const fakeError = opt.fakeError || new Error('TimeoutError');
24
31
  // eslint-disable-next-line no-async-promise-executor
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.227.0",
3
+ "version": "14.229.0",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -602,3 +602,11 @@ export function localDateOrUndefined(d?: LocalDateInput | null): LocalDate | und
602
602
  export function localDateOrToday(d?: LocalDateInput | null): LocalDate {
603
603
  return d ? LocalDate.of(d) : LocalDate.today()
604
604
  }
605
+
606
+ /**
607
+ Convenience function to return current today's IsoDateString representation, e.g `2024-06-21`
608
+ */
609
+ export function todayIsoDateString(): IsoDateString {
610
+ // It was benchmarked to be faster than by concatenating individual Date components
611
+ return new Date().toISOString().slice(0, 10)
612
+ }
@@ -541,29 +541,8 @@ export class LocalTime {
541
541
  }
542
542
 
543
543
  toPretty(seconds = true): IsoDateTimeString {
544
- const { year, month, day, hour, minute, second } = this.components()
545
-
546
- return (
547
- [
548
- String(year).padStart(4, '0'),
549
- String(month).padStart(2, '0'),
550
- String(day).padStart(2, '0'),
551
- ].join('-') +
552
- ' ' +
553
- [
554
- String(hour).padStart(2, '0'),
555
- String(minute).padStart(2, '0'),
556
- seconds && String(second).padStart(2, '0'),
557
- ]
558
- .filter(Boolean)
559
- .join(':')
560
- )
561
-
562
- // return this.$date
563
- // .toISOString()
564
- // .slice(0, seconds ? 19 : 16)
565
- // .split('T')
566
- // .join(' ')
544
+ const s = this.$date.toISOString()
545
+ return s.slice(0, 10) + ' ' + s.slice(11, seconds ? 19 : 16)
567
546
  }
568
547
 
569
548
  /**
@@ -577,31 +556,14 @@ export class LocalTime {
577
556
  * Returns e.g: `1984-06-21`, only the date part of DateTime
578
557
  */
579
558
  toISODate(): IsoDateString {
580
- const { year, month, day } = this.components()
581
-
582
- return [
583
- String(year).padStart(4, '0'),
584
- String(month).padStart(2, '0'),
585
- String(day).padStart(2, '0'),
586
- ].join('-')
587
-
588
- // return this.$date.toISOString().slice(0, 10)
559
+ return this.$date.toISOString().slice(0, 10)
589
560
  }
590
561
 
591
562
  /**
592
563
  * Returns e.g: `17:03:15` (or `17:03` with seconds=false)
593
564
  */
594
565
  toISOTime(seconds = true): string {
595
- // return this.$date.toISOString().slice(11, seconds ? 19 : 16)
596
- const { hour, minute, second } = this.components()
597
-
598
- return [
599
- String(hour).padStart(2, '0'),
600
- String(minute).padStart(2, '0'),
601
- seconds && String(second).padStart(2, '0'),
602
- ]
603
- .filter(Boolean)
604
- .join(':')
566
+ return this.$date.toISOString().slice(11, seconds ? 19 : 16)
605
567
  }
606
568
 
607
569
  /**
@@ -8,6 +8,8 @@ export function _Timeout(opt: PTimeoutOptions): MethodDecorator {
8
8
  return (target, key, descriptor) => {
9
9
  _assert(typeof descriptor.value === 'function', '@_Timeout can be applied only to methods')
10
10
 
11
+ if (!opt.timeout) return descriptor
12
+
11
13
  const originalFn = descriptor.value
12
14
  const keyStr = String(key)
13
15
 
@@ -5,6 +5,8 @@ import type { AnyAsyncFunction, NumberOfMilliseconds } from '../types'
5
5
  export interface PTimeoutOptions {
6
6
  /**
7
7
  * Timeout in milliseconds.
8
+ *
9
+ * If 0 is passed - the function would be executed right away, with no timeout.
8
10
  */
9
11
  timeout: NumberOfMilliseconds
10
12
 
@@ -46,6 +48,10 @@ export interface PTimeoutOptions {
46
48
  export function pTimeoutFn<T extends AnyAsyncFunction>(fn: T, opt: PTimeoutOptions): T {
47
49
  opt.name ||= fn.name
48
50
 
51
+ if (!opt.timeout) {
52
+ return fn
53
+ }
54
+
49
55
  return async function pTimeoutInternalFn(this: any, ...args: any[]) {
50
56
  return await pTimeout(() => fn.apply(this, args), opt)
51
57
  } as T
@@ -58,6 +64,11 @@ export function pTimeoutFn<T extends AnyAsyncFunction>(fn: T, opt: PTimeoutOptio
58
64
  * If the Function rejects - passes this rejection further.
59
65
  */
60
66
  export async function pTimeout<T>(fn: AnyAsyncFunction<T>, opt: PTimeoutOptions): Promise<T> {
67
+ if (!opt.timeout) {
68
+ // short-circuit to direct execution if 0 timeout is passed
69
+ return await fn()
70
+ }
71
+
61
72
  const { timeout, name = fn.name || 'pTimeout function', onTimeout } = opt
62
73
  const fakeError = opt.fakeError || new Error('TimeoutError')
63
74