@naturalcycles/js-lib 14.190.0 → 14.192.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.
Files changed (44) hide show
  1. package/dist/array/range.d.ts +6 -0
  2. package/dist/array/range.js +16 -2
  3. package/dist/datetime/localDate.d.ts +5 -0
  4. package/dist/datetime/localDate.js +22 -11
  5. package/dist/decorators/logMethod.decorator.js +1 -1
  6. package/dist/error/assert.js +2 -6
  7. package/dist/error/error.util.d.ts +1 -1
  8. package/dist/error/error.util.js +2 -2
  9. package/dist/error/try.js +3 -3
  10. package/dist/http/fetcher.js +2 -2
  11. package/dist/index.d.ts +2 -2
  12. package/dist/index.js +2 -2
  13. package/dist/iter/iterable2.d.ts +21 -0
  14. package/dist/iter/iterable2.js +75 -0
  15. package/dist/string/{stringifyAny.d.ts → stringify.d.ts} +10 -6
  16. package/dist/string/{stringifyAny.js → stringify.js} +15 -11
  17. package/dist/zod/zod.util.js +2 -2
  18. package/dist-esm/array/range.js +14 -1
  19. package/dist-esm/datetime/localDate.js +22 -11
  20. package/dist-esm/decorators/logMethod.decorator.js +2 -2
  21. package/dist-esm/error/assert.js +3 -7
  22. package/dist-esm/error/error.util.js +3 -3
  23. package/dist-esm/error/try.js +3 -3
  24. package/dist-esm/http/fetcher.js +2 -2
  25. package/dist-esm/index.js +2 -2
  26. package/dist-esm/iter/iterable2.js +71 -0
  27. package/dist-esm/string/{stringifyAny.js → stringify.js} +13 -9
  28. package/dist-esm/zod/zod.util.js +2 -2
  29. package/package.json +1 -1
  30. package/src/array/range.ts +22 -0
  31. package/src/datetime/localDate.ts +30 -11
  32. package/src/decorators/logMethod.decorator.ts +2 -2
  33. package/src/error/assert.ts +3 -7
  34. package/src/error/error.util.ts +3 -3
  35. package/src/error/try.ts +3 -3
  36. package/src/http/fetcher.ts +2 -2
  37. package/src/index.ts +2 -2
  38. package/src/iter/iterable2.ts +74 -0
  39. package/src/string/{stringifyAny.ts → stringify.ts} +15 -10
  40. package/src/zod/zod.util.ts +2 -2
  41. package/dist/seq/seq.d.ts +0 -54
  42. package/dist/seq/seq.js +0 -257
  43. package/dist-esm/seq/seq.js +0 -251
  44. package/src/seq/seq.ts +0 -277
@@ -1,3 +1,4 @@
1
+ import { Iterable2 } from '../iter/iterable2';
1
2
  /**
2
3
  * Returns an array with ranges from `from` up to (but not including) `to`.
3
4
  *
@@ -10,3 +11,8 @@
10
11
  */
11
12
  export declare function _range(toExcl: number): number[];
12
13
  export declare function _range(fromIncl: number, toExcl: number, step?: number): number[];
14
+ /**
15
+ * Like _range, but returns an Iterable2.
16
+ */
17
+ export declare function _rangeIt(toExcl: number): Iterable2<number>;
18
+ export declare function _rangeIt(fromIncl: number, toExcl: number, step?: number): Iterable2<number>;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
- /* eslint-disable no-redeclare, unicorn/no-new-array */
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports._range = void 0;
3
+ exports._rangeIt = exports._range = void 0;
4
+ const iterable2_1 = require("../iter/iterable2");
5
5
  function _range(fromIncl, toExcl, step = 1) {
6
6
  if (toExcl === undefined) {
7
7
  return Array.from(new Array(fromIncl), (_, i) => i);
@@ -9,3 +9,17 @@ function _range(fromIncl, toExcl, step = 1) {
9
9
  return Array.from({ length: Math.ceil((toExcl - fromIncl) / step) }, (_, i) => i * step + fromIncl);
10
10
  }
11
11
  exports._range = _range;
12
+ function _rangeIt(fromIncl, toExcl, step = 1) {
13
+ if (toExcl === undefined) {
14
+ toExcl = fromIncl;
15
+ fromIncl = 0;
16
+ }
17
+ return iterable2_1.Iterable2.of({
18
+ *[Symbol.iterator]() {
19
+ for (let i = fromIncl; i < toExcl; i += step) {
20
+ yield i;
21
+ }
22
+ },
23
+ });
24
+ }
25
+ exports._rangeIt = _rangeIt;
@@ -1,3 +1,4 @@
1
+ import { Iterable2 } from '../iter/iterable2';
1
2
  import type { IsoDateString, IsoDateTimeString, MonthId, SortDirection, UnixTimestampMillisNumber, UnixTimestampNumber } from '../types';
2
3
  import { LocalTime } from './localTime';
3
4
  export type LocalDateUnit = LocalDateUnitStrict | 'week';
@@ -35,6 +36,10 @@ export declare class LocalDate {
35
36
  static latestOrUndefined(items: LocalDateInput[]): LocalDate | undefined;
36
37
  static latest(items: LocalDateInput[]): LocalDate;
37
38
  static range(min: LocalDateInput, max: LocalDateInput, incl?: Inclusiveness, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
39
+ /**
40
+ * Experimental, returns the range as Iterable2.
41
+ */
42
+ static rangeIt(min: LocalDateInput, max: LocalDateInput, incl?: Inclusiveness, step?: number, stepUnit?: LocalDateUnit): Iterable2<LocalDate>;
38
43
  get(unit: LocalDateUnitStrict): number;
39
44
  set(unit: LocalDateUnitStrict, v: number, mutate?: boolean): LocalDate;
40
45
  year(): number;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.localDateOrToday = exports.localDateOrUndefined = exports.localDateToday = exports.localDate = exports.LocalDate = void 0;
4
4
  const assert_1 = require("../error/assert");
5
+ const iterable2_1 = require("../iter/iterable2");
5
6
  const localTime_1 = require("./localTime");
6
7
  const MDAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
7
8
  const DATE_REGEX = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
@@ -104,27 +105,37 @@ class LocalDate {
104
105
  .reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
105
106
  }
106
107
  static range(min, max, incl = '[)', step = 1, stepUnit = 'day') {
108
+ return this.rangeIt(min, max, incl, step, stepUnit).toArray();
109
+ }
110
+ /**
111
+ * Experimental, returns the range as Iterable2.
112
+ */
113
+ static rangeIt(min, max, incl = '[)', step = 1, stepUnit = 'day') {
107
114
  if (stepUnit === 'week') {
108
115
  step *= 7;
109
116
  stepUnit = 'day';
110
117
  }
111
- const dates = [];
112
- const $min = LocalDate.of(min);
118
+ const $min = LocalDate.of(min).startOf(stepUnit);
113
119
  const $max = LocalDate.of(max).startOf(stepUnit);
114
- let current = $min.startOf(stepUnit);
120
+ let value = $min;
115
121
  // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
116
- if (current.isAfter($min, incl[0] === '[')) {
122
+ if (value.isAfter($min, incl[0] === '[')) {
117
123
  // ok
118
124
  }
119
125
  else {
120
- current.plus(1, stepUnit, true);
126
+ value.plus(1, stepUnit, true);
121
127
  }
122
- const incl2 = incl[1] === ']';
123
- while (current.isBefore($max, incl2)) {
124
- dates.push(current);
125
- current = current.plus(step, stepUnit);
126
- }
127
- return dates;
128
+ const rightInclusive = incl[1] === ']';
129
+ return iterable2_1.Iterable2.of({
130
+ *[Symbol.iterator]() {
131
+ while (value.isBefore($max, rightInclusive)) {
132
+ yield value;
133
+ // We don't mutate, because we already returned `current`
134
+ // in the previous iteration
135
+ value = value.plus(step, stepUnit);
136
+ }
137
+ },
138
+ });
128
139
  }
129
140
  get(unit) {
130
141
  return unit === 'year' ? this.$year : unit === 'month' ? this.$month : this.$day;
@@ -27,7 +27,7 @@ function _LogMethod(opt = {}) {
27
27
  let { logResultFn } = opt;
28
28
  if (!logResultFn) {
29
29
  if (logResult) {
30
- logResultFn = r => ['result:', (0, __1._stringifyAny)(r)];
30
+ logResultFn = r => ['result:', (0, __1._stringify)(r)];
31
31
  }
32
32
  else if (logResultLength) {
33
33
  logResultFn = r => (Array.isArray(r) ? [`result: ${r.length} items`] : []);
@@ -36,7 +36,7 @@ exports._assert = _assert;
36
36
  function _assertEquals(actual, expected, message, errorData) {
37
37
  if (actual !== expected) {
38
38
  const msg = message ||
39
- ['not equal', `expected: ${(0, __1._stringifyAny)(expected)}`, `got : ${(0, __1._stringifyAny)(actual)}`]
39
+ ['not equal', `expected: ${(0, __1._stringify)(expected)}`, `got : ${(0, __1._stringify)(actual)}`]
40
40
  .filter(Boolean)
41
41
  .join('\n');
42
42
  throw new __1.AssertionError(msg, {
@@ -55,11 +55,7 @@ exports._assertEquals = _assertEquals;
55
55
  function _assertDeepEquals(actual, expected, message, errorData) {
56
56
  if (!(0, __1._deepEquals)(actual, expected)) {
57
57
  const msg = message ||
58
- [
59
- `not deeply equal`,
60
- `expected: ${(0, __1._stringifyAny)(expected)}`,
61
- `got : ${(0, __1._stringifyAny)(actual)}`,
62
- ]
58
+ [`not deeply equal`, `expected: ${(0, __1._stringify)(expected)}`, `got : ${(0, __1._stringify)(actual)}`]
63
59
  .filter(Boolean)
64
60
  .join('\n');
65
61
  throw new __1.AssertionError(msg, {
@@ -12,7 +12,7 @@ export declare function _anyToError<ERROR_TYPE extends Error = Error>(o: any, er
12
12
  * Converts "anything" to ErrorObject.
13
13
  * Detects if it's HttpErrorResponse, HttpErrorObject, ErrorObject, Error, etc..
14
14
  * If object is Error - Error.message will be used.
15
- * Objects (not Errors) get converted to prettified JSON string (via `_stringifyAny`).
15
+ * Objects (not Errors) get converted to prettified JSON string (via `_stringify`).
16
16
  */
17
17
  export declare function _anyToErrorObject<DATA_TYPE extends ErrorData = ErrorData>(o: any, errorData?: Partial<DATA_TYPE>): ErrorObject<DATA_TYPE>;
18
18
  export declare function _errorLikeToErrorObject<DATA_TYPE extends ErrorData = ErrorData>(e: AppError<DATA_TYPE> | Error | ErrorLike): ErrorObject<DATA_TYPE>;
@@ -34,7 +34,7 @@ exports._anyToError = _anyToError;
34
34
  * Converts "anything" to ErrorObject.
35
35
  * Detects if it's HttpErrorResponse, HttpErrorObject, ErrorObject, Error, etc..
36
36
  * If object is Error - Error.message will be used.
37
- * Objects (not Errors) get converted to prettified JSON string (via `_stringifyAny`).
37
+ * Objects (not Errors) get converted to prettified JSON string (via `_stringify`).
38
38
  */
39
39
  function _anyToErrorObject(o, errorData) {
40
40
  let eo;
@@ -57,7 +57,7 @@ function _anyToErrorObject(o, errorData) {
57
57
  // so, fair to return `data: {}` in the end
58
58
  // Also we're sure it includes no "error name", e.g no `Error: ...`,
59
59
  // so, fair to include `name: 'Error'`
60
- const message = (0, __1._stringifyAny)(o);
60
+ const message = (0, __1._stringify)(o);
61
61
  eo = {
62
62
  name: 'Error',
63
63
  message,
package/dist/error/try.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports._expectedErrorString = exports.pExpectedErrorString = exports.pExpectedError = exports._expectedError = exports.pTry = exports._try = void 0;
4
- const stringifyAny_1 = require("../string/stringifyAny");
4
+ const stringify_1 = require("../string/stringify");
5
5
  const assert_1 = require("./assert");
6
6
  const error_util_1 = require("./error.util");
7
7
  /**
@@ -101,7 +101,7 @@ exports.pExpectedError = pExpectedError;
101
101
  */
102
102
  async function pExpectedErrorString(promise, errorClass) {
103
103
  const err = await pExpectedError(promise, errorClass);
104
- return (0, stringifyAny_1._stringifyAny)(err);
104
+ return (0, stringify_1._stringify)(err);
105
105
  }
106
106
  exports.pExpectedErrorString = pExpectedErrorString;
107
107
  /**
@@ -109,6 +109,6 @@ exports.pExpectedErrorString = pExpectedErrorString;
109
109
  */
110
110
  function _expectedErrorString(fn, errorClass) {
111
111
  const err = _expectedError(fn, errorClass);
112
- return (0, stringifyAny_1._stringifyAny)(err);
112
+ return (0, stringify_1._stringify)(err);
113
113
  }
114
114
  exports._expectedErrorString = _expectedErrorString;
@@ -12,7 +12,7 @@ const object_util_1 = require("../object/object.util");
12
12
  const pDelay_1 = require("../promise/pDelay");
13
13
  const pTimeout_1 = require("../promise/pTimeout");
14
14
  const json_util_1 = require("../string/json.util");
15
- const stringifyAny_1 = require("../string/stringifyAny");
15
+ const stringify_1 = require("../string/stringify");
16
16
  const time_util_1 = require("../time/time.util");
17
17
  const http_model_1 = require("./http.model");
18
18
  const acceptByResponseType = {
@@ -392,7 +392,7 @@ class Fetcher {
392
392
  .filter(Boolean)
393
393
  .join(' ') + '\n',
394
394
  // We're stringifying the error here, otherwise Sentry shows it as [object Object]
395
- (0, stringifyAny_1._stringifyAny)(res.err.cause || res.err));
395
+ (0, stringify_1._stringify)(res.err.cause || res.err));
396
396
  }
397
397
  if (retryStatus.retryStopped)
398
398
  return;
package/dist/index.d.ts CHANGED
@@ -50,7 +50,7 @@ export * from './string/string.util';
50
50
  export * from './string/readingTime';
51
51
  export * from './string/escape';
52
52
  export * from './string/pupa';
53
- export * from './string/stringifyAny';
53
+ export * from './string/stringify';
54
54
  export * from './time/time.util';
55
55
  export * from './is.util';
56
56
  export * from './typeFest';
@@ -60,7 +60,7 @@ export * from './log/commonLogger';
60
60
  export * from './string/safeJsonStringify';
61
61
  export * from './promise/pQueue';
62
62
  export * from './promise/abortable';
63
- export * from './seq/seq';
63
+ export * from './iter/iterable2';
64
64
  export * from './math/stack.util';
65
65
  export * from './string/leven';
66
66
  export * from './datetime/localDate';
package/dist/index.js CHANGED
@@ -54,7 +54,7 @@ tslib_1.__exportStar(require("./string/string.util"), exports);
54
54
  tslib_1.__exportStar(require("./string/readingTime"), exports);
55
55
  tslib_1.__exportStar(require("./string/escape"), exports);
56
56
  tslib_1.__exportStar(require("./string/pupa"), exports);
57
- tslib_1.__exportStar(require("./string/stringifyAny"), exports);
57
+ tslib_1.__exportStar(require("./string/stringify"), exports);
58
58
  tslib_1.__exportStar(require("./time/time.util"), exports);
59
59
  tslib_1.__exportStar(require("./is.util"), exports);
60
60
  tslib_1.__exportStar(require("./typeFest"), exports);
@@ -64,7 +64,7 @@ tslib_1.__exportStar(require("./log/commonLogger"), exports);
64
64
  tslib_1.__exportStar(require("./string/safeJsonStringify"), exports);
65
65
  tslib_1.__exportStar(require("./promise/pQueue"), exports);
66
66
  tslib_1.__exportStar(require("./promise/abortable"), exports);
67
- tslib_1.__exportStar(require("./seq/seq"), exports);
67
+ tslib_1.__exportStar(require("./iter/iterable2"), exports);
68
68
  tslib_1.__exportStar(require("./math/stack.util"), exports);
69
69
  tslib_1.__exportStar(require("./string/leven"), exports);
70
70
  tslib_1.__exportStar(require("./datetime/localDate"), exports);
@@ -0,0 +1,21 @@
1
+ import { AbortableMapper, AbortablePredicate, END, Predicate } from '../types';
2
+ /**
3
+ * Iterable2 is a wrapper around Iterable that implements "Iterator Helpers proposal":
4
+ * https://github.com/tc39/proposal-iterator-helpers
5
+ *
6
+ * Iterable2 can be removed after the proposal is widely implemented in Node & browsers.
7
+ *
8
+ * @experimental
9
+ */
10
+ export declare class Iterable2<T> implements Iterable<T> {
11
+ private it;
12
+ private constructor();
13
+ static of<T>(it: Iterable<T>): Iterable2<T>;
14
+ static empty<T>(): Iterable2<T>;
15
+ [Symbol.iterator](): Iterator<T>;
16
+ toArray(): T[];
17
+ forEach(cb: (v: T, i: number) => any | typeof END): void;
18
+ find(cb: Predicate<T>): T | undefined;
19
+ filter(cb: AbortablePredicate<T>): Iterable2<T>;
20
+ map<OUT>(mapper: AbortableMapper<T, OUT>): Iterable2<OUT>;
21
+ }
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Iterable2 = void 0;
4
+ const types_1 = require("../types");
5
+ /**
6
+ * Iterable2 is a wrapper around Iterable that implements "Iterator Helpers proposal":
7
+ * https://github.com/tc39/proposal-iterator-helpers
8
+ *
9
+ * Iterable2 can be removed after the proposal is widely implemented in Node & browsers.
10
+ *
11
+ * @experimental
12
+ */
13
+ class Iterable2 {
14
+ constructor(it) {
15
+ this.it = it;
16
+ }
17
+ static of(it) {
18
+ return new Iterable2(it);
19
+ }
20
+ static empty() {
21
+ return new Iterable2([]);
22
+ }
23
+ [Symbol.iterator]() {
24
+ return this.it[Symbol.iterator]();
25
+ }
26
+ toArray() {
27
+ return [...this.it];
28
+ }
29
+ forEach(cb) {
30
+ let i = 0;
31
+ for (const v of this.it) {
32
+ if (cb(v, i++) === types_1.END)
33
+ return;
34
+ }
35
+ }
36
+ find(cb) {
37
+ let i = 0;
38
+ for (const v of this.it) {
39
+ if (cb(v, i++))
40
+ return v;
41
+ }
42
+ }
43
+ filter(cb) {
44
+ const { it } = this;
45
+ return new Iterable2({
46
+ *[Symbol.iterator]() {
47
+ let i = 0;
48
+ for (const v of it) {
49
+ const r = cb(v, i++);
50
+ if (r === types_1.END)
51
+ return;
52
+ if (r)
53
+ yield v;
54
+ }
55
+ },
56
+ });
57
+ }
58
+ map(mapper) {
59
+ const { it } = this;
60
+ return new Iterable2({
61
+ *[Symbol.iterator]() {
62
+ let i = 0;
63
+ for (const v of it) {
64
+ const r = mapper(v, i++);
65
+ if (r === types_1.END)
66
+ return;
67
+ if (r === types_1.SKIP)
68
+ continue;
69
+ yield r;
70
+ }
71
+ },
72
+ });
73
+ }
74
+ }
75
+ exports.Iterable2 = Iterable2;
@@ -3,18 +3,18 @@ import type { Reviver } from '../types';
3
3
  * Allows to set Global "stringifyFunction" that will be used to "pretty-print" objects
4
4
  * in various cases.
5
5
  *
6
- * Used, for example, by _stringifyAny() to pretty-print objects/arrays.
6
+ * Used, for example, by _stringify() to pretty-print objects/arrays.
7
7
  *
8
8
  * Defaults to _safeJsonStringify.
9
9
  *
10
- * Node.js project can set it to _inspectAny, which allows to use `util.inspect`
10
+ * Node.js project can set it to _inspect, which allows to use `util.inspect`
11
11
  * as pretty-printing function.
12
12
  *
13
13
  * It's recommended that this function is circular-reference-safe.
14
14
  */
15
15
  export declare function setGlobalStringifyFunction(fn: JsonStringifyFunction): void;
16
16
  export type JsonStringifyFunction = (obj: any, reviver?: Reviver, space?: number) => string;
17
- export interface StringifyAnyOptions {
17
+ export interface StringifyOptions {
18
18
  /**
19
19
  * @default 10_000
20
20
  * Default limit is less than in Node, cause it's likely to be used e.g in Browser alert()
@@ -47,13 +47,13 @@ export interface StringifyAnyOptions {
47
47
  stringifyFn?: JsonStringifyFunction;
48
48
  }
49
49
  /**
50
- * Inspired by inspectAny from nodejs-lib, which is based on util.inpect that is not available in the Browser.
50
+ * Inspired by `_inspect` from nodejs-lib, which is based on util.inpect that is not available in the Browser.
51
51
  * Potentially can do this (with extra 2Kb gz size): https://github.com/deecewan/browser-util-inspect
52
52
  *
53
53
  * Transforms ANY to human-readable string (via JSON.stringify pretty).
54
54
  * Safe (no error throwing).
55
55
  *
56
- * Correclty prints Errors, AppErrors, ErrorObjects: error.message + \n + stringifyAny(error.data)
56
+ * Correctly prints Errors, AppErrors, ErrorObjects: error.message + \n + _stringify(error.data)
57
57
  *
58
58
  * Enforces max length (default to 1000, pass 0 to skip it).
59
59
  *
@@ -64,4 +64,8 @@ export interface StringifyAnyOptions {
64
64
  * Returns 'empty_string' if empty string is passed.
65
65
  * Returns 'undefined' if undefined is passed (default util.inspect behavior).
66
66
  */
67
- export declare function _stringifyAny(obj: any, opt?: StringifyAnyOptions): string;
67
+ export declare function _stringify(obj: any, opt?: StringifyOptions): string;
68
+ /**
69
+ * @deprecated renamed to _stringify
70
+ */
71
+ export declare const _stringifyAny: typeof _stringify;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._stringifyAny = exports.setGlobalStringifyFunction = void 0;
3
+ exports._stringifyAny = exports._stringify = exports.setGlobalStringifyFunction = void 0;
4
4
  const error_util_1 = require("../error/error.util");
5
5
  const json_util_1 = require("./json.util");
6
6
  const safeJsonStringify_1 = require("./safeJsonStringify");
@@ -10,11 +10,11 @@ let globalStringifyFunction = safeJsonStringify_1._safeJsonStringify;
10
10
  * Allows to set Global "stringifyFunction" that will be used to "pretty-print" objects
11
11
  * in various cases.
12
12
  *
13
- * Used, for example, by _stringifyAny() to pretty-print objects/arrays.
13
+ * Used, for example, by _stringify() to pretty-print objects/arrays.
14
14
  *
15
15
  * Defaults to _safeJsonStringify.
16
16
  *
17
- * Node.js project can set it to _inspectAny, which allows to use `util.inspect`
17
+ * Node.js project can set it to _inspect, which allows to use `util.inspect`
18
18
  * as pretty-printing function.
19
19
  *
20
20
  * It's recommended that this function is circular-reference-safe.
@@ -24,13 +24,13 @@ function setGlobalStringifyFunction(fn) {
24
24
  }
25
25
  exports.setGlobalStringifyFunction = setGlobalStringifyFunction;
26
26
  /**
27
- * Inspired by inspectAny from nodejs-lib, which is based on util.inpect that is not available in the Browser.
27
+ * Inspired by `_inspect` from nodejs-lib, which is based on util.inpect that is not available in the Browser.
28
28
  * Potentially can do this (with extra 2Kb gz size): https://github.com/deecewan/browser-util-inspect
29
29
  *
30
30
  * Transforms ANY to human-readable string (via JSON.stringify pretty).
31
31
  * Safe (no error throwing).
32
32
  *
33
- * Correclty prints Errors, AppErrors, ErrorObjects: error.message + \n + stringifyAny(error.data)
33
+ * Correctly prints Errors, AppErrors, ErrorObjects: error.message + \n + _stringify(error.data)
34
34
  *
35
35
  * Enforces max length (default to 1000, pass 0 to skip it).
36
36
  *
@@ -41,7 +41,7 @@ exports.setGlobalStringifyFunction = setGlobalStringifyFunction;
41
41
  * Returns 'empty_string' if empty string is passed.
42
42
  * Returns 'undefined' if undefined is passed (default util.inspect behavior).
43
43
  */
44
- function _stringifyAny(obj, opt = {}) {
44
+ function _stringify(obj, opt = {}) {
45
45
  if (obj === undefined)
46
46
  return 'undefined';
47
47
  if (obj === null)
@@ -57,7 +57,7 @@ function _stringifyAny(obj, opt = {}) {
57
57
  // HttpErrorResponse
58
58
  //
59
59
  if ((0, error_util_1._isBackendErrorResponseObject)(obj)) {
60
- return _stringifyAny(obj.error, opt);
60
+ return _stringify(obj.error, opt);
61
61
  }
62
62
  if (obj instanceof Error || (0, error_util_1._isErrorLike)(obj)) {
63
63
  const { includeErrorCause = true } = opt;
@@ -79,7 +79,7 @@ function _stringifyAny(obj, opt = {}) {
79
79
  s += `\ncode: ${obj.code}`;
80
80
  }
81
81
  if (opt.includeErrorData && (0, error_util_1._isErrorObject)(obj) && Object.keys(obj.data).length) {
82
- s += '\n' + _stringifyAny(obj.data, opt);
82
+ s += '\n' + _stringify(obj.data, opt);
83
83
  }
84
84
  if (opt.includeErrorStack && obj.stack) {
85
85
  // Here we're using the previously-generated "title line" (e.g "Error: some_message"),
@@ -94,11 +94,11 @@ function _stringifyAny(obj, opt = {}) {
94
94
  s = [
95
95
  s,
96
96
  `${obj.errors.length} error(s):`,
97
- ...obj.errors.map((err, i) => `${i + 1}. ${_stringifyAny(err, opt)}`),
97
+ ...obj.errors.map((err, i) => `${i + 1}. ${_stringify(err, opt)}`),
98
98
  ].join('\n');
99
99
  }
100
100
  if (obj.cause && includeErrorCause) {
101
- s = s + '\nCaused by: ' + _stringifyAny(obj.cause, opt);
101
+ s = s + '\nCaused by: ' + _stringify(obj.cause, opt);
102
102
  }
103
103
  }
104
104
  else if (typeof obj === 'string') {
@@ -129,4 +129,8 @@ function _stringifyAny(obj, opt = {}) {
129
129
  }
130
130
  return s;
131
131
  }
132
- exports._stringifyAny = _stringifyAny;
132
+ exports._stringify = _stringify;
133
+ /**
134
+ * @deprecated renamed to _stringify
135
+ */
136
+ exports._stringifyAny = _stringify;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ZodValidationError = exports.zSafeValidate = exports.zValidate = exports.zIsValid = void 0;
4
4
  const zod_1 = require("zod");
5
- const stringifyAny_1 = require("../string/stringifyAny");
5
+ const stringify_1 = require("../string/stringify");
6
6
  function zIsValid(value, schema) {
7
7
  const { success } = schema.safeParse(value);
8
8
  return success;
@@ -48,7 +48,7 @@ class ZodValidationError extends zod_1.ZodError {
48
48
  `Invalid ${objectTitle}`,
49
49
  '',
50
50
  'Input:',
51
- (0, stringifyAny_1._stringifyAny)(this.value),
51
+ (0, stringify_1._stringify)(this.value),
52
52
  this.issues.length > 1 ? `\n${this.issues.length} issues:` : '',
53
53
  ...this.issues.slice(0, 100).map(i => {
54
54
  return [i.path.join('.'), i.message].filter(Boolean).join(': ');
@@ -1,7 +1,20 @@
1
- /* eslint-disable no-redeclare, unicorn/no-new-array */
1
+ import { Iterable2 } from '../iter/iterable2';
2
2
  export function _range(fromIncl, toExcl, step = 1) {
3
3
  if (toExcl === undefined) {
4
4
  return Array.from(new Array(fromIncl), (_, i) => i);
5
5
  }
6
6
  return Array.from({ length: Math.ceil((toExcl - fromIncl) / step) }, (_, i) => i * step + fromIncl);
7
7
  }
8
+ export function _rangeIt(fromIncl, toExcl, step = 1) {
9
+ if (toExcl === undefined) {
10
+ toExcl = fromIncl;
11
+ fromIncl = 0;
12
+ }
13
+ return Iterable2.of({
14
+ *[Symbol.iterator]() {
15
+ for (let i = fromIncl; i < toExcl; i += step) {
16
+ yield i;
17
+ }
18
+ },
19
+ });
20
+ }
@@ -1,4 +1,5 @@
1
1
  import { _assert } from '../error/assert';
2
+ import { Iterable2 } from '../iter/iterable2';
2
3
  import { LocalTime } from './localTime';
3
4
  const MDAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
4
5
  const DATE_REGEX = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
@@ -101,27 +102,37 @@ export class LocalDate {
101
102
  .reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
102
103
  }
103
104
  static range(min, max, incl = '[)', step = 1, stepUnit = 'day') {
105
+ return this.rangeIt(min, max, incl, step, stepUnit).toArray();
106
+ }
107
+ /**
108
+ * Experimental, returns the range as Iterable2.
109
+ */
110
+ static rangeIt(min, max, incl = '[)', step = 1, stepUnit = 'day') {
104
111
  if (stepUnit === 'week') {
105
112
  step *= 7;
106
113
  stepUnit = 'day';
107
114
  }
108
- const dates = [];
109
- const $min = LocalDate.of(min);
115
+ const $min = LocalDate.of(min).startOf(stepUnit);
110
116
  const $max = LocalDate.of(max).startOf(stepUnit);
111
- let current = $min.startOf(stepUnit);
117
+ let value = $min;
112
118
  // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
113
- if (current.isAfter($min, incl[0] === '[')) {
119
+ if (value.isAfter($min, incl[0] === '[')) {
114
120
  // ok
115
121
  }
116
122
  else {
117
- current.plus(1, stepUnit, true);
123
+ value.plus(1, stepUnit, true);
118
124
  }
119
- const incl2 = incl[1] === ']';
120
- while (current.isBefore($max, incl2)) {
121
- dates.push(current);
122
- current = current.plus(step, stepUnit);
123
- }
124
- return dates;
125
+ const rightInclusive = incl[1] === ']';
126
+ return Iterable2.of({
127
+ *[Symbol.iterator]() {
128
+ while (value.isBefore($max, rightInclusive)) {
129
+ yield value;
130
+ // We don't mutate, because we already returned `current`
131
+ // in the previous iteration
132
+ value = value.plus(step, stepUnit);
133
+ }
134
+ },
135
+ });
125
136
  }
126
137
  get(unit) {
127
138
  return unit === 'year' ? this.$year : unit === 'month' ? this.$month : this.$day;
@@ -1,4 +1,4 @@
1
- import { SimpleMovingAverage, _stringifyAny, _assert } from '..';
1
+ import { SimpleMovingAverage, _stringify, _assert } from '..';
2
2
  import { _ms } from '../time/time.util';
3
3
  import { _getArgsSignature, _getMethodSignature } from './decorator.util';
4
4
  /**
@@ -24,7 +24,7 @@ export function _LogMethod(opt = {}) {
24
24
  let { logResultFn } = opt;
25
25
  if (!logResultFn) {
26
26
  if (logResult) {
27
- logResultFn = r => ['result:', _stringifyAny(r)];
27
+ logResultFn = r => ['result:', _stringify(r)];
28
28
  }
29
29
  else if (logResultLength) {
30
30
  logResultFn = r => (Array.isArray(r) ? [`result: ${r.length} items`] : []);
@@ -1,4 +1,4 @@
1
- import { _deepEquals, _isErrorObject, _stringifyAny, AssertionError } from '..';
1
+ import { _deepEquals, _isErrorObject, _stringify, AssertionError } from '..';
2
2
  /**
3
3
  * Evaluates the `condition` (casts it to Boolean).
4
4
  * Expects it to be truthy, otherwise throws AppError.
@@ -29,7 +29,7 @@ message, errorData) {
29
29
  export function _assertEquals(actual, expected, message, errorData) {
30
30
  if (actual !== expected) {
31
31
  const msg = message ||
32
- ['not equal', `expected: ${_stringifyAny(expected)}`, `got : ${_stringifyAny(actual)}`]
32
+ ['not equal', `expected: ${_stringify(expected)}`, `got : ${_stringify(actual)}`]
33
33
  .filter(Boolean)
34
34
  .join('\n');
35
35
  throw new AssertionError(msg, Object.assign({ userFriendly: true }, errorData));
@@ -44,11 +44,7 @@ export function _assertEquals(actual, expected, message, errorData) {
44
44
  export function _assertDeepEquals(actual, expected, message, errorData) {
45
45
  if (!_deepEquals(actual, expected)) {
46
46
  const msg = message ||
47
- [
48
- `not deeply equal`,
49
- `expected: ${_stringifyAny(expected)}`,
50
- `got : ${_stringifyAny(actual)}`,
51
- ]
47
+ [`not deeply equal`, `expected: ${_stringify(expected)}`, `got : ${_stringify(actual)}`]
52
48
  .filter(Boolean)
53
49
  .join('\n');
54
50
  throw new AssertionError(msg, Object.assign({ userFriendly: true }, errorData));