@naturalcycles/js-lib 14.254.0 → 14.255.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.
@@ -161,11 +161,15 @@ export declare function _intersection<T>(a1: T[], a2: T[] | Set<T>): T[];
161
161
  */
162
162
  export declare function _intersectsWith<T>(a1: T[], a2: T[] | Set<T>): boolean;
163
163
  /**
164
+ * Returns array1 minus array2.
165
+ *
164
166
  * @example
165
167
  * _difference([2, 1], [2, 3])
166
168
  * // [1]
169
+ *
170
+ * Passing second array as Set is more performant (it'll skip turning the array into Set in-place).
167
171
  */
168
- export declare function _difference<T>(source: T[], ...diffs: T[][]): T[];
172
+ export declare function _difference<T>(a1: T[], a2: T[] | Set<T>): T[];
169
173
  /**
170
174
  * Returns the sum of items, or 0 for empty array.
171
175
  */
@@ -318,16 +318,17 @@ function _intersectsWith(a1, a2) {
318
318
  return a1.some(v => a2set.has(v));
319
319
  }
320
320
  /**
321
+ * Returns array1 minus array2.
322
+ *
321
323
  * @example
322
324
  * _difference([2, 1], [2, 3])
323
325
  * // [1]
326
+ *
327
+ * Passing second array as Set is more performant (it'll skip turning the array into Set in-place).
324
328
  */
325
- function _difference(source, ...diffs) {
326
- let a = source;
327
- for (const b of diffs) {
328
- a = a.filter(c => !b.includes(c));
329
- }
330
- return a;
329
+ function _difference(a1, a2) {
330
+ const a2set = a2 instanceof Set ? a2 : new Set(a2);
331
+ return a1.filter(v => !a2set.has(v));
331
332
  }
332
333
  /**
333
334
  * Returns the sum of items, or 0 for empty array.
@@ -29,7 +29,7 @@ function _tryCatch(fn, opt = {}) {
29
29
  }
30
30
  if (onError) {
31
31
  try {
32
- return await onError((0, index_1._anyToError)(err)); // eslint-disable-line @typescript-eslint/return-await
32
+ return await onError((0, index_1._anyToError)(err));
33
33
  }
34
34
  catch { }
35
35
  }
package/dist/types.d.ts CHANGED
@@ -1,4 +1,18 @@
1
1
  import type { Promisable } from './typeFest';
2
+ declare const __brand: unique symbol;
3
+ interface Brand<B> {
4
+ [__brand]: B;
5
+ }
6
+ /**
7
+ * Helper to create "Branded" types.
8
+ *
9
+ * Example:
10
+ * export type MyId = Branded<string, 'MyId'>
11
+ *
12
+ * MyId can be assigned to a string,
13
+ * but string cannot be assigned to MyId without casting it (`as MyId`).
14
+ */
15
+ export type Branded<T, B> = T & Brand<B>;
2
16
  /**
3
17
  * Map from String to String (or <T>).
4
18
  *
@@ -292,3 +306,4 @@ export interface CommonClient extends AsyncDisposable {
292
306
  disconnect: () => Promise<void>;
293
307
  ping: () => Promise<void>;
294
308
  }
309
+ export {};
@@ -277,16 +277,17 @@ export function _intersectsWith(a1, a2) {
277
277
  return a1.some(v => a2set.has(v));
278
278
  }
279
279
  /**
280
+ * Returns array1 minus array2.
281
+ *
280
282
  * @example
281
283
  * _difference([2, 1], [2, 3])
282
284
  * // [1]
285
+ *
286
+ * Passing second array as Set is more performant (it'll skip turning the array into Set in-place).
283
287
  */
284
- export function _difference(source, ...diffs) {
285
- let a = source;
286
- for (const b of diffs) {
287
- a = a.filter(c => !b.includes(c));
288
- }
289
- return a;
288
+ export function _difference(a1, a2) {
289
+ const a2set = a2 instanceof Set ? a2 : new Set(a2);
290
+ return a1.filter(v => !a2set.has(v));
290
291
  }
291
292
  /**
292
293
  * Returns the sum of items, or 0 for empty array.
@@ -25,7 +25,7 @@ export function _tryCatch(fn, opt = {}) {
25
25
  }
26
26
  if (onError) {
27
27
  try {
28
- return await onError(_anyToError(err)); // eslint-disable-line @typescript-eslint/return-await
28
+ return await onError(_anyToError(err));
29
29
  }
30
30
  catch { }
31
31
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.254.0",
3
+ "version": "14.255.0",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build": "dev-lib build-esm-cjs",
@@ -331,16 +331,17 @@ export function _intersectsWith<T>(a1: T[], a2: T[] | Set<T>): boolean {
331
331
  }
332
332
 
333
333
  /**
334
+ * Returns array1 minus array2.
335
+ *
334
336
  * @example
335
337
  * _difference([2, 1], [2, 3])
336
338
  * // [1]
339
+ *
340
+ * Passing second array as Set is more performant (it'll skip turning the array into Set in-place).
337
341
  */
338
- export function _difference<T>(source: T[], ...diffs: T[][]): T[] {
339
- let a = source
340
- for (const b of diffs) {
341
- a = a.filter(c => !b.includes(c))
342
- }
343
- return a
342
+ export function _difference<T>(a1: T[], a2: T[] | Set<T>): T[] {
343
+ const a2set = a2 instanceof Set ? a2 : new Set(a2)
344
+ return a1.filter(v => !a2set.has(v))
344
345
  }
345
346
 
346
347
  /**
@@ -56,7 +56,7 @@ export function _tryCatch<T extends AnyFunction>(fn: T, opt: TryCatchOptions = {
56
56
 
57
57
  if (onError) {
58
58
  try {
59
- return await onError(_anyToError(err)) // eslint-disable-line @typescript-eslint/return-await
59
+ return await onError(_anyToError(err))
60
60
  } catch {}
61
61
  }
62
62
  // returns undefined, but doesn't rethrow
package/src/types.ts CHANGED
@@ -1,5 +1,22 @@
1
1
  import type { Promisable } from './typeFest'
2
2
 
3
+ declare const __brand: unique symbol
4
+
5
+ interface Brand<B> {
6
+ [__brand]: B
7
+ }
8
+
9
+ /**
10
+ * Helper to create "Branded" types.
11
+ *
12
+ * Example:
13
+ * export type MyId = Branded<string, 'MyId'>
14
+ *
15
+ * MyId can be assigned to a string,
16
+ * but string cannot be assigned to MyId without casting it (`as MyId`).
17
+ */
18
+ export type Branded<T, B> = T & Brand<B>
19
+
3
20
  /**
4
21
  * Map from String to String (or <T>).
5
22
  *