@naturalcycles/js-lib 14.208.0 → 14.210.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.
@@ -116,11 +116,26 @@ export declare function _dropRightWhile<T>(items: T[], predicate: Predicate<T>):
116
116
  export declare function _count<T>(items: T[], predicate: AbortablePredicate<T>): number;
117
117
  export declare function _countBy<T>(items: T[], mapper: Mapper<T, any>): StringMap<number>;
118
118
  /**
119
+ * Returns an intersection between 2 arrays.
120
+ *
121
+ * Intersecion means an array of items that are present in both of the arrays.
122
+ *
123
+ * It's more performant to pass a Set as a second argument.
124
+ *
119
125
  * @example
120
126
  * _intersection([2, 1], [2, 3])
121
127
  * // [2]
122
128
  */
123
- export declare function _intersection<T>(...arrays: T[][]): T[];
129
+ export declare function _intersection<T>(a1: T[], a2: T[] | Set<T>): T[];
130
+ /**
131
+ * Returns true if there is at least 1 item common between 2 arrays.
132
+ * Otherwise returns false.
133
+ *
134
+ * It's more performant to use that versus `_intersection(a1, a2).length > 0`.
135
+ *
136
+ * Passing second array as Set is more performant (it'll skip turning the array into Set in-place).
137
+ */
138
+ export declare function _intersectsWith<T>(a1: T[], a2: T[] | Set<T>): boolean;
124
139
  /**
125
140
  * @example
126
141
  * _difference([2, 1], [2, 3])
@@ -171,8 +186,8 @@ export declare function _maxOrUndefined<T>(array: T[]): NonNullable<T> | undefin
171
186
  * Filters out nullish values (undefined and null).
172
187
  */
173
188
  export declare function _max<T>(array: T[]): NonNullable<T>;
174
- export declare function _maxBy<T>(array: T[], mapper: Mapper<T, number | undefined>): T;
175
- export declare function _minBy<T>(array: T[], mapper: Mapper<T, number | undefined>): T;
176
- export declare function _maxByOrUndefined<T>(array: T[], mapper: Mapper<T, number | undefined>): T | undefined;
177
- export declare function _minByOrUndefined<T>(array: T[], mapper: Mapper<T, number | undefined>): T | undefined;
189
+ export declare function _maxBy<T>(array: T[], mapper: Mapper<T, number | string | undefined>): T;
190
+ export declare function _minBy<T>(array: T[], mapper: Mapper<T, number | string | undefined>): T;
191
+ export declare function _maxByOrUndefined<T>(array: T[], mapper: Mapper<T, number | string | undefined>): T | undefined;
192
+ export declare function _minByOrUndefined<T>(array: T[], mapper: Mapper<T, number | string | undefined>): T | undefined;
178
193
  export declare function _zip<T1, T2>(array1: T1[], array2: T2[]): [T1, T2][];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
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;
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._intersectsWith = 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
5
  const types_1 = require("../types");
6
6
  /**
@@ -227,16 +227,34 @@ function _countBy(items, mapper) {
227
227
  exports._countBy = _countBy;
228
228
  // investigate: _groupBy
229
229
  /**
230
+ * Returns an intersection between 2 arrays.
231
+ *
232
+ * Intersecion means an array of items that are present in both of the arrays.
233
+ *
234
+ * It's more performant to pass a Set as a second argument.
235
+ *
230
236
  * @example
231
237
  * _intersection([2, 1], [2, 3])
232
238
  * // [2]
233
239
  */
234
- function _intersection(...arrays) {
235
- if (arrays.length === 0)
236
- return []; // edge case
237
- return arrays.reduce((a, b) => a.filter(v => b.includes(v)));
240
+ function _intersection(a1, a2) {
241
+ const a2set = a2 instanceof Set ? a2 : new Set(a2);
242
+ return a1.filter(v => a2set.has(v));
238
243
  }
239
244
  exports._intersection = _intersection;
245
+ /**
246
+ * Returns true if there is at least 1 item common between 2 arrays.
247
+ * Otherwise returns false.
248
+ *
249
+ * It's more performant to use that versus `_intersection(a1, a2).length > 0`.
250
+ *
251
+ * Passing second array as Set is more performant (it'll skip turning the array into Set in-place).
252
+ */
253
+ function _intersectsWith(a1, a2) {
254
+ const a2set = a2 instanceof Set ? a2 : new Set(a2);
255
+ return a1.some(v => a2set.has(v));
256
+ }
257
+ exports._intersectsWith = _intersectsWith;
240
258
  /**
241
259
  * @example
242
260
  * _difference([2, 1], [2, 3])
@@ -207,14 +207,31 @@ export function _countBy(items, mapper) {
207
207
  }
208
208
  // investigate: _groupBy
209
209
  /**
210
+ * Returns an intersection between 2 arrays.
211
+ *
212
+ * Intersecion means an array of items that are present in both of the arrays.
213
+ *
214
+ * It's more performant to pass a Set as a second argument.
215
+ *
210
216
  * @example
211
217
  * _intersection([2, 1], [2, 3])
212
218
  * // [2]
213
219
  */
214
- export function _intersection(...arrays) {
215
- if (arrays.length === 0)
216
- return []; // edge case
217
- return arrays.reduce((a, b) => a.filter(v => b.includes(v)));
220
+ export function _intersection(a1, a2) {
221
+ const a2set = a2 instanceof Set ? a2 : new Set(a2);
222
+ return a1.filter(v => a2set.has(v));
223
+ }
224
+ /**
225
+ * Returns true if there is at least 1 item common between 2 arrays.
226
+ * Otherwise returns false.
227
+ *
228
+ * It's more performant to use that versus `_intersection(a1, a2).length > 0`.
229
+ *
230
+ * Passing second array as Set is more performant (it'll skip turning the array into Set in-place).
231
+ */
232
+ export function _intersectsWith(a1, a2) {
233
+ const a2set = a2 instanceof Set ? a2 : new Set(a2);
234
+ return a1.some(v => a2set.has(v));
218
235
  }
219
236
  /**
220
237
  * @example
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.208.0",
3
+ "version": "14.210.0",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -253,13 +253,32 @@ export function _countBy<T>(items: T[], mapper: Mapper<T, any>): StringMap<numbe
253
253
  // investigate: _groupBy
254
254
 
255
255
  /**
256
+ * Returns an intersection between 2 arrays.
257
+ *
258
+ * Intersecion means an array of items that are present in both of the arrays.
259
+ *
260
+ * It's more performant to pass a Set as a second argument.
261
+ *
256
262
  * @example
257
263
  * _intersection([2, 1], [2, 3])
258
264
  * // [2]
259
265
  */
260
- export function _intersection<T>(...arrays: T[][]): T[] {
261
- if (arrays.length === 0) return [] // edge case
262
- return arrays.reduce((a, b) => a.filter(v => b.includes(v)))
266
+ export function _intersection<T>(a1: T[], a2: T[] | Set<T>): T[] {
267
+ const a2set = a2 instanceof Set ? a2 : new Set(a2)
268
+ return a1.filter(v => a2set.has(v))
269
+ }
270
+
271
+ /**
272
+ * Returns true if there is at least 1 item common between 2 arrays.
273
+ * Otherwise returns false.
274
+ *
275
+ * It's more performant to use that versus `_intersection(a1, a2).length > 0`.
276
+ *
277
+ * Passing second array as Set is more performant (it'll skip turning the array into Set in-place).
278
+ */
279
+ export function _intersectsWith<T>(a1: T[], a2: T[] | Set<T>): boolean {
280
+ const a2set = a2 instanceof Set ? a2 : new Set(a2)
281
+ return a1.some(v => a2set.has(v))
263
282
  }
264
283
 
265
284
  /**
@@ -376,13 +395,13 @@ export function _max<T>(array: T[]): NonNullable<T> {
376
395
  return a.reduce((max, item) => (max >= item ? max : item))
377
396
  }
378
397
 
379
- export function _maxBy<T>(array: T[], mapper: Mapper<T, number | undefined>): T {
398
+ export function _maxBy<T>(array: T[], mapper: Mapper<T, number | string | undefined>): T {
380
399
  const max = _maxByOrUndefined(array, mapper)
381
400
  if (max === undefined) throw new Error(`_maxBy returned undefined`)
382
401
  return max
383
402
  }
384
403
 
385
- export function _minBy<T>(array: T[], mapper: Mapper<T, number | undefined>): T {
404
+ export function _minBy<T>(array: T[], mapper: Mapper<T, number | string | undefined>): T {
386
405
  const min = _minByOrUndefined(array, mapper)
387
406
  if (min === undefined) throw new Error(`_minBy returned undefined`)
388
407
  return min
@@ -392,11 +411,11 @@ export function _minBy<T>(array: T[], mapper: Mapper<T, number | undefined>): T
392
411
 
393
412
  export function _maxByOrUndefined<T>(
394
413
  array: T[],
395
- mapper: Mapper<T, number | undefined>,
414
+ mapper: Mapper<T, number | string | undefined>,
396
415
  ): T | undefined {
397
416
  if (!array.length) return
398
417
  let maxItem: T | undefined
399
- let max: number | undefined
418
+ let max: number | string | undefined
400
419
  array.forEach((item, i) => {
401
420
  const v = mapper(item, i)
402
421
  if (v !== undefined && (max === undefined || v > max)) {
@@ -410,11 +429,11 @@ export function _maxByOrUndefined<T>(
410
429
 
411
430
  export function _minByOrUndefined<T>(
412
431
  array: T[],
413
- mapper: Mapper<T, number | undefined>,
432
+ mapper: Mapper<T, number | string | undefined>,
414
433
  ): T | undefined {
415
434
  if (!array.length) return
416
435
  let minItem: T | undefined
417
- let min: number | undefined
436
+ let min: number | string | undefined
418
437
  array.forEach((item, i) => {
419
438
  const v = mapper(item, i)
420
439
  if (v !== undefined && (min === undefined || v < min)) {