@naturalcycles/js-lib 14.217.0 → 14.218.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.
@@ -115,8 +115,13 @@ export declare function _dropWhile<T>(items: T[], predicate: Predicate<T>): T[];
115
115
  export declare function _dropRightWhile<T>(items: T[], predicate: Predicate<T>): T[];
116
116
  /**
117
117
  * Counts how many items match the predicate.
118
+ *
119
+ * `limit` allows to exit early when limit count is reached, skipping further iterations (perf optimization).
120
+ */
121
+ export declare function _count<T>(items: T[], predicate: AbortablePredicate<T>, limit?: number): number;
122
+ /**
123
+ * `limit` allows to exit early when limit count is reached, skipping further iterations (perf optimization).
118
124
  */
119
- export declare function _count<T>(items: T[], predicate: AbortablePredicate<T>): number;
120
125
  export declare function _countBy<T>(items: T[], mapper: Mapper<T, any>): StringMap<number>;
121
126
  /**
122
127
  * Returns an intersection between 2 arrays.
@@ -206,19 +206,29 @@ function _dropRightWhile(items, predicate) {
206
206
  exports._dropRightWhile = _dropRightWhile;
207
207
  /**
208
208
  * Counts how many items match the predicate.
209
+ *
210
+ * `limit` allows to exit early when limit count is reached, skipping further iterations (perf optimization).
209
211
  */
210
- function _count(items, predicate) {
212
+ function _count(items, predicate, limit) {
213
+ if (limit === 0)
214
+ return 0;
211
215
  let count = 0;
212
216
  for (const [i, item] of items.entries()) {
213
217
  const r = predicate(item, i);
214
218
  if (r === types_1.END)
215
219
  break;
216
- if (r)
220
+ if (r) {
217
221
  count++;
222
+ if (limit && count >= limit)
223
+ break;
224
+ }
218
225
  }
219
226
  return count;
220
227
  }
221
228
  exports._count = _count;
229
+ /**
230
+ * `limit` allows to exit early when limit count is reached, skipping further iterations (perf optimization).
231
+ */
222
232
  function _countBy(items, mapper) {
223
233
  const map = {};
224
234
  items.forEach((item, i) => {
@@ -2,5 +2,8 @@
2
2
  * Modified version of: https://github.com/sindresorhus/leven/
3
3
  *
4
4
  * Returns a Levenshtein distance between first and second word.
5
+ *
6
+ * `limit` optional parameter can be used to limit the distance calculation
7
+ * and skip unnecessary iterations when limit is reached.
5
8
  */
6
- export declare function _leven(first: string, second: string): number;
9
+ export declare function _leven(first: string, second: string, limit?: number): number;
@@ -8,9 +8,12 @@ const characterCodeCache = [];
8
8
  * Modified version of: https://github.com/sindresorhus/leven/
9
9
  *
10
10
  * Returns a Levenshtein distance between first and second word.
11
+ *
12
+ * `limit` optional parameter can be used to limit the distance calculation
13
+ * and skip unnecessary iterations when limit is reached.
11
14
  */
12
- function _leven(first, second) {
13
- if (first === second) {
15
+ function _leven(first, second, limit) {
16
+ if (first === second || limit === 0) {
14
17
  return 0;
15
18
  }
16
19
  const swap = first;
@@ -40,6 +43,8 @@ function _leven(first, second) {
40
43
  firstLength -= start;
41
44
  secondLength -= start;
42
45
  if (firstLength === 0) {
46
+ if (limit && secondLength >= limit)
47
+ return limit;
43
48
  return secondLength;
44
49
  }
45
50
  let bCharacterCode;
@@ -56,6 +61,8 @@ function _leven(first, second) {
56
61
  bCharacterCode = second.charCodeAt(start + index2);
57
62
  temporary = index2++;
58
63
  result = index2;
64
+ if (limit && result >= limit)
65
+ return limit; // exit early on limit
59
66
  for (index = 0; index < firstLength; index++) {
60
67
  temporary2 = bCharacterCode === characterCodeCache[index] ? temporary : temporary + 1;
61
68
  temporary = array[index];
@@ -188,18 +188,28 @@ export function _dropRightWhile(items, predicate) {
188
188
  }
189
189
  /**
190
190
  * Counts how many items match the predicate.
191
+ *
192
+ * `limit` allows to exit early when limit count is reached, skipping further iterations (perf optimization).
191
193
  */
192
- export function _count(items, predicate) {
194
+ export function _count(items, predicate, limit) {
195
+ if (limit === 0)
196
+ return 0;
193
197
  let count = 0;
194
198
  for (const [i, item] of items.entries()) {
195
199
  const r = predicate(item, i);
196
200
  if (r === END)
197
201
  break;
198
- if (r)
202
+ if (r) {
199
203
  count++;
204
+ if (limit && count >= limit)
205
+ break;
206
+ }
200
207
  }
201
208
  return count;
202
209
  }
210
+ /**
211
+ * `limit` allows to exit early when limit count is reached, skipping further iterations (perf optimization).
212
+ */
203
213
  export function _countBy(items, mapper) {
204
214
  const map = {};
205
215
  items.forEach((item, i) => {
@@ -5,9 +5,12 @@ const characterCodeCache = [];
5
5
  * Modified version of: https://github.com/sindresorhus/leven/
6
6
  *
7
7
  * Returns a Levenshtein distance between first and second word.
8
+ *
9
+ * `limit` optional parameter can be used to limit the distance calculation
10
+ * and skip unnecessary iterations when limit is reached.
8
11
  */
9
- export function _leven(first, second) {
10
- if (first === second) {
12
+ export function _leven(first, second, limit) {
13
+ if (first === second || limit === 0) {
11
14
  return 0;
12
15
  }
13
16
  const swap = first;
@@ -37,6 +40,8 @@ export function _leven(first, second) {
37
40
  firstLength -= start;
38
41
  secondLength -= start;
39
42
  if (firstLength === 0) {
43
+ if (limit && secondLength >= limit)
44
+ return limit;
40
45
  return secondLength;
41
46
  }
42
47
  let bCharacterCode;
@@ -53,6 +58,8 @@ export function _leven(first, second) {
53
58
  bCharacterCode = second.charCodeAt(start + index2);
54
59
  temporary = index2++;
55
60
  result = index2;
61
+ if (limit && result >= limit)
62
+ return limit; // exit early on limit
56
63
  for (index = 0; index < firstLength; index++) {
57
64
  temporary2 = bCharacterCode === characterCodeCache[index] ? temporary : temporary + 1;
58
65
  temporary = array[index];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.217.0",
3
+ "version": "14.218.0",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -229,19 +229,28 @@ export function _dropRightWhile<T>(items: T[], predicate: Predicate<T>): T[] {
229
229
 
230
230
  /**
231
231
  * Counts how many items match the predicate.
232
+ *
233
+ * `limit` allows to exit early when limit count is reached, skipping further iterations (perf optimization).
232
234
  */
233
- export function _count<T>(items: T[], predicate: AbortablePredicate<T>): number {
235
+ export function _count<T>(items: T[], predicate: AbortablePredicate<T>, limit?: number): number {
236
+ if (limit === 0) return 0
234
237
  let count = 0
235
238
 
236
239
  for (const [i, item] of items.entries()) {
237
240
  const r = predicate(item, i)
238
241
  if (r === END) break
239
- if (r) count++
242
+ if (r) {
243
+ count++
244
+ if (limit && count >= limit) break
245
+ }
240
246
  }
241
247
 
242
248
  return count
243
249
  }
244
250
 
251
+ /**
252
+ * `limit` allows to exit early when limit count is reached, skipping further iterations (perf optimization).
253
+ */
245
254
  export function _countBy<T>(items: T[], mapper: Mapper<T, any>): StringMap<number> {
246
255
  const map: StringMap<number> = {}
247
256
 
@@ -7,9 +7,12 @@ const characterCodeCache: number[] = []
7
7
  * Modified version of: https://github.com/sindresorhus/leven/
8
8
  *
9
9
  * Returns a Levenshtein distance between first and second word.
10
+ *
11
+ * `limit` optional parameter can be used to limit the distance calculation
12
+ * and skip unnecessary iterations when limit is reached.
10
13
  */
11
- export function _leven(first: string, second: string): number {
12
- if (first === second) {
14
+ export function _leven(first: string, second: string, limit?: number): number {
15
+ if (first === second || limit === 0) {
13
16
  return 0
14
17
  }
15
18
 
@@ -47,6 +50,7 @@ export function _leven(first: string, second: string): number {
47
50
  secondLength -= start
48
51
 
49
52
  if (firstLength === 0) {
53
+ if (limit && secondLength >= limit) return limit
50
54
  return secondLength
51
55
  }
52
56
 
@@ -66,6 +70,7 @@ export function _leven(first: string, second: string): number {
66
70
  bCharacterCode = second.charCodeAt(start + index2)
67
71
  temporary = index2++
68
72
  result = index2
73
+ if (limit && result >= limit) return limit // exit early on limit
69
74
 
70
75
  for (index = 0; index < firstLength; index++) {
71
76
  temporary2 = bCharacterCode === characterCodeCache[index] ? temporary : temporary + 1