@naturalcycles/js-lib 14.198.0 → 14.200.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.
@@ -1,4 +1,4 @@
1
- import type { FalsyValue, Mapper, Predicate, SortDirection, StringMap } from '../types';
1
+ import { AbortablePredicate, FalsyValue, Mapper, Predicate, SortDirection, StringMap } from '../types';
2
2
  /**
3
3
  * Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the
4
4
  * final chunk will be the remaining elements.
@@ -110,6 +110,10 @@ export declare function _takeWhile<T>(items: T[], predicate: Predicate<T>): T[];
110
110
  export declare function _takeRightWhile<T>(items: T[], predicate: Predicate<T>): T[];
111
111
  export declare function _dropWhile<T>(items: T[], predicate: Predicate<T>): T[];
112
112
  export declare function _dropRightWhile<T>(items: T[], predicate: Predicate<T>): T[];
113
+ /**
114
+ * Counts how many items match the predicate.
115
+ */
116
+ export declare function _count<T>(items: T[], predicate: AbortablePredicate<T>): number;
113
117
  export declare function _countBy<T>(items: T[], mapper: Mapper<T, any>): StringMap<number>;
114
118
  /**
115
119
  * @example
@@ -171,3 +175,4 @@ export declare function _maxBy<T>(array: T[], mapper: Mapper<T, number | undefin
171
175
  export declare function _minBy<T>(array: T[], mapper: Mapper<T, number | undefined>): T;
172
176
  export declare function _maxByOrUndefined<T>(array: T[], mapper: Mapper<T, number | undefined>): T | undefined;
173
177
  export declare function _minByOrUndefined<T>(array: T[], mapper: Mapper<T, number | undefined>): T | undefined;
178
+ export declare function _zip<T1, T2>(array1: T1[], array2: T2[]): [T1, T2][];
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._minByOrUndefined = exports._maxByOrUndefined = exports._minBy = exports._maxBy = exports._max = exports._maxOrUndefined = exports._min = exports._minOrUndefined = exports._lastOrUndefined = exports._last = exports._shuffle = exports._mapToObject = exports._sumBy = exports._sum = exports._difference = exports._intersection = exports._countBy = exports._dropRightWhile = exports._dropWhile = exports._takeRightWhile = exports._takeWhile = exports._findLast = exports._sortDescBy = exports._sortBy = exports._groupBy = exports._mapBy = exports._by = exports._uniqBy = exports._pushUniqBy = exports._pushUniq = exports._uniq = exports._chunk = void 0;
3
+ exports._zip = exports._minByOrUndefined = exports._maxByOrUndefined = exports._minBy = exports._maxBy = exports._max = exports._maxOrUndefined = exports._min = exports._minOrUndefined = exports._lastOrUndefined = exports._last = exports._shuffle = exports._mapToObject = exports._sumBy = exports._sum = exports._difference = exports._intersection = exports._countBy = exports._count = exports._dropRightWhile = exports._dropWhile = exports._takeRightWhile = exports._takeWhile = exports._findLast = exports._sortDescBy = exports._sortBy = exports._groupBy = exports._mapBy = exports._by = exports._uniqBy = exports._pushUniqBy = exports._pushUniq = exports._uniq = exports._chunk = void 0;
4
4
  const is_util_1 = require("../is.util");
5
+ const types_1 = require("../types");
5
6
  /**
6
7
  * Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the
7
8
  * final chunk will be the remaining elements.
@@ -200,6 +201,21 @@ function _dropRightWhile(items, predicate) {
200
201
  .reverse();
201
202
  }
202
203
  exports._dropRightWhile = _dropRightWhile;
204
+ /**
205
+ * Counts how many items match the predicate.
206
+ */
207
+ function _count(items, predicate) {
208
+ let count = 0;
209
+ for (const [i, item] of items.entries()) {
210
+ const r = predicate(item, i);
211
+ if (r === types_1.END)
212
+ break;
213
+ if (r)
214
+ count++;
215
+ }
216
+ return count;
217
+ }
218
+ exports._count = _count;
203
219
  function _countBy(items, mapper) {
204
220
  const map = {};
205
221
  items.forEach((item, index) => {
@@ -378,3 +394,12 @@ function _minByOrUndefined(array, mapper) {
378
394
  return minItem;
379
395
  }
380
396
  exports._minByOrUndefined = _minByOrUndefined;
397
+ function _zip(array1, array2) {
398
+ const len = Math.min(array1.length, array2.length);
399
+ const res = [];
400
+ for (let i = 0; i < len; i++) {
401
+ res.push([array1[i], array2[i]]);
402
+ }
403
+ return res;
404
+ }
405
+ exports._zip = _zip;
@@ -1,4 +1,5 @@
1
1
  import { _isNotNullish } from '../is.util';
2
+ import { END, } from '../types';
2
3
  /**
3
4
  * Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the
4
5
  * final chunk will be the remaining elements.
@@ -182,6 +183,20 @@ export function _dropRightWhile(items, predicate) {
182
183
  .filter((v, index) => (proceed || (proceed = !predicate(v, index))))
183
184
  .reverse();
184
185
  }
186
+ /**
187
+ * Counts how many items match the predicate.
188
+ */
189
+ export function _count(items, predicate) {
190
+ let count = 0;
191
+ for (const [i, item] of items.entries()) {
192
+ const r = predicate(item, i);
193
+ if (r === END)
194
+ break;
195
+ if (r)
196
+ count++;
197
+ }
198
+ return count;
199
+ }
185
200
  export function _countBy(items, mapper) {
186
201
  const map = {};
187
202
  items.forEach((item, index) => {
@@ -343,3 +358,11 @@ export function _minByOrUndefined(array, mapper) {
343
358
  });
344
359
  return minItem;
345
360
  }
361
+ export function _zip(array1, array2) {
362
+ const len = Math.min(array1.length, array2.length);
363
+ const res = [];
364
+ for (let i = 0; i < len; i++) {
365
+ res.push([array1[i], array2[i]]);
366
+ }
367
+ return res;
368
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.198.0",
3
+ "version": "14.200.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -1,5 +1,13 @@
1
1
  import { _isNotNullish } from '../is.util'
2
- import type { FalsyValue, Mapper, Predicate, SortDirection, StringMap } from '../types'
2
+ import {
3
+ AbortablePredicate,
4
+ END,
5
+ FalsyValue,
6
+ Mapper,
7
+ Predicate,
8
+ SortDirection,
9
+ StringMap,
10
+ } from '../types'
3
11
 
4
12
  /**
5
13
  * Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the
@@ -216,6 +224,21 @@ export function _dropRightWhile<T>(items: T[], predicate: Predicate<T>): T[] {
216
224
  .reverse()
217
225
  }
218
226
 
227
+ /**
228
+ * Counts how many items match the predicate.
229
+ */
230
+ export function _count<T>(items: T[], predicate: AbortablePredicate<T>): number {
231
+ let count = 0
232
+
233
+ for (const [i, item] of items.entries()) {
234
+ const r = predicate(item, i)
235
+ if (r === END) break
236
+ if (r) count++
237
+ }
238
+
239
+ return count
240
+ }
241
+
219
242
  export function _countBy<T>(items: T[], mapper: Mapper<T, any>): StringMap<number> {
220
243
  const map: StringMap<number> = {}
221
244
 
@@ -402,3 +425,14 @@ export function _minByOrUndefined<T>(
402
425
 
403
426
  return minItem
404
427
  }
428
+
429
+ export function _zip<T1, T2>(array1: T1[], array2: T2[]): [T1, T2][] {
430
+ const len = Math.min(array1.length, array2.length)
431
+ const res: [T1, T2][] = []
432
+
433
+ for (let i = 0; i < len; i++) {
434
+ res.push([array1[i]!, array2[i]!])
435
+ }
436
+
437
+ return res
438
+ }