@naturalcycles/js-lib 14.166.1 → 14.168.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.
@@ -48,14 +48,6 @@ export interface ErrorData {
48
48
  * Sentry takes string[], but for convenience we allow to pass a singe string.
49
49
  */
50
50
  fingerprint?: string | string[];
51
- /**
52
- * If set to true - it'll use error.message as fingerprint,
53
- * so, all errors with the same message will be grouped together, even if they occurred in different places.
54
- * Defaults to false.
55
- *
56
- * @experimental
57
- */
58
- fingerprintByMessage?: boolean;
59
51
  /**
60
52
  * Set when throwing an error from your backend code, to indicate desired http status code.
61
53
  * e.g throw new AppError('oj', { backendResponseStatusCode: 401 })
@@ -1,3 +1,10 @@
1
+ /**
2
+ * deepEquals, but after JSON stringify/parse
3
+ * E.g if object A has extra properties with value `undefined` -
4
+ * it won't be _deepEquals true, but will be _deepJsonEquals true.
5
+ * (because JSON.stringify removes undefined properties).
6
+ */
7
+ export declare function _deepJsonEquals(a: any, b: any): boolean;
1
8
  /**
2
9
  * Based on: https://github.com/epoberezkin/fast-deep-equal/
3
10
  */
@@ -1,45 +1,60 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._deepEquals = void 0;
3
+ exports._deepEquals = exports._deepJsonEquals = void 0;
4
4
  const isArray = Array.isArray;
5
5
  const keyList = Object.keys;
6
6
  const hasProp = Object.prototype.hasOwnProperty;
7
- /* eslint-disable eqeqeq */
7
+ /**
8
+ * deepEquals, but after JSON stringify/parse
9
+ * E.g if object A has extra properties with value `undefined` -
10
+ * it won't be _deepEquals true, but will be _deepJsonEquals true.
11
+ * (because JSON.stringify removes undefined properties).
12
+ */
13
+ function _deepJsonEquals(a, b) {
14
+ if (a === b)
15
+ return true;
16
+ const aj = JSON.stringify(a);
17
+ const bj = JSON.stringify(b);
18
+ if (aj === bj)
19
+ return true;
20
+ return _deepEquals(JSON.parse(aj), JSON.parse(bj));
21
+ }
22
+ exports._deepJsonEquals = _deepJsonEquals;
8
23
  /**
9
24
  * Based on: https://github.com/epoberezkin/fast-deep-equal/
10
25
  */
11
26
  function _deepEquals(a, b) {
12
27
  if (a === b)
13
28
  return true;
14
- if (a && b && typeof a == 'object' && typeof b == 'object') {
29
+ if (a && b && typeof a === 'object' && typeof b === 'object') {
15
30
  const arrA = isArray(a);
16
31
  const arrB = isArray(b);
17
32
  let i;
18
33
  let length;
19
34
  let key;
35
+ if (arrA !== arrB)
36
+ return false;
20
37
  if (arrA && arrB) {
21
38
  length = a.length;
22
- if (length != b.length)
39
+ if (length !== b.length)
23
40
  return false;
24
41
  for (i = length; i-- !== 0;)
25
42
  if (!_deepEquals(a[i], b[i]))
26
43
  return false;
27
44
  return true;
28
45
  }
29
- if (arrA != arrB)
30
- return false;
31
46
  const dateA = a instanceof Date;
32
47
  const dateB = b instanceof Date;
33
- if (dateA != dateB)
48
+ if (dateA !== dateB)
34
49
  return false;
35
50
  if (dateA && dateB)
36
- return a.getTime() == b.getTime();
51
+ return a.getTime() === b.getTime();
37
52
  const regexpA = a instanceof RegExp;
38
53
  const regexpB = b instanceof RegExp;
39
- if (regexpA != regexpB)
54
+ if (regexpA !== regexpB)
40
55
  return false;
41
56
  if (regexpA && regexpB)
42
- return a.toString() == b.toString();
57
+ return a.toString() === b.toString();
43
58
  const keys = keyList(a);
44
59
  length = keys.length;
45
60
  if (length !== keyList(b).length)
@@ -32,6 +32,12 @@ export interface StringifyAnyOptions {
32
32
  * @default true
33
33
  */
34
34
  includeErrorCause?: boolean;
35
+ /**
36
+ * Set to true to include Error.data.
37
+ *
38
+ * @default false
39
+ */
40
+ includeErrorData?: boolean;
35
41
  /**
36
42
  * Allows to pass custom "stringify function".
37
43
  * E.g in Node.js you can pass `util.inspect` instead.
@@ -78,6 +78,9 @@ function _stringifyAny(obj, opt = {}) {
78
78
  // Error that has no `data`, but has `code` property
79
79
  s += `\ncode: ${obj.code}`;
80
80
  }
81
+ if (opt.includeErrorData && (0, error_util_1._isErrorObject)(obj)) {
82
+ s += '\n' + _stringifyAny(obj.data, opt);
83
+ }
81
84
  if (opt.includeErrorStack && obj.stack) {
82
85
  // Here we're using the previously-generated "title line" (e.g "Error: some_message"),
83
86
  // concatenating it with the Stack (but without the title line of the Stack)
@@ -1,42 +1,56 @@
1
1
  const isArray = Array.isArray;
2
2
  const keyList = Object.keys;
3
3
  const hasProp = Object.prototype.hasOwnProperty;
4
- /* eslint-disable eqeqeq */
4
+ /**
5
+ * deepEquals, but after JSON stringify/parse
6
+ * E.g if object A has extra properties with value `undefined` -
7
+ * it won't be _deepEquals true, but will be _deepJsonEquals true.
8
+ * (because JSON.stringify removes undefined properties).
9
+ */
10
+ export function _deepJsonEquals(a, b) {
11
+ if (a === b)
12
+ return true;
13
+ const aj = JSON.stringify(a);
14
+ const bj = JSON.stringify(b);
15
+ if (aj === bj)
16
+ return true;
17
+ return _deepEquals(JSON.parse(aj), JSON.parse(bj));
18
+ }
5
19
  /**
6
20
  * Based on: https://github.com/epoberezkin/fast-deep-equal/
7
21
  */
8
22
  export function _deepEquals(a, b) {
9
23
  if (a === b)
10
24
  return true;
11
- if (a && b && typeof a == 'object' && typeof b == 'object') {
25
+ if (a && b && typeof a === 'object' && typeof b === 'object') {
12
26
  const arrA = isArray(a);
13
27
  const arrB = isArray(b);
14
28
  let i;
15
29
  let length;
16
30
  let key;
31
+ if (arrA !== arrB)
32
+ return false;
17
33
  if (arrA && arrB) {
18
34
  length = a.length;
19
- if (length != b.length)
35
+ if (length !== b.length)
20
36
  return false;
21
37
  for (i = length; i-- !== 0;)
22
38
  if (!_deepEquals(a[i], b[i]))
23
39
  return false;
24
40
  return true;
25
41
  }
26
- if (arrA != arrB)
27
- return false;
28
42
  const dateA = a instanceof Date;
29
43
  const dateB = b instanceof Date;
30
- if (dateA != dateB)
44
+ if (dateA !== dateB)
31
45
  return false;
32
46
  if (dateA && dateB)
33
- return a.getTime() == b.getTime();
47
+ return a.getTime() === b.getTime();
34
48
  const regexpA = a instanceof RegExp;
35
49
  const regexpB = b instanceof RegExp;
36
- if (regexpA != regexpB)
50
+ if (regexpA !== regexpB)
37
51
  return false;
38
52
  if (regexpA && regexpB)
39
- return a.toString() == b.toString();
53
+ return a.toString() === b.toString();
40
54
  const keys = keyList(a);
41
55
  length = keys.length;
42
56
  if (length !== keyList(b).length)
@@ -1,4 +1,4 @@
1
- import { _isBackendErrorResponseObject, _isErrorLike } from '../error/error.util';
1
+ import { _isBackendErrorResponseObject, _isErrorLike, _isErrorObject } from '../error/error.util';
2
2
  import { _jsonParseIfPossible } from './json.util';
3
3
  import { _safeJsonStringify } from './safeJsonStringify';
4
4
  const supportsAggregateError = typeof globalThis.AggregateError === 'function';
@@ -74,6 +74,9 @@ export function _stringifyAny(obj, opt = {}) {
74
74
  // Error that has no `data`, but has `code` property
75
75
  s += `\ncode: ${obj.code}`;
76
76
  }
77
+ if (opt.includeErrorData && _isErrorObject(obj)) {
78
+ s += '\n' + _stringifyAny(obj.data, opt);
79
+ }
77
80
  if (opt.includeErrorStack && obj.stack) {
78
81
  // Here we're using the previously-generated "title line" (e.g "Error: some_message"),
79
82
  // concatenating it with the Stack (but without the title line of the Stack)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.166.1",
3
+ "version": "14.168.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -59,15 +59,6 @@ export interface ErrorData {
59
59
  */
60
60
  fingerprint?: string | string[]
61
61
 
62
- /**
63
- * If set to true - it'll use error.message as fingerprint,
64
- * so, all errors with the same message will be grouped together, even if they occurred in different places.
65
- * Defaults to false.
66
- *
67
- * @experimental
68
- */
69
- fingerprintByMessage?: boolean
70
-
71
62
  /**
72
63
  * Set when throwing an error from your backend code, to indicate desired http status code.
73
64
  * e.g throw new AppError('oj', { backendResponseStatusCode: 401 })
@@ -2,7 +2,19 @@ const isArray = Array.isArray
2
2
  const keyList = Object.keys
3
3
  const hasProp = Object.prototype.hasOwnProperty
4
4
 
5
- /* eslint-disable eqeqeq */
5
+ /**
6
+ * deepEquals, but after JSON stringify/parse
7
+ * E.g if object A has extra properties with value `undefined` -
8
+ * it won't be _deepEquals true, but will be _deepJsonEquals true.
9
+ * (because JSON.stringify removes undefined properties).
10
+ */
11
+ export function _deepJsonEquals(a: any, b: any): boolean {
12
+ if (a === b) return true
13
+ const aj = JSON.stringify(a)
14
+ const bj = JSON.stringify(b)
15
+ if (aj === bj) return true
16
+ return _deepEquals(JSON.parse(aj), JSON.parse(bj))
17
+ }
6
18
 
7
19
  /**
8
20
  * Based on: https://github.com/epoberezkin/fast-deep-equal/
@@ -10,31 +22,31 @@ const hasProp = Object.prototype.hasOwnProperty
10
22
  export function _deepEquals(a: any, b: any): boolean {
11
23
  if (a === b) return true
12
24
 
13
- if (a && b && typeof a == 'object' && typeof b == 'object') {
25
+ if (a && b && typeof a === 'object' && typeof b === 'object') {
14
26
  const arrA = isArray(a)
15
27
  const arrB = isArray(b)
16
- let i
17
- let length
28
+ let i: number
29
+ let length: number
18
30
  let key: string
19
31
 
32
+ if (arrA !== arrB) return false
33
+
20
34
  if (arrA && arrB) {
21
35
  length = a.length
22
- if (length != b.length) return false
36
+ if (length !== b.length) return false
23
37
  for (i = length; i-- !== 0; ) if (!_deepEquals(a[i], b[i])) return false
24
38
  return true
25
39
  }
26
40
 
27
- if (arrA != arrB) return false
28
-
29
41
  const dateA = a instanceof Date
30
42
  const dateB = b instanceof Date
31
- if (dateA != dateB) return false
32
- if (dateA && dateB) return a.getTime() == b.getTime()
43
+ if (dateA !== dateB) return false
44
+ if (dateA && dateB) return a.getTime() === b.getTime()
33
45
 
34
46
  const regexpA = a instanceof RegExp
35
47
  const regexpB = b instanceof RegExp
36
- if (regexpA != regexpB) return false
37
- if (regexpA && regexpB) return a.toString() == b.toString()
48
+ if (regexpA !== regexpB) return false
49
+ if (regexpA && regexpB) return a.toString() === b.toString()
38
50
 
39
51
  const keys = keyList(a)
40
52
  length = keys.length
@@ -1,4 +1,4 @@
1
- import { _isBackendErrorResponseObject, _isErrorLike } from '../error/error.util'
1
+ import { _isBackendErrorResponseObject, _isErrorLike, _isErrorObject } from '../error/error.util'
2
2
  import type { Reviver } from '../types'
3
3
  import { _jsonParseIfPossible } from './json.util'
4
4
  import { _safeJsonStringify } from './safeJsonStringify'
@@ -47,6 +47,13 @@ export interface StringifyAnyOptions {
47
47
  */
48
48
  includeErrorCause?: boolean
49
49
 
50
+ /**
51
+ * Set to true to include Error.data.
52
+ *
53
+ * @default false
54
+ */
55
+ includeErrorData?: boolean
56
+
50
57
  /**
51
58
  * Allows to pass custom "stringify function".
52
59
  * E.g in Node.js you can pass `util.inspect` instead.
@@ -115,6 +122,10 @@ export function _stringifyAny(obj: any, opt: StringifyAnyOptions = {}): string {
115
122
  s += `\ncode: ${(obj as any).code}`
116
123
  }
117
124
 
125
+ if (opt.includeErrorData && _isErrorObject(obj)) {
126
+ s += '\n' + _stringifyAny(obj.data, opt)
127
+ }
128
+
118
129
  if (opt.includeErrorStack && obj.stack) {
119
130
  // Here we're using the previously-generated "title line" (e.g "Error: some_message"),
120
131
  // concatenating it with the Stack (but without the title line of the Stack)