@naturalcycles/js-lib 14.94.0 → 14.96.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.
@@ -1,5 +1,6 @@
1
1
  import { LocalDate, LocalDateConfig } from './localDate';
2
2
  export declare type DateIntervalConfig = DateInterval | string;
3
+ export declare type DateIntervalString = string;
3
4
  /**
4
5
  * Class that supports ISO8601 "Time interval" standard that looks like `2022-02-24/2022-03-30`.
5
6
  *
@@ -33,6 +34,6 @@ export declare class DateInterval {
33
34
  * Ranges are INCLUSIVE.
34
35
  */
35
36
  getDays(): LocalDate[];
36
- toString(): string;
37
- toJSON(): string;
37
+ toString(): DateIntervalString;
38
+ toJSON(): DateIntervalString;
38
39
  }
@@ -24,8 +24,8 @@ export declare class LocalDate {
24
24
  /**
25
25
  * Returns null if invalid.
26
26
  */
27
- static parseOrNull(d: LocalDateConfig): LocalDate | null;
28
- static isValid(iso: string): boolean;
27
+ static parseOrNull(d: LocalDateConfig | undefined | null): LocalDate | null;
28
+ static isValid(iso: string | undefined | null): boolean;
29
29
  static today(): LocalDate;
30
30
  static todayUTC(): LocalDate;
31
31
  static sort(items: LocalDate[], mutate?: boolean, descending?: boolean): LocalDate[];
@@ -46,6 +46,8 @@ class LocalDate {
46
46
  * Returns null if invalid.
47
47
  */
48
48
  static parseOrNull(d) {
49
+ if (!d)
50
+ return null;
49
51
  if (d instanceof LocalDate)
50
52
  return d;
51
53
  // todo: explore more performant options
@@ -27,8 +27,8 @@ export declare class LocalTime {
27
27
  /**
28
28
  * Returns null if invalid
29
29
  */
30
- static parseOrNull(d: LocalTimeConfig): LocalTime | null;
31
- static isValid(d: LocalTimeConfig): boolean;
30
+ static parseOrNull(d: LocalTimeConfig | undefined | null): LocalTime | null;
31
+ static isValid(d: LocalTimeConfig | undefined | null): boolean;
32
32
  static now(): LocalTime;
33
33
  static fromComponents(c: {
34
34
  year: number;
@@ -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
  */
@@ -48,6 +36,8 @@ class LocalTime {
48
36
  * Returns null if invalid
49
37
  */
50
38
  static parseOrNull(d) {
39
+ if (!d)
40
+ return null;
51
41
  if (d instanceof LocalTime)
52
42
  return d;
53
43
  let date;
@@ -10,7 +10,7 @@ function _debounce(func, wait, opt = {}) {
10
10
  let lastInvokeTime = 0;
11
11
  const maxing = 'maxWait' in opt;
12
12
  const { leading = false, trailing = true } = opt;
13
- const maxWait = maxing ? Math.max(+opt.maxWait || 0, wait) : opt.maxWait;
13
+ const maxWait = maxing ? Math.max(Number(opt.maxWait) || 0, wait) : opt.maxWait;
14
14
  function invokeFunc(time) {
15
15
  const args = lastArgs;
16
16
  const thisArg = lastThis;
@@ -5,7 +5,7 @@ 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 pTuple, but for sync functions.
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!
package/dist/error/try.js CHANGED
@@ -7,7 +7,7 @@ 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 pTuple, but for sync functions.
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!
package/dist/index.d.ts CHANGED
@@ -45,7 +45,6 @@ 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';
@@ -66,6 +65,6 @@ export * from './datetime/localTime';
66
65
  export * from './datetime/dateInterval';
67
66
  import { LocalDateConfig, LocalDateUnit, Inclusiveness } from './datetime/localDate';
68
67
  import { LocalTimeConfig, LocalTimeUnit, LocalTimeComponents } from './datetime/localTime';
69
- import { DateIntervalConfig } from './datetime/dateInterval';
70
- export type { DateIntervalConfig, 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, IsoDateTime, Reviver, PMapOptions, Mapper, AsyncMapper, Predicate, AsyncPredicate, BatchResult, DeferredPromise, PRetryOptions, PTimeoutOptions, TryCatchOptions, StringifyAnyOptions, JsonStringifyFunction, Merge, ReadonlyDeep, Promisable, Simplify, ConditionalPick, ConditionalExcept, Class, 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, };
68
+ import { DateIntervalConfig, DateIntervalString } from './datetime/dateInterval';
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, IsoDateTime, Reviver, PMapOptions, Mapper, AsyncMapper, Predicate, AsyncPredicate, BatchResult, DeferredPromise, PRetryOptions, PTimeoutOptions, TryCatchOptions, StringifyAnyOptions, JsonStringifyFunction, Merge, ReadonlyDeep, Promisable, Simplify, ConditionalPick, ConditionalExcept, Class, 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);
@@ -17,7 +17,7 @@ function _safeJsonStringify(obj, replacer, spaces, cycleReplacer) {
17
17
  }
18
18
  }
19
19
  exports._safeJsonStringify = _safeJsonStringify;
20
- /* eslint-disable @typescript-eslint/no-unused-expressions, no-bitwise */
20
+ /* eslint-disable @typescript-eslint/no-unused-expressions, no-bitwise, no-implicit-coercion */
21
21
  function serializer(replacer, cycleReplacer) {
22
22
  const stack = [];
23
23
  const keys = [];
@@ -43,6 +43,8 @@ export class LocalDate {
43
43
  * Returns null if invalid.
44
44
  */
45
45
  static parseOrNull(d) {
46
+ if (!d)
47
+ return null;
46
48
  if (d instanceof LocalDate)
47
49
  return d;
48
50
  // todo: explore more performant options
@@ -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
  */
@@ -45,6 +33,8 @@ export class LocalTime {
45
33
  * Returns null if invalid
46
34
  */
47
35
  static parseOrNull(d) {
36
+ if (!d)
37
+ return null;
48
38
  if (d instanceof LocalTime)
49
39
  return d;
50
40
  let date;
@@ -7,7 +7,7 @@ export function _debounce(func, wait, opt = {}) {
7
7
  let lastInvokeTime = 0;
8
8
  const maxing = 'maxWait' in opt;
9
9
  const { leading = false, trailing = true } = opt;
10
- const maxWait = maxing ? Math.max(+opt.maxWait || 0, wait) : opt.maxWait;
10
+ const maxWait = maxing ? Math.max(Number(opt.maxWait) || 0, wait) : opt.maxWait;
11
11
  function invokeFunc(time) {
12
12
  const args = lastArgs;
13
13
  const thisArg = lastThis;
@@ -4,7 +4,7 @@ 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 pTuple, but for sync functions.
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!
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';
@@ -13,7 +13,7 @@ export function _safeJsonStringify(obj, replacer, spaces, cycleReplacer) {
13
13
  return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces);
14
14
  }
15
15
  }
16
- /* eslint-disable @typescript-eslint/no-unused-expressions, no-bitwise */
16
+ /* eslint-disable @typescript-eslint/no-unused-expressions, no-bitwise, no-implicit-coercion */
17
17
  function serializer(replacer, cycleReplacer) {
18
18
  const stack = [];
19
19
  const keys = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.94.0",
3
+ "version": "14.96.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -1,6 +1,7 @@
1
1
  import { LocalDate, LocalDateConfig } from './localDate'
2
2
 
3
3
  export type DateIntervalConfig = DateInterval | string
4
+ export type DateIntervalString = string
4
5
 
5
6
  /**
6
7
  * Class that supports ISO8601 "Time interval" standard that looks like `2022-02-24/2022-03-30`.
@@ -81,11 +82,11 @@ export class DateInterval {
81
82
  return days
82
83
  }
83
84
 
84
- toString(): string {
85
+ toString(): DateIntervalString {
85
86
  return [this.start, this.end].join('/')
86
87
  }
87
88
 
88
- toJSON(): string {
89
+ toJSON(): DateIntervalString {
89
90
  return this.toString()
90
91
  }
91
92
  }
@@ -55,7 +55,8 @@ export class LocalDate {
55
55
  /**
56
56
  * Returns null if invalid.
57
57
  */
58
- static parseOrNull(d: LocalDateConfig): LocalDate | null {
58
+ static parseOrNull(d: LocalDateConfig | undefined | null): LocalDate | null {
59
+ if (!d) return null
59
60
  if (d instanceof LocalDate) return d
60
61
 
61
62
  // todo: explore more performant options
@@ -76,7 +77,7 @@ export class LocalDate {
76
77
  return new LocalDate(year, month, day)
77
78
  }
78
79
 
79
- static isValid(iso: string): boolean {
80
+ static isValid(iso: string | undefined | null): boolean {
80
81
  return this.parseOrNull(iso) !== null
81
82
  }
82
83
 
@@ -18,18 +18,6 @@ export interface LocalTimeComponents {
18
18
 
19
19
  /* eslint-disable no-dupe-class-members */
20
20
 
21
- // Design choices:
22
- // No milliseconds
23
- // No timezone support, ISO8601 is parsed as LocalDateTime, discarding Timezone information
24
- // Formats as unix timestamp, ISO8601 or "pretty string"
25
- // toString and .toJSON formats as unix timestamp
26
- // No "unixMillis", just pure unixtimestamp
27
- // .valueOf returns unix timestamp (no millis)
28
- // Prevents dayjs(undefined) being dayjs.now()
29
- // Validates on parse, throws if invalid. Doesn't allow invalid objects
30
- // No arbitrary .format('') (which is slow to parse) vs well-defined (opinionated) formats
31
- // Separate LocalTime, powered by native js Date, and LocalDate - a smaller functionality class when
32
- // you only need "whole dates", no time, no timezone worry
33
21
  /**
34
22
  * @experimental
35
23
  */
@@ -63,7 +51,8 @@ export class LocalTime {
63
51
  /**
64
52
  * Returns null if invalid
65
53
  */
66
- static parseOrNull(d: LocalTimeConfig): LocalTime | null {
54
+ static parseOrNull(d: LocalTimeConfig | undefined | null): LocalTime | null {
55
+ if (!d) return null
67
56
  if (d instanceof LocalTime) return d
68
57
 
69
58
  let date
@@ -89,7 +78,7 @@ export class LocalTime {
89
78
  return new LocalTime(date, false)
90
79
  }
91
80
 
92
- static isValid(d: LocalTimeConfig): boolean {
81
+ static isValid(d: LocalTimeConfig | undefined | null): boolean {
93
82
  return this.parseOrNull(d) !== null
94
83
  }
95
84
 
@@ -49,7 +49,7 @@ export function _debounce<T extends AnyFunction>(
49
49
  const maxing = 'maxWait' in opt
50
50
 
51
51
  const { leading = false, trailing = true } = opt
52
- const maxWait = maxing ? Math.max(+opt.maxWait! || 0, wait) : opt.maxWait
52
+ const maxWait = maxing ? Math.max(Number(opt.maxWait) || 0, wait) : opt.maxWait
53
53
 
54
54
  function invokeFunc(time: number) {
55
55
  const args = lastArgs
package/src/error/try.ts CHANGED
@@ -6,7 +6,7 @@ import { AppError } from './app.error'
6
6
  * Allows to write shorter code that avoids `try/catch`.
7
7
  * Useful e.g. in unit tests.
8
8
  *
9
- * Similar to pTuple, but for sync functions.
9
+ * Similar to pTry, but for sync functions.
10
10
  *
11
11
  * For convenience, second argument type is non-optional,
12
12
  * so you can use it without `!`. But you SHOULD always check `if (err)` first!
package/src/index.ts CHANGED
@@ -78,7 +78,6 @@ export * from './promise/pProps'
78
78
  import { pRetry, pRetryFn, PRetryOptions } from './promise/pRetry'
79
79
  export * from './promise/pState'
80
80
  import { pTimeout, pTimeoutFn, PTimeoutOptions } from './promise/pTimeout'
81
- export * from './promise/pTuple'
82
81
  export * from './string/case'
83
82
  export * from './string/json.util'
84
83
  export * from './string/string.util'
@@ -161,10 +160,11 @@ export * from './datetime/localTime'
161
160
  export * from './datetime/dateInterval'
162
161
  import { LocalDateConfig, LocalDateUnit, Inclusiveness } from './datetime/localDate'
163
162
  import { LocalTimeConfig, LocalTimeUnit, LocalTimeComponents } from './datetime/localTime'
164
- import { DateIntervalConfig } from './datetime/dateInterval'
163
+ import { DateIntervalConfig, DateIntervalString } from './datetime/dateInterval'
165
164
 
166
165
  export type {
167
166
  DateIntervalConfig,
167
+ DateIntervalString,
168
168
  LocalDateConfig,
169
169
  LocalDateUnit,
170
170
  Inclusiveness,
@@ -20,7 +20,7 @@ export function _safeJsonStringify(
20
20
  }
21
21
  }
22
22
 
23
- /* eslint-disable @typescript-eslint/no-unused-expressions, no-bitwise */
23
+ /* eslint-disable @typescript-eslint/no-unused-expressions, no-bitwise, no-implicit-coercion */
24
24
 
25
25
  function serializer(replacer?: Reviver, cycleReplacer?: Reviver): Reviver {
26
26
  const stack: any[] = []
@@ -1,7 +0,0 @@
1
- /**
2
- * Wraps async calls in try catch blocks
3
- * to simplify syntax.
4
- *
5
- * source: https://github.com/scopsy/await-to-js/blob/master/src/await-to-js.ts
6
- */
7
- export declare function pTuple<RETURN, ERR = Error>(promise: Promise<RETURN>): Promise<[ERR, undefined] | [null, RETURN]>;
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.pTuple = void 0;
4
- /**
5
- * Wraps async calls in try catch blocks
6
- * to simplify syntax.
7
- *
8
- * source: https://github.com/scopsy/await-to-js/blob/master/src/await-to-js.ts
9
- */
10
- async function pTuple(promise) {
11
- return promise.then(data => [null, data]).catch(err => [err, undefined]);
12
- }
13
- exports.pTuple = pTuple;
@@ -1,9 +0,0 @@
1
- /**
2
- * Wraps async calls in try catch blocks
3
- * to simplify syntax.
4
- *
5
- * source: https://github.com/scopsy/await-to-js/blob/master/src/await-to-js.ts
6
- */
7
- export async function pTuple(promise) {
8
- return promise.then(data => [null, data]).catch(err => [err, undefined]);
9
- }
@@ -1,11 +0,0 @@
1
- /**
2
- * Wraps async calls in try catch blocks
3
- * to simplify syntax.
4
- *
5
- * source: https://github.com/scopsy/await-to-js/blob/master/src/await-to-js.ts
6
- */
7
- export async function pTuple<RETURN, ERR = Error>(
8
- promise: Promise<RETURN>,
9
- ): Promise<[ERR, undefined] | [null, RETURN]> {
10
- return promise.then(data => [null, data] as [null, RETURN]).catch(err => [err as ERR, undefined])
11
- }