@naturalcycles/js-lib 14.55.0 → 14.58.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.
@@ -16,6 +16,8 @@ export declare function _chunk<T>(array: readonly T[], size?: number): T[][];
16
16
  /**
17
17
  * Polyfill to Array.flat() with depth=1.
18
18
  * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
19
+ *
20
+ * @deprecated prefer native Array.flat(), it's supported since iOS 12
19
21
  */
20
22
  export declare function _flatten<T>(arrays: T[][]): T[];
21
23
  /**
@@ -25,6 +27,8 @@ export declare function _flatten<T>(arrays: T[][]): T[];
25
27
  * @return Returns the new flattened array.
26
28
  *
27
29
  * Based on: https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_flattendeep
30
+ *
31
+ * @deprecated prefer native Array.flat(), it's supported since iOS 12
28
32
  */
29
33
  export declare function _flattenDeep<T>(arr: RecursiveArray<T>): T[];
30
34
  /**
@@ -22,6 +22,8 @@ exports._chunk = _chunk;
22
22
  /**
23
23
  * Polyfill to Array.flat() with depth=1.
24
24
  * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
25
+ *
26
+ * @deprecated prefer native Array.flat(), it's supported since iOS 12
25
27
  */
26
28
  function _flatten(arrays) {
27
29
  // to flat single level array
@@ -35,6 +37,8 @@ exports._flatten = _flatten;
35
37
  * @return Returns the new flattened array.
36
38
  *
37
39
  * Based on: https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_flattendeep
40
+ *
41
+ * @deprecated prefer native Array.flat(), it's supported since iOS 12
38
42
  */
39
43
  function _flattenDeep(arr) {
40
44
  return Array.isArray(arr)
@@ -13,19 +13,23 @@ exports.AppError = void 0;
13
13
  class AppError extends Error {
14
14
  constructor(message, data = {}) {
15
15
  super(message);
16
- this.data = data;
17
- this.constructor = AppError;
18
- this.__proto__ = AppError.prototype;
19
16
  Object.defineProperty(this, 'name', {
20
17
  value: this.constructor.name,
21
18
  configurable: true,
22
19
  });
20
+ Object.defineProperty(this, 'data', {
21
+ value: data,
22
+ writable: true,
23
+ configurable: true,
24
+ enumerable: false,
25
+ });
23
26
  if (Error.captureStackTrace) {
24
27
  Error.captureStackTrace(this, this.constructor);
25
28
  }
26
29
  else {
27
30
  Object.defineProperty(this, 'stack', {
28
31
  value: new Error().stack,
32
+ writable: true,
29
33
  configurable: true,
30
34
  });
31
35
  }
@@ -36,5 +36,5 @@ export declare function _assertIsString(v: any, message?: string): asserts v is
36
36
  export declare function _assertIsNumber(v: any, message?: string): asserts v is number;
37
37
  export declare function _assertTypeOf<T>(v: any, expectedType: string, message?: string): asserts v is T;
38
38
  export declare class AssertionError extends AppError {
39
- constructor(message: string, data: ErrorData);
39
+ constructor(message: string, data?: ErrorData);
40
40
  }
@@ -103,23 +103,8 @@ function _assertTypeOf(v, expectedType, message) {
103
103
  }
104
104
  exports._assertTypeOf = _assertTypeOf;
105
105
  class AssertionError extends app_error_1.AppError {
106
- constructor(message, data) {
106
+ constructor(message, data = {}) {
107
107
  super(message, data);
108
- this.constructor = AssertionError;
109
- this.__proto__ = AssertionError.prototype;
110
- Object.defineProperty(this, 'name', {
111
- value: this.constructor.name,
112
- configurable: true, // otherwise throws with "TypeError: Cannot redefine property: name"
113
- });
114
- if (Error.captureStackTrace) {
115
- Error.captureStackTrace(this, this.constructor);
116
- }
117
- else {
118
- Object.defineProperty(this, 'stack', {
119
- value: new Error().stack,
120
- configurable: true,
121
- });
122
- }
123
108
  }
124
109
  }
125
110
  exports.AssertionError = AssertionError;
@@ -8,21 +8,6 @@ const app_error_1 = require("./app.error");
8
8
  class HttpError extends app_error_1.AppError {
9
9
  constructor(message, data) {
10
10
  super(message, data);
11
- this.constructor = HttpError;
12
- this.__proto__ = HttpError.prototype;
13
- Object.defineProperty(this, 'name', {
14
- value: this.constructor.name,
15
- configurable: true, // otherwise throws with "TypeError: Cannot redefine property: name"
16
- });
17
- if (Error.captureStackTrace) {
18
- Error.captureStackTrace(this, this.constructor);
19
- }
20
- else {
21
- Object.defineProperty(this, 'stack', {
22
- value: new Error().stack,
23
- configurable: true,
24
- });
25
- }
26
11
  }
27
12
  }
28
13
  exports.HttpError = HttpError;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { _by, _chunk, _countBy, _difference, _dropRightWhile, _dropWhile, _findLast, _flatten, _flattenDeep, _groupBy, _intersection, _last, _mapToObject, _shuffle, _sortBy, _sum, _sumBy, _takeRightWhile, _takeWhile, _uniq, _uniqBy } from './array/array.util';
2
+ import { _defineLazyProperty, _defineLazyProps, _lazyValue } from './lazy';
2
3
  import { _parseQueryString } from './string/url.util';
3
4
  import { _range } from './array/range';
4
5
  import { PromiseDecoratorCfg, PromiseDecoratorResp, _createPromiseDecorator } from './decorators/createPromiseDecorator';
@@ -54,4 +55,4 @@ import { AsyncMapper, AsyncPredicate, BaseDBEntity, CreatedUpdated, CreatedUpdat
54
55
  import { _gb, _hb, _kb, _mb } from './unit/size.util';
55
56
  import { is } from './vendor/is';
56
57
  export type { MemoCache, 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, PromiseValue, Simplify, ConditionalPick, ConditionalExcept, Class, UnixTimestamp, 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, };
57
- export { is, _Memo, _memoFn, _LogMethod, _getArgsSignature, _createPromiseDecorator, AppError, HttpError, AssertionError, _isErrorObject, _isHttpErrorObject, _isHttpErrorResponse, _assert, _assertEquals, _assertDeepEquals, _assertIsError, _assertIsString, _assertIsNumber, _assertTypeOf, _randomInt, _randomArrayItem, _createDeterministicRandom, _inRange, _stringMapValues, _stringMapEntries, _objectKeys, _capitalize, _upperFirst, _lowerFirst, _split, _removeWhitespace, _substringBefore, _substringBeforeLast, _substringAfter, _substringAfterLast, _substringBetweenLast, _replaceAll, _nl2br, _truncate, _truncateMiddle, _pick, _omit, _filterFalsyValues, _filterUndefinedValues, _filterNullishValues, _filterEmptyArrays, _filterEmptyValues, _filterObject, _undefinedIfEmpty, _isObject, _isPrimitive, _mapKeys, _mapValues, _mapObject, _objectNullValuesToUndefined, _deepEquals, _deepCopy, _isEmptyObject, _isEmpty, _merge, _deepTrim, _sortObjectDeep, _sortObject, _get, _set, _has, _unset, _mask, _invert, _invertMap, _by, _groupBy, _sortBy, _sortNumbers, _toFixed, _toPrecision, _round, _findLast, _takeWhile, _takeRightWhile, _dropWhile, _dropRightWhile, _countBy, _intersection, _difference, _shuffle, _mapToObject, _findKeyByValue, _anyToError, _anyToErrorObject, _errorToErrorObject, _errorObjectToAppError, _range, _uniq, _uniqBy, _flatten, _flattenDeep, _chunk, SimpleMovingAverage, _average, _averageWeighted, _percentile, _median, _debounce, _throttle, _Debounce, _Throttle, pMap, _passthroughMapper, _passUndefinedMapper, _passthroughPredicate, _passNothingPredicate, _noop, pBatch, ErrorMode, pFilter, pProps, pDelay, pDefer, pHang, pState, AggregatedError, pRetry, pTimeout, pTuple, _Retry, _Timeout, _tryCatch, _TryCatch, _try, pTry, _jsonParseIfPossible, _stringifyAny, _ms, _since, _hb, _gb, _mb, _kb, _snakeCase, _camelCase, _kebabCase, _sum, _sumBy, _clamp, _last, mergeJsonSchemaObjects, jsonSchema, JsonSchemaAnyBuilder, JSON_SCHEMA_ORDER, generateJsonSchemaFromData, _parseQueryString, };
58
+ export { is, _Memo, _memoFn, _LogMethod, _getArgsSignature, _createPromiseDecorator, AppError, HttpError, AssertionError, _isErrorObject, _isHttpErrorObject, _isHttpErrorResponse, _assert, _assertEquals, _assertDeepEquals, _assertIsError, _assertIsString, _assertIsNumber, _assertTypeOf, _randomInt, _randomArrayItem, _createDeterministicRandom, _inRange, _stringMapValues, _stringMapEntries, _objectKeys, _capitalize, _upperFirst, _lowerFirst, _split, _removeWhitespace, _substringBefore, _substringBeforeLast, _substringAfter, _substringAfterLast, _substringBetweenLast, _replaceAll, _nl2br, _truncate, _truncateMiddle, _pick, _omit, _filterFalsyValues, _filterUndefinedValues, _filterNullishValues, _filterEmptyArrays, _filterEmptyValues, _filterObject, _undefinedIfEmpty, _isObject, _isPrimitive, _mapKeys, _mapValues, _mapObject, _objectNullValuesToUndefined, _deepEquals, _deepCopy, _isEmptyObject, _isEmpty, _merge, _deepTrim, _sortObjectDeep, _sortObject, _get, _set, _has, _unset, _mask, _invert, _invertMap, _by, _groupBy, _sortBy, _sortNumbers, _toFixed, _toPrecision, _round, _findLast, _takeWhile, _takeRightWhile, _dropWhile, _dropRightWhile, _countBy, _intersection, _difference, _shuffle, _mapToObject, _findKeyByValue, _anyToError, _anyToErrorObject, _errorToErrorObject, _errorObjectToAppError, _range, _uniq, _uniqBy, _flatten, _flattenDeep, _chunk, SimpleMovingAverage, _average, _averageWeighted, _percentile, _median, _debounce, _throttle, _Debounce, _Throttle, pMap, _passthroughMapper, _passUndefinedMapper, _passthroughPredicate, _passNothingPredicate, _noop, pBatch, ErrorMode, pFilter, pProps, pDelay, pDefer, pHang, pState, AggregatedError, pRetry, pTimeout, pTuple, _Retry, _Timeout, _tryCatch, _TryCatch, _try, pTry, _jsonParseIfPossible, _stringifyAny, _ms, _since, _hb, _gb, _mb, _kb, _snakeCase, _camelCase, _kebabCase, _sum, _sumBy, _clamp, _last, mergeJsonSchemaObjects, jsonSchema, JsonSchemaAnyBuilder, JSON_SCHEMA_ORDER, generateJsonSchemaFromData, _parseQueryString, _defineLazyProperty, _defineLazyProps, _lazyValue, };
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports._isObject = exports._undefinedIfEmpty = exports._filterObject = exports._filterEmptyValues = exports._filterEmptyArrays = exports._filterNullishValues = exports._filterUndefinedValues = exports._filterFalsyValues = exports._omit = exports._pick = exports._truncateMiddle = exports._truncate = exports._nl2br = exports._replaceAll = exports._substringBetweenLast = exports._substringAfterLast = exports._substringAfter = exports._substringBeforeLast = exports._substringBefore = exports._removeWhitespace = exports._split = exports._lowerFirst = exports._upperFirst = exports._capitalize = exports._objectKeys = exports._stringMapEntries = exports._stringMapValues = exports._inRange = exports._createDeterministicRandom = exports._randomArrayItem = exports._randomInt = exports._assertTypeOf = exports._assertIsNumber = exports._assertIsString = exports._assertIsError = exports._assertDeepEquals = exports._assertEquals = exports._assert = exports._isHttpErrorResponse = exports._isHttpErrorObject = exports._isErrorObject = exports.AssertionError = exports.HttpError = exports.AppError = exports._createPromiseDecorator = exports._getArgsSignature = exports._LogMethod = exports._memoFn = exports._Memo = exports.is = void 0;
4
4
  exports._average = exports.SimpleMovingAverage = exports._chunk = exports._flattenDeep = exports._flatten = exports._uniqBy = exports._uniq = exports._range = exports._errorObjectToAppError = exports._errorToErrorObject = exports._anyToErrorObject = exports._anyToError = exports._findKeyByValue = exports._mapToObject = exports._shuffle = exports._difference = exports._intersection = exports._countBy = exports._dropRightWhile = exports._dropWhile = exports._takeRightWhile = exports._takeWhile = exports._findLast = exports._round = exports._toPrecision = exports._toFixed = exports._sortNumbers = exports._sortBy = exports._groupBy = exports._by = exports._invertMap = exports._invert = exports._mask = exports._unset = exports._has = exports._set = exports._get = exports._sortObject = exports._sortObjectDeep = exports._deepTrim = exports._merge = exports._isEmpty = exports._isEmptyObject = exports._deepCopy = exports._deepEquals = exports._objectNullValuesToUndefined = exports._mapObject = exports._mapValues = exports._mapKeys = exports._isPrimitive = void 0;
5
5
  exports.JSON_SCHEMA_ORDER = exports.JsonSchemaAnyBuilder = exports.jsonSchema = exports.mergeJsonSchemaObjects = exports._last = exports._clamp = exports._sumBy = exports._sum = exports._kebabCase = exports._camelCase = exports._snakeCase = exports._kb = exports._mb = exports._gb = exports._hb = exports._since = exports._ms = exports._stringifyAny = exports._jsonParseIfPossible = exports.pTry = exports._try = exports._TryCatch = exports._tryCatch = exports._Timeout = exports._Retry = exports.pTuple = exports.pTimeout = exports.pRetry = exports.AggregatedError = exports.pState = exports.pHang = exports.pDefer = exports.pDelay = exports.pProps = exports.pFilter = exports.ErrorMode = exports.pBatch = exports._noop = exports._passNothingPredicate = exports._passthroughPredicate = exports._passUndefinedMapper = exports._passthroughMapper = exports.pMap = exports._Throttle = exports._Debounce = exports._throttle = exports._debounce = exports._median = exports._percentile = exports._averageWeighted = void 0;
6
- exports._parseQueryString = exports.generateJsonSchemaFromData = void 0;
6
+ exports._lazyValue = exports._defineLazyProps = exports._defineLazyProperty = exports._parseQueryString = exports.generateJsonSchemaFromData = void 0;
7
7
  const array_util_1 = require("./array/array.util");
8
8
  Object.defineProperty(exports, "_by", { enumerable: true, get: function () { return array_util_1._by; } });
9
9
  Object.defineProperty(exports, "_chunk", { enumerable: true, get: function () { return array_util_1._chunk; } });
@@ -26,6 +26,10 @@ Object.defineProperty(exports, "_takeRightWhile", { enumerable: true, get: funct
26
26
  Object.defineProperty(exports, "_takeWhile", { enumerable: true, get: function () { return array_util_1._takeWhile; } });
27
27
  Object.defineProperty(exports, "_uniq", { enumerable: true, get: function () { return array_util_1._uniq; } });
28
28
  Object.defineProperty(exports, "_uniqBy", { enumerable: true, get: function () { return array_util_1._uniqBy; } });
29
+ const lazy_1 = require("./lazy");
30
+ Object.defineProperty(exports, "_defineLazyProperty", { enumerable: true, get: function () { return lazy_1._defineLazyProperty; } });
31
+ Object.defineProperty(exports, "_defineLazyProps", { enumerable: true, get: function () { return lazy_1._defineLazyProps; } });
32
+ Object.defineProperty(exports, "_lazyValue", { enumerable: true, get: function () { return lazy_1._lazyValue; } });
29
33
  const url_util_1 = require("./string/url.util");
30
34
  Object.defineProperty(exports, "_parseQueryString", { enumerable: true, get: function () { return url_util_1._parseQueryString; } });
31
35
  const range_1 = require("./array/range");
package/dist/lazy.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ import { AnyFunction, AnyObject } from './types';
2
+ /**
3
+ * const value = lazyValue(() => expensiveComputation())
4
+ *
5
+ * value() // calls expensiveComputation() once
6
+ * value() // returns cached result
7
+ * value() // returns cached result
8
+ *
9
+ * Based on: https://github.com/sindresorhus/lazy-value
10
+ */
11
+ export declare function _lazyValue<T extends AnyFunction>(fn: T): T;
12
+ /**
13
+ * interface Obj {
14
+ * v: number
15
+ * }
16
+ *
17
+ * const obj = {} as Obj
18
+ *
19
+ * _defineLazyProperty(obj, 'v', () => expensiveComputation())
20
+ * obj.v // runs expensiveComputation() once
21
+ * obj.v // cached value
22
+ * obj.v // cached value
23
+ *
24
+ * Based on: https://github.com/sindresorhus/define-lazy-prop
25
+ */
26
+ export declare function _defineLazyProperty<OBJ extends AnyObject>(obj: OBJ, propertyName: keyof OBJ, fn: AnyFunction): OBJ;
27
+ /**
28
+ * Like _defineLazyProperty, but allows to define multiple props at once.
29
+ */
30
+ export declare function _defineLazyProps<OBJ extends AnyObject>(obj: OBJ, props: Partial<Record<keyof OBJ, AnyFunction>>): OBJ;
package/dist/lazy.js ADDED
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports._defineLazyProps = exports._defineLazyProperty = exports._lazyValue = void 0;
4
+ /**
5
+ * const value = lazyValue(() => expensiveComputation())
6
+ *
7
+ * value() // calls expensiveComputation() once
8
+ * value() // returns cached result
9
+ * value() // returns cached result
10
+ *
11
+ * Based on: https://github.com/sindresorhus/lazy-value
12
+ */
13
+ function _lazyValue(fn) {
14
+ let isCalled = false;
15
+ let result;
16
+ return (() => {
17
+ if (!isCalled) {
18
+ isCalled = true;
19
+ result = fn();
20
+ }
21
+ return result;
22
+ });
23
+ }
24
+ exports._lazyValue = _lazyValue;
25
+ /**
26
+ * interface Obj {
27
+ * v: number
28
+ * }
29
+ *
30
+ * const obj = {} as Obj
31
+ *
32
+ * _defineLazyProperty(obj, 'v', () => expensiveComputation())
33
+ * obj.v // runs expensiveComputation() once
34
+ * obj.v // cached value
35
+ * obj.v // cached value
36
+ *
37
+ * Based on: https://github.com/sindresorhus/define-lazy-prop
38
+ */
39
+ function _defineLazyProperty(obj, propertyName, fn) {
40
+ const define = (value) => Object.defineProperty(obj, propertyName, { value, enumerable: true, writable: true });
41
+ Object.defineProperty(obj, propertyName, {
42
+ configurable: true,
43
+ enumerable: true,
44
+ get() {
45
+ const result = fn();
46
+ define(result);
47
+ return result;
48
+ },
49
+ set(value) {
50
+ define(value);
51
+ },
52
+ });
53
+ return obj;
54
+ }
55
+ exports._defineLazyProperty = _defineLazyProperty;
56
+ /**
57
+ * Like _defineLazyProperty, but allows to define multiple props at once.
58
+ */
59
+ function _defineLazyProps(obj, props) {
60
+ Object.entries(props).forEach(([k, fn]) => _defineLazyProperty(obj, k, fn));
61
+ return obj;
62
+ }
63
+ exports._defineLazyProps = _defineLazyProps;
@@ -20,8 +20,6 @@ class AggregatedError extends Error {
20
20
  super(message);
21
21
  this.errors = mappedErrors;
22
22
  this.results = results;
23
- this.constructor = AggregatedError;
24
- this.__proto__ = AggregatedError.prototype;
25
23
  Object.defineProperty(this, 'name', {
26
24
  value: this.constructor.name,
27
25
  configurable: true,
@@ -32,6 +30,7 @@ class AggregatedError extends Error {
32
30
  else {
33
31
  Object.defineProperty(this, 'stack', {
34
32
  value: new Error().stack,
33
+ writable: true,
35
34
  configurable: true,
36
35
  });
37
36
  }
@@ -18,6 +18,8 @@ export function _chunk(array, size = 1) {
18
18
  /**
19
19
  * Polyfill to Array.flat() with depth=1.
20
20
  * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
21
+ *
22
+ * @deprecated prefer native Array.flat(), it's supported since iOS 12
21
23
  */
22
24
  export function _flatten(arrays) {
23
25
  // to flat single level array
@@ -30,6 +32,8 @@ export function _flatten(arrays) {
30
32
  * @return Returns the new flattened array.
31
33
  *
32
34
  * Based on: https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_flattendeep
35
+ *
36
+ * @deprecated prefer native Array.flat(), it's supported since iOS 12
33
37
  */
34
38
  export function _flattenDeep(arr) {
35
39
  return Array.isArray(arr)
@@ -10,19 +10,23 @@
10
10
  export class AppError extends Error {
11
11
  constructor(message, data = {}) {
12
12
  super(message);
13
- this.data = data;
14
- this.constructor = AppError;
15
- this.__proto__ = AppError.prototype;
16
13
  Object.defineProperty(this, 'name', {
17
14
  value: this.constructor.name,
18
15
  configurable: true,
19
16
  });
17
+ Object.defineProperty(this, 'data', {
18
+ value: data,
19
+ writable: true,
20
+ configurable: true,
21
+ enumerable: false,
22
+ });
20
23
  if (Error.captureStackTrace) {
21
24
  Error.captureStackTrace(this, this.constructor);
22
25
  }
23
26
  else {
24
27
  Object.defineProperty(this, 'stack', {
25
28
  value: new Error().stack,
29
+ writable: true,
26
30
  configurable: true,
27
31
  });
28
32
  }
@@ -84,22 +84,7 @@ export function _assertTypeOf(v, expectedType, message) {
84
84
  }
85
85
  }
86
86
  export class AssertionError extends AppError {
87
- constructor(message, data) {
87
+ constructor(message, data = {}) {
88
88
  super(message, data);
89
- this.constructor = AssertionError;
90
- this.__proto__ = AssertionError.prototype;
91
- Object.defineProperty(this, 'name', {
92
- value: this.constructor.name,
93
- configurable: true, // otherwise throws with "TypeError: Cannot redefine property: name"
94
- });
95
- if (Error.captureStackTrace) {
96
- Error.captureStackTrace(this, this.constructor);
97
- }
98
- else {
99
- Object.defineProperty(this, 'stack', {
100
- value: new Error().stack,
101
- configurable: true,
102
- });
103
- }
104
89
  }
105
90
  }
@@ -5,20 +5,5 @@ import { AppError } from './app.error';
5
5
  export class HttpError extends AppError {
6
6
  constructor(message, data) {
7
7
  super(message, data);
8
- this.constructor = HttpError;
9
- this.__proto__ = HttpError.prototype;
10
- Object.defineProperty(this, 'name', {
11
- value: this.constructor.name,
12
- configurable: true, // otherwise throws with "TypeError: Cannot redefine property: name"
13
- });
14
- if (Error.captureStackTrace) {
15
- Error.captureStackTrace(this, this.constructor);
16
- }
17
- else {
18
- Object.defineProperty(this, 'stack', {
19
- value: new Error().stack,
20
- configurable: true,
21
- });
22
- }
23
8
  }
24
9
  }
package/dist-esm/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { _by, _chunk, _countBy, _difference, _dropRightWhile, _dropWhile, _findLast, _flatten, _flattenDeep, _groupBy, _intersection, _last, _mapToObject, _shuffle, _sortBy, _sum, _sumBy, _takeRightWhile, _takeWhile, _uniq, _uniqBy, } from './array/array.util';
2
+ import { _defineLazyProperty, _defineLazyProps, _lazyValue } from './lazy';
2
3
  import { _parseQueryString } from './string/url.util';
3
4
  import { _range } from './array/range';
4
5
  import { _createPromiseDecorator, } from './decorators/createPromiseDecorator';
@@ -49,4 +50,4 @@ import { _ms, _since } from './time/time.util';
49
50
  import { _noop, _objectKeys, _passNothingPredicate, _passthroughMapper, _passthroughPredicate, _passUndefinedMapper, _stringMapEntries, _stringMapValues, } from './types';
50
51
  import { _gb, _hb, _kb, _mb } from './unit/size.util';
51
52
  import { is } from './vendor/is';
52
- export { is, _Memo, _memoFn, _LogMethod, _getArgsSignature, _createPromiseDecorator, AppError, HttpError, AssertionError, _isErrorObject, _isHttpErrorObject, _isHttpErrorResponse, _assert, _assertEquals, _assertDeepEquals, _assertIsError, _assertIsString, _assertIsNumber, _assertTypeOf, _randomInt, _randomArrayItem, _createDeterministicRandom, _inRange, _stringMapValues, _stringMapEntries, _objectKeys, _capitalize, _upperFirst, _lowerFirst, _split, _removeWhitespace, _substringBefore, _substringBeforeLast, _substringAfter, _substringAfterLast, _substringBetweenLast, _replaceAll, _nl2br, _truncate, _truncateMiddle, _pick, _omit, _filterFalsyValues, _filterUndefinedValues, _filterNullishValues, _filterEmptyArrays, _filterEmptyValues, _filterObject, _undefinedIfEmpty, _isObject, _isPrimitive, _mapKeys, _mapValues, _mapObject, _objectNullValuesToUndefined, _deepEquals, _deepCopy, _isEmptyObject, _isEmpty, _merge, _deepTrim, _sortObjectDeep, _sortObject, _get, _set, _has, _unset, _mask, _invert, _invertMap, _by, _groupBy, _sortBy, _sortNumbers, _toFixed, _toPrecision, _round, _findLast, _takeWhile, _takeRightWhile, _dropWhile, _dropRightWhile, _countBy, _intersection, _difference, _shuffle, _mapToObject, _findKeyByValue, _anyToError, _anyToErrorObject, _errorToErrorObject, _errorObjectToAppError, _range, _uniq, _uniqBy, _flatten, _flattenDeep, _chunk, SimpleMovingAverage, _average, _averageWeighted, _percentile, _median, _debounce, _throttle, _Debounce, _Throttle, pMap, _passthroughMapper, _passUndefinedMapper, _passthroughPredicate, _passNothingPredicate, _noop, pBatch, ErrorMode, pFilter, pProps, pDelay, pDefer, pHang, pState, AggregatedError, pRetry, pTimeout, pTuple, _Retry, _Timeout, _tryCatch, _TryCatch, _try, pTry, _jsonParseIfPossible, _stringifyAny, _ms, _since, _hb, _gb, _mb, _kb, _snakeCase, _camelCase, _kebabCase, _sum, _sumBy, _clamp, _last, mergeJsonSchemaObjects, jsonSchema, JsonSchemaAnyBuilder, JSON_SCHEMA_ORDER, generateJsonSchemaFromData, _parseQueryString, };
53
+ export { is, _Memo, _memoFn, _LogMethod, _getArgsSignature, _createPromiseDecorator, AppError, HttpError, AssertionError, _isErrorObject, _isHttpErrorObject, _isHttpErrorResponse, _assert, _assertEquals, _assertDeepEquals, _assertIsError, _assertIsString, _assertIsNumber, _assertTypeOf, _randomInt, _randomArrayItem, _createDeterministicRandom, _inRange, _stringMapValues, _stringMapEntries, _objectKeys, _capitalize, _upperFirst, _lowerFirst, _split, _removeWhitespace, _substringBefore, _substringBeforeLast, _substringAfter, _substringAfterLast, _substringBetweenLast, _replaceAll, _nl2br, _truncate, _truncateMiddle, _pick, _omit, _filterFalsyValues, _filterUndefinedValues, _filterNullishValues, _filterEmptyArrays, _filterEmptyValues, _filterObject, _undefinedIfEmpty, _isObject, _isPrimitive, _mapKeys, _mapValues, _mapObject, _objectNullValuesToUndefined, _deepEquals, _deepCopy, _isEmptyObject, _isEmpty, _merge, _deepTrim, _sortObjectDeep, _sortObject, _get, _set, _has, _unset, _mask, _invert, _invertMap, _by, _groupBy, _sortBy, _sortNumbers, _toFixed, _toPrecision, _round, _findLast, _takeWhile, _takeRightWhile, _dropWhile, _dropRightWhile, _countBy, _intersection, _difference, _shuffle, _mapToObject, _findKeyByValue, _anyToError, _anyToErrorObject, _errorToErrorObject, _errorObjectToAppError, _range, _uniq, _uniqBy, _flatten, _flattenDeep, _chunk, SimpleMovingAverage, _average, _averageWeighted, _percentile, _median, _debounce, _throttle, _Debounce, _Throttle, pMap, _passthroughMapper, _passUndefinedMapper, _passthroughPredicate, _passNothingPredicate, _noop, pBatch, ErrorMode, pFilter, pProps, pDelay, pDefer, pHang, pState, AggregatedError, pRetry, pTimeout, pTuple, _Retry, _Timeout, _tryCatch, _TryCatch, _try, pTry, _jsonParseIfPossible, _stringifyAny, _ms, _since, _hb, _gb, _mb, _kb, _snakeCase, _camelCase, _kebabCase, _sum, _sumBy, _clamp, _last, mergeJsonSchemaObjects, jsonSchema, JsonSchemaAnyBuilder, JSON_SCHEMA_ORDER, generateJsonSchemaFromData, _parseQueryString, _defineLazyProperty, _defineLazyProps, _lazyValue, };
@@ -0,0 +1,57 @@
1
+ /**
2
+ * const value = lazyValue(() => expensiveComputation())
3
+ *
4
+ * value() // calls expensiveComputation() once
5
+ * value() // returns cached result
6
+ * value() // returns cached result
7
+ *
8
+ * Based on: https://github.com/sindresorhus/lazy-value
9
+ */
10
+ export function _lazyValue(fn) {
11
+ let isCalled = false;
12
+ let result;
13
+ return (() => {
14
+ if (!isCalled) {
15
+ isCalled = true;
16
+ result = fn();
17
+ }
18
+ return result;
19
+ });
20
+ }
21
+ /**
22
+ * interface Obj {
23
+ * v: number
24
+ * }
25
+ *
26
+ * const obj = {} as Obj
27
+ *
28
+ * _defineLazyProperty(obj, 'v', () => expensiveComputation())
29
+ * obj.v // runs expensiveComputation() once
30
+ * obj.v // cached value
31
+ * obj.v // cached value
32
+ *
33
+ * Based on: https://github.com/sindresorhus/define-lazy-prop
34
+ */
35
+ export function _defineLazyProperty(obj, propertyName, fn) {
36
+ const define = (value) => Object.defineProperty(obj, propertyName, { value, enumerable: true, writable: true });
37
+ Object.defineProperty(obj, propertyName, {
38
+ configurable: true,
39
+ enumerable: true,
40
+ get() {
41
+ const result = fn();
42
+ define(result);
43
+ return result;
44
+ },
45
+ set(value) {
46
+ define(value);
47
+ },
48
+ });
49
+ return obj;
50
+ }
51
+ /**
52
+ * Like _defineLazyProperty, but allows to define multiple props at once.
53
+ */
54
+ export function _defineLazyProps(obj, props) {
55
+ Object.entries(props).forEach(([k, fn]) => _defineLazyProperty(obj, k, fn));
56
+ return obj;
57
+ }
@@ -17,8 +17,6 @@ export class AggregatedError extends Error {
17
17
  super(message);
18
18
  this.errors = mappedErrors;
19
19
  this.results = results;
20
- this.constructor = AggregatedError;
21
- this.__proto__ = AggregatedError.prototype;
22
20
  Object.defineProperty(this, 'name', {
23
21
  value: this.constructor.name,
24
22
  configurable: true,
@@ -29,6 +27,7 @@ export class AggregatedError extends Error {
29
27
  else {
30
28
  Object.defineProperty(this, 'stack', {
31
29
  value: new Error().stack,
30
+ writable: true,
32
31
  configurable: true,
33
32
  });
34
33
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.55.0",
3
+ "version": "14.58.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -22,6 +22,8 @@ export function _chunk<T>(array: readonly T[], size = 1): T[][] {
22
22
  /**
23
23
  * Polyfill to Array.flat() with depth=1.
24
24
  * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
25
+ *
26
+ * @deprecated prefer native Array.flat(), it's supported since iOS 12
25
27
  */
26
28
  export function _flatten<T>(arrays: T[][]): T[] {
27
29
  // to flat single level array
@@ -35,6 +37,8 @@ export function _flatten<T>(arrays: T[][]): T[] {
35
37
  * @return Returns the new flattened array.
36
38
  *
37
39
  * Based on: https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_flattendeep
40
+ *
41
+ * @deprecated prefer native Array.flat(), it's supported since iOS 12
38
42
  */
39
43
  export function _flattenDeep<T>(arr: RecursiveArray<T>): T[] {
40
44
  return Array.isArray(arr)
@@ -10,21 +10,29 @@ import { ErrorData } from './error.model'
10
10
  * Based on: https://medium.com/@xpl/javascript-deriving-from-error-properly-8d2f8f315801
11
11
  */
12
12
  export class AppError<DATA_TYPE extends ErrorData = ErrorData> extends Error {
13
- constructor(message: string, public data = {} as DATA_TYPE) {
13
+ data!: DATA_TYPE
14
+
15
+ constructor(message: string, data = {} as DATA_TYPE) {
14
16
  super(message)
15
17
 
16
- this.constructor = AppError
17
- ;(this as any).__proto__ = AppError.prototype
18
18
  Object.defineProperty(this, 'name', {
19
19
  value: this.constructor.name,
20
20
  configurable: true,
21
21
  })
22
22
 
23
+ Object.defineProperty(this, 'data', {
24
+ value: data,
25
+ writable: true,
26
+ configurable: true,
27
+ enumerable: false,
28
+ })
29
+
23
30
  if (Error.captureStackTrace) {
24
31
  Error.captureStackTrace(this, this.constructor)
25
32
  } else {
26
33
  Object.defineProperty(this, 'stack', {
27
34
  value: new Error().stack, // eslint-disable-line unicorn/error-message
35
+ writable: true,
28
36
  configurable: true,
29
37
  })
30
38
  }
@@ -123,23 +123,7 @@ export function _assertTypeOf<T>(v: any, expectedType: string, message?: string)
123
123
  }
124
124
 
125
125
  export class AssertionError extends AppError {
126
- constructor(message: string, data: ErrorData) {
126
+ constructor(message: string, data = {} as ErrorData) {
127
127
  super(message, data)
128
-
129
- this.constructor = AssertionError
130
- ;(this as any).__proto__ = AssertionError.prototype
131
- Object.defineProperty(this, 'name', {
132
- value: this.constructor.name,
133
- configurable: true, // otherwise throws with "TypeError: Cannot redefine property: name"
134
- })
135
-
136
- if (Error.captureStackTrace) {
137
- Error.captureStackTrace(this, this.constructor)
138
- } else {
139
- Object.defineProperty(this, 'stack', {
140
- value: new Error().stack, // eslint-disable-line unicorn/error-message
141
- configurable: true,
142
- })
143
- }
144
128
  }
145
129
  }
@@ -9,21 +9,5 @@ export class HttpError<
9
9
  > extends AppError<DATA_TYPE> {
10
10
  constructor(message: string, data: DATA_TYPE) {
11
11
  super(message, data)
12
-
13
- this.constructor = HttpError
14
- ;(this as any).__proto__ = HttpError.prototype
15
- Object.defineProperty(this, 'name', {
16
- value: this.constructor.name,
17
- configurable: true, // otherwise throws with "TypeError: Cannot redefine property: name"
18
- })
19
-
20
- if (Error.captureStackTrace) {
21
- Error.captureStackTrace(this, this.constructor)
22
- } else {
23
- Object.defineProperty(this, 'stack', {
24
- value: new Error().stack, // eslint-disable-line unicorn/error-message
25
- configurable: true,
26
- })
27
- }
28
12
  }
29
13
  }
package/src/index.ts CHANGED
@@ -21,6 +21,7 @@ import {
21
21
  _uniq,
22
22
  _uniqBy,
23
23
  } from './array/array.util'
24
+ import { _defineLazyProperty, _defineLazyProps, _lazyValue } from './lazy'
24
25
  import { _parseQueryString } from './string/url.util'
25
26
  import { _range } from './array/range'
26
27
  import {
@@ -449,4 +450,7 @@ export {
449
450
  JSON_SCHEMA_ORDER,
450
451
  generateJsonSchemaFromData,
451
452
  _parseQueryString,
453
+ _defineLazyProperty,
454
+ _defineLazyProps,
455
+ _lazyValue,
452
456
  }