@naturalcycles/js-lib 14.208.0 → 14.209.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])
@@ -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.209.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
  /**