@naturalcycles/js-lib 14.248.0 → 14.249.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.
@@ -38,7 +38,7 @@
38
38
 
39
39
  TLDR: _deepEquals should be useful in most of the cases, start there.
40
40
  */
41
- export declare function _deepEquals(a: any, b: any): boolean;
41
+ export declare function _deepEquals<T>(a: T, b: T): boolean;
42
42
  /**
43
43
  Returns true if a and b are deeply equal.
44
44
 
@@ -53,7 +53,7 @@ export declare function _deepEquals(a: any, b: any): boolean;
53
53
 
54
54
  See _deepEquals docs for more details and comparison.
55
55
  */
56
- export declare function _deepJsonEquals(a: any, b: any): boolean;
56
+ export declare function _deepJsonEquals<T>(a: T, b: T): boolean;
57
57
  /**
58
58
  * Shortcut for JSON.stringify(a) === JSON.stringify(b)
59
59
  *
@@ -55,7 +55,7 @@ function _deepEquals(a, b) {
55
55
  return false;
56
56
  if (Array.isArray(a)) {
57
57
  const length = a.length;
58
- if (length !== b.length)
58
+ if (!Array.isArray(b) || length !== b.length)
59
59
  return false;
60
60
  for (let i = length; i-- !== 0;) {
61
61
  if (!_deepEquals(a[i], b[i]))
@@ -79,8 +79,9 @@ function _deepEquals(a, b) {
79
79
  }
80
80
  return true;
81
81
  }
82
- if (a.constructor === RegExp)
82
+ if (a.constructor === RegExp) {
83
83
  return a.source === b.source && a.flags === b.flags;
84
+ }
84
85
  if (a.valueOf !== Object.prototype.valueOf)
85
86
  return a.valueOf() === b.valueOf();
86
87
  if (a.toString !== Object.prototype.toString)
@@ -111,13 +112,16 @@ function _deepJsonEquals(a, b) {
111
112
  if (a === b)
112
113
  return true;
113
114
  if (Number.isNaN(a)) {
115
+ ;
114
116
  a = null;
115
117
  }
116
118
  else if (typeof a === 'function') {
119
+ ;
117
120
  a = undefined;
118
121
  }
119
122
  else if (a && typeof a === 'object') {
120
123
  if (a instanceof Date) {
124
+ ;
121
125
  a = a.valueOf();
122
126
  }
123
127
  else if ('toJSON' in a) {
@@ -125,13 +129,16 @@ function _deepJsonEquals(a, b) {
125
129
  }
126
130
  }
127
131
  if (Number.isNaN(b)) {
132
+ ;
128
133
  b = null;
129
134
  }
130
135
  else if (typeof b === 'function') {
136
+ ;
131
137
  b = undefined;
132
138
  }
133
139
  else if (b && typeof b === 'object') {
134
140
  if (b instanceof Date) {
141
+ ;
135
142
  b = b.valueOf();
136
143
  }
137
144
  else if ('toJSON' in b) {
@@ -141,7 +148,7 @@ function _deepJsonEquals(a, b) {
141
148
  if (a && b && typeof a === 'object' && typeof b === 'object') {
142
149
  if (Array.isArray(a)) {
143
150
  const length = a.length;
144
- if (length !== b.length)
151
+ if (!Array.isArray(b) || length !== b.length)
145
152
  return false;
146
153
  for (let i = length; i-- !== 0;) {
147
154
  if (!_deepJsonEquals(a[i], b[i]))
@@ -51,7 +51,9 @@ async function pRetry(fn, opt = {}) {
51
51
  }
52
52
  catch (err) {
53
53
  if (logFailures) {
54
- logger.error(`${fname} attempt #${attempt} error in ${(0, __1._since)(started)}:`, err);
54
+ // Logger at warn (not error) level, because it's not a fatal error, but a retriable one
55
+ // Fatal one is not logged either, because it's been thrown instead
56
+ logger.warn(`${fname} attempt #${attempt} error in ${(0, __1._since)(started)}:`, err);
55
57
  }
56
58
  if (attempt >= maxAttempts || (predicate && !predicate(err, attempt, maxAttempts))) {
57
59
  // Give up
@@ -50,7 +50,7 @@ export function _deepEquals(a, b) {
50
50
  return false;
51
51
  if (Array.isArray(a)) {
52
52
  const length = a.length;
53
- if (length !== b.length)
53
+ if (!Array.isArray(b) || length !== b.length)
54
54
  return false;
55
55
  for (let i = length; i-- !== 0;) {
56
56
  if (!_deepEquals(a[i], b[i]))
@@ -74,8 +74,9 @@ export function _deepEquals(a, b) {
74
74
  }
75
75
  return true;
76
76
  }
77
- if (a.constructor === RegExp)
77
+ if (a.constructor === RegExp) {
78
78
  return a.source === b.source && a.flags === b.flags;
79
+ }
79
80
  if (a.valueOf !== Object.prototype.valueOf)
80
81
  return a.valueOf() === b.valueOf();
81
82
  if (a.toString !== Object.prototype.toString)
@@ -106,13 +107,16 @@ export function _deepJsonEquals(a, b) {
106
107
  if (a === b)
107
108
  return true;
108
109
  if (Number.isNaN(a)) {
110
+ ;
109
111
  a = null;
110
112
  }
111
113
  else if (typeof a === 'function') {
114
+ ;
112
115
  a = undefined;
113
116
  }
114
117
  else if (a && typeof a === 'object') {
115
118
  if (a instanceof Date) {
119
+ ;
116
120
  a = a.valueOf();
117
121
  }
118
122
  else if ('toJSON' in a) {
@@ -120,13 +124,16 @@ export function _deepJsonEquals(a, b) {
120
124
  }
121
125
  }
122
126
  if (Number.isNaN(b)) {
127
+ ;
123
128
  b = null;
124
129
  }
125
130
  else if (typeof b === 'function') {
131
+ ;
126
132
  b = undefined;
127
133
  }
128
134
  else if (b && typeof b === 'object') {
129
135
  if (b instanceof Date) {
136
+ ;
130
137
  b = b.valueOf();
131
138
  }
132
139
  else if ('toJSON' in b) {
@@ -136,7 +143,7 @@ export function _deepJsonEquals(a, b) {
136
143
  if (a && b && typeof a === 'object' && typeof b === 'object') {
137
144
  if (Array.isArray(a)) {
138
145
  const length = a.length;
139
- if (length !== b.length)
146
+ if (!Array.isArray(b) || length !== b.length)
140
147
  return false;
141
148
  for (let i = length; i-- !== 0;) {
142
149
  if (!_deepJsonEquals(a[i], b[i]))
@@ -47,7 +47,9 @@ export async function pRetry(fn, opt = {}) {
47
47
  }
48
48
  catch (err) {
49
49
  if (logFailures) {
50
- logger.error(`${fname} attempt #${attempt} error in ${_since(started)}:`, err);
50
+ // Logger at warn (not error) level, because it's not a fatal error, but a retriable one
51
+ // Fatal one is not logged either, because it's been thrown instead
52
+ logger.warn(`${fname} attempt #${attempt} error in ${_since(started)}:`, err);
51
53
  }
52
54
  if (attempt >= maxAttempts || (predicate && !predicate(err, attempt, maxAttempts))) {
53
55
  // Give up
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.248.0",
3
+ "version": "14.249.0",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build": "dev-lib build-esm-cjs",
@@ -40,7 +40,7 @@
40
40
 
41
41
  TLDR: _deepEquals should be useful in most of the cases, start there.
42
42
  */
43
- export function _deepEquals(a: any, b: any): boolean {
43
+ export function _deepEquals<T>(a: T, b: T): boolean {
44
44
  if (a === b) return true
45
45
 
46
46
  if (Number.isNaN(a)) {
@@ -52,7 +52,7 @@ export function _deepEquals(a: any, b: any): boolean {
52
52
 
53
53
  if (Array.isArray(a)) {
54
54
  const length = a.length
55
- if (length !== b.length) return false
55
+ if (!Array.isArray(b) || length !== b.length) return false
56
56
  for (let i = length; i-- !== 0; ) {
57
57
  if (!_deepEquals(a[i], b[i])) return false
58
58
  }
@@ -74,12 +74,14 @@ export function _deepEquals(a: any, b: any): boolean {
74
74
  return true
75
75
  }
76
76
 
77
- if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags
77
+ if (a.constructor === RegExp) {
78
+ return (a as RegExp).source === (b as any).source && (a as RegExp).flags === (b as any).flags
79
+ }
78
80
  if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf()
79
81
  if (a.toString !== Object.prototype.toString) return a.toString() === b.toString()
80
82
 
81
83
  for (const key of new Set([...Object.keys(a), ...Object.keys(b)])) {
82
- if (!_deepEquals(a[key], b[key])) return false
84
+ if (!_deepEquals(a[key as keyof T], b[key as keyof T])) return false
83
85
  }
84
86
 
85
87
  return true
@@ -102,36 +104,36 @@ export function _deepEquals(a: any, b: any): boolean {
102
104
 
103
105
  See _deepEquals docs for more details and comparison.
104
106
  */
105
- export function _deepJsonEquals(a: any, b: any): boolean {
107
+ export function _deepJsonEquals<T>(a: T, b: T): boolean {
106
108
  if (a === b) return true
107
109
 
108
110
  if (Number.isNaN(a)) {
109
- a = null
111
+ ;(a as any) = null
110
112
  } else if (typeof a === 'function') {
111
- a = undefined
113
+ ;(a as any) = undefined
112
114
  } else if (a && typeof a === 'object') {
113
115
  if (a instanceof Date) {
114
- a = a.valueOf()
116
+ ;(a as any) = a.valueOf()
115
117
  } else if ('toJSON' in a) {
116
- a = a.toJSON()
118
+ a = (a as any).toJSON()
117
119
  }
118
120
  }
119
121
  if (Number.isNaN(b)) {
120
- b = null
122
+ ;(b as any) = null
121
123
  } else if (typeof b === 'function') {
122
- b = undefined
124
+ ;(b as any) = undefined
123
125
  } else if (b && typeof b === 'object') {
124
126
  if (b instanceof Date) {
125
- b = b.valueOf()
127
+ ;(b as any) = b.valueOf()
126
128
  } else if ('toJSON' in b) {
127
- b = b.toJSON()
129
+ b = (b as any).toJSON()
128
130
  }
129
131
  }
130
132
 
131
133
  if (a && b && typeof a === 'object' && typeof b === 'object') {
132
134
  if (Array.isArray(a)) {
133
135
  const length = a.length
134
- if (length !== b.length) return false
136
+ if (!Array.isArray(b) || length !== b.length) return false
135
137
  for (let i = length; i-- !== 0; ) {
136
138
  if (!_deepJsonEquals(a[i], b[i])) return false
137
139
  }
@@ -139,7 +141,7 @@ export function _deepJsonEquals(a: any, b: any): boolean {
139
141
  }
140
142
 
141
143
  for (const key of new Set([...Object.keys(a), ...Object.keys(b)])) {
142
- if (!_deepJsonEquals(a[key], b[key])) return false
144
+ if (!_deepJsonEquals(a[key as keyof T], b[key as keyof T])) return false
143
145
  }
144
146
 
145
147
  return true
@@ -157,7 +157,9 @@ export async function pRetry<T>(
157
157
  return result
158
158
  } catch (err) {
159
159
  if (logFailures) {
160
- logger.error(`${fname} attempt #${attempt} error in ${_since(started)}:`, err)
160
+ // Logger at warn (not error) level, because it's not a fatal error, but a retriable one
161
+ // Fatal one is not logged either, because it's been thrown instead
162
+ logger.warn(`${fname} attempt #${attempt} error in ${_since(started)}:`, err)
161
163
  }
162
164
 
163
165
  if (attempt >= maxAttempts || (predicate && !predicate(err as Error, attempt, maxAttempts))) {