@naturalcycles/js-lib 14.162.0 → 14.163.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.
@@ -16,6 +16,25 @@ export declare function _chunk<T>(array: readonly T[], size?: number): T[][];
16
16
  * Removes duplicates from given array.
17
17
  */
18
18
  export declare function _uniq<T>(a: readonly T[]): T[];
19
+ /**
20
+ * Pushes an item to an array if it's not already there.
21
+ * Mutates the array (same as normal `push`) and also returns it for chaining convenience.
22
+ *
23
+ * _pushUniq([1, 2, 3], 2) // => [1, 2, 3]
24
+ *
25
+ * Shortcut for:
26
+ * if (!a.includes(item)) a.push(item)
27
+ * // or
28
+ * a = [...new Set(a).add(item)]
29
+ * // or
30
+ * a = _uniq([...a, item])
31
+ */
32
+ export declare function _pushUniq<T>(a: T[], ...items: T[]): T[];
33
+ /**
34
+ * Like _pushUniq but uses a mapper to determine uniqueness (like _uniqBy).
35
+ * Mutates the array (same as normal `push`).
36
+ */
37
+ export declare function _pushUniqBy<T>(a: T[], mapper: Mapper<T, any>, ...items: T[]): T[];
19
38
  /**
20
39
  * This method is like `_.uniq` except that it accepts `iteratee` which is
21
40
  * invoked for each element in `array` to generate the criterion by which
@@ -1,6 +1,6 @@
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._sortBy = exports._groupBy = exports._by = exports._uniqBy = exports._uniq = exports._chunk = void 0;
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._sortBy = exports._groupBy = 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
  /**
6
6
  * Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the
@@ -27,6 +27,43 @@ function _uniq(a) {
27
27
  return [...new Set(a)];
28
28
  }
29
29
  exports._uniq = _uniq;
30
+ /**
31
+ * Pushes an item to an array if it's not already there.
32
+ * Mutates the array (same as normal `push`) and also returns it for chaining convenience.
33
+ *
34
+ * _pushUniq([1, 2, 3], 2) // => [1, 2, 3]
35
+ *
36
+ * Shortcut for:
37
+ * if (!a.includes(item)) a.push(item)
38
+ * // or
39
+ * a = [...new Set(a).add(item)]
40
+ * // or
41
+ * a = _uniq([...a, item])
42
+ */
43
+ function _pushUniq(a, ...items) {
44
+ items.forEach(item => {
45
+ if (!a.includes(item))
46
+ a.push(item);
47
+ });
48
+ return a;
49
+ }
50
+ exports._pushUniq = _pushUniq;
51
+ /**
52
+ * Like _pushUniq but uses a mapper to determine uniqueness (like _uniqBy).
53
+ * Mutates the array (same as normal `push`).
54
+ */
55
+ function _pushUniqBy(a, mapper, ...items) {
56
+ const mappedSet = new Set(a.map((item, i) => mapper(item, i)));
57
+ items.forEach((item, i) => {
58
+ const mapped = mapper(item, i);
59
+ if (!mappedSet.has(mapped)) {
60
+ a.push(item);
61
+ mappedSet.add(mapped);
62
+ }
63
+ });
64
+ return a;
65
+ }
66
+ exports._pushUniqBy = _pushUniqBy;
30
67
  /**
31
68
  * This method is like `_.uniq` except that it accepts `iteratee` which is
32
69
  * invoked for each element in `array` to generate the criterion by which
@@ -44,12 +44,16 @@ export interface ErrorData {
44
44
  * Can be used by error-reporting tools (e.g Sentry).
45
45
  * If fingerprint is defined - it'll be used INSTEAD of default fingerprint of a tool.
46
46
  * Can be used to force-group errors that are NOT needed to be split by endpoint or calling function.
47
+ *
48
+ * Sentry takes string[], but for convenience we allow to pass a singe string.
47
49
  */
48
- fingerprint?: string[];
50
+ fingerprint?: string | string[];
49
51
  /**
50
52
  * If set to true - it'll use error.message as fingerprint,
51
53
  * so, all errors with the same message will be grouped together, even if they occurred in different places.
52
54
  * Defaults to false.
55
+ *
56
+ * @experimental
53
57
  */
54
58
  fingerprintByMessage?: boolean;
55
59
  /**
@@ -22,6 +22,41 @@ export function _chunk(array, size = 1) {
22
22
  export function _uniq(a) {
23
23
  return [...new Set(a)];
24
24
  }
25
+ /**
26
+ * Pushes an item to an array if it's not already there.
27
+ * Mutates the array (same as normal `push`) and also returns it for chaining convenience.
28
+ *
29
+ * _pushUniq([1, 2, 3], 2) // => [1, 2, 3]
30
+ *
31
+ * Shortcut for:
32
+ * if (!a.includes(item)) a.push(item)
33
+ * // or
34
+ * a = [...new Set(a).add(item)]
35
+ * // or
36
+ * a = _uniq([...a, item])
37
+ */
38
+ export function _pushUniq(a, ...items) {
39
+ items.forEach(item => {
40
+ if (!a.includes(item))
41
+ a.push(item);
42
+ });
43
+ return a;
44
+ }
45
+ /**
46
+ * Like _pushUniq but uses a mapper to determine uniqueness (like _uniqBy).
47
+ * Mutates the array (same as normal `push`).
48
+ */
49
+ export function _pushUniqBy(a, mapper, ...items) {
50
+ const mappedSet = new Set(a.map((item, i) => mapper(item, i)));
51
+ items.forEach((item, i) => {
52
+ const mapped = mapper(item, i);
53
+ if (!mappedSet.has(mapped)) {
54
+ a.push(item);
55
+ mappedSet.add(mapped);
56
+ }
57
+ });
58
+ return a;
59
+ }
25
60
  /**
26
61
  * This method is like `_.uniq` except that it accepts `iteratee` which is
27
62
  * invoked for each element in `array` to generate the criterion by which
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.162.0",
3
+ "version": "14.163.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -26,6 +26,42 @@ export function _uniq<T>(a: readonly T[]): T[] {
26
26
  return [...new Set(a)]
27
27
  }
28
28
 
29
+ /**
30
+ * Pushes an item to an array if it's not already there.
31
+ * Mutates the array (same as normal `push`) and also returns it for chaining convenience.
32
+ *
33
+ * _pushUniq([1, 2, 3], 2) // => [1, 2, 3]
34
+ *
35
+ * Shortcut for:
36
+ * if (!a.includes(item)) a.push(item)
37
+ * // or
38
+ * a = [...new Set(a).add(item)]
39
+ * // or
40
+ * a = _uniq([...a, item])
41
+ */
42
+ export function _pushUniq<T>(a: T[], ...items: T[]): T[] {
43
+ items.forEach(item => {
44
+ if (!a.includes(item)) a.push(item)
45
+ })
46
+ return a
47
+ }
48
+
49
+ /**
50
+ * Like _pushUniq but uses a mapper to determine uniqueness (like _uniqBy).
51
+ * Mutates the array (same as normal `push`).
52
+ */
53
+ export function _pushUniqBy<T>(a: T[], mapper: Mapper<T, any>, ...items: T[]): T[] {
54
+ const mappedSet = new Set(a.map((item, i) => mapper(item, i)))
55
+ items.forEach((item, i) => {
56
+ const mapped = mapper(item, i)
57
+ if (!mappedSet.has(mapped)) {
58
+ a.push(item)
59
+ mappedSet.add(mapped)
60
+ }
61
+ })
62
+ return a
63
+ }
64
+
29
65
  /**
30
66
  * This method is like `_.uniq` except that it accepts `iteratee` which is
31
67
  * invoked for each element in `array` to generate the criterion by which
@@ -54,13 +54,17 @@ export interface ErrorData {
54
54
  * Can be used by error-reporting tools (e.g Sentry).
55
55
  * If fingerprint is defined - it'll be used INSTEAD of default fingerprint of a tool.
56
56
  * Can be used to force-group errors that are NOT needed to be split by endpoint or calling function.
57
+ *
58
+ * Sentry takes string[], but for convenience we allow to pass a singe string.
57
59
  */
58
- fingerprint?: string[]
60
+ fingerprint?: string | string[]
59
61
 
60
62
  /**
61
63
  * If set to true - it'll use error.message as fingerprint,
62
64
  * so, all errors with the same message will be grouped together, even if they occurred in different places.
63
65
  * Defaults to false.
66
+ *
67
+ * @experimental
64
68
  */
65
69
  fingerprintByMessage?: boolean
66
70