@naturalcycles/js-lib 14.194.0 → 14.196.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.
@@ -74,6 +74,10 @@ export declare function _uniqBy<T>(arr: readonly T[], mapper: Mapper<T, any>): T
74
74
  * Returning `undefined` from the Mapper will EXCLUDE the item.
75
75
  */
76
76
  export declare function _by<T>(items: readonly T[], mapper: Mapper<T, any>): StringMap<T>;
77
+ /**
78
+ * Map an array of items by a key, that is calculated by a Mapper.
79
+ */
80
+ export declare function _mapBy<ITEM, KEY>(items: readonly ITEM[], mapper: Mapper<ITEM, KEY>): Map<KEY, ITEM>;
77
81
  /**
78
82
  * const a = [1, 2, 3, 4, 5]
79
83
  *
@@ -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._sortDescBy = exports._sortBy = exports._groupBy = exports._by = exports._uniqBy = exports._pushUniqBy = exports._pushUniq = 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._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
  /**
6
6
  * Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the
@@ -115,14 +115,16 @@ exports._uniqBy = _uniqBy;
115
115
  * Returning `undefined` from the Mapper will EXCLUDE the item.
116
116
  */
117
117
  function _by(items, mapper) {
118
- return items.reduce((map, item, index) => {
119
- const res = mapper(item, index);
120
- if (res !== undefined)
121
- map[res] = item;
122
- return map;
123
- }, {});
118
+ return Object.fromEntries(items.map((item, i) => [mapper(item, i), item]).filter(([k]) => k !== undefined));
124
119
  }
125
120
  exports._by = _by;
121
+ /**
122
+ * Map an array of items by a key, that is calculated by a Mapper.
123
+ */
124
+ function _mapBy(items, mapper) {
125
+ return new Map(items.map((item, i) => [mapper(item, i), item]).filter(([k]) => k !== undefined));
126
+ }
127
+ exports._mapBy = _mapBy;
126
128
  /**
127
129
  * const a = [1, 2, 3, 4, 5]
128
130
  *
@@ -562,7 +562,7 @@ class Fetcher {
562
562
  },
563
563
  init: (0, object_util_1._merge)({
564
564
  ...this.cfg.init,
565
- headers: { ...this.cfg.init.headers },
565
+ headers: { ...this.cfg.init.headers }, // this avoids mutation
566
566
  method: opt.method || this.cfg.init.method,
567
567
  credentials: opt.credentials || this.cfg.init.credentials,
568
568
  redirect: opt.redirect || this.cfg.init.redirect || 'follow',
@@ -1,4 +1,4 @@
1
- import { AbortableMapper, AbortablePredicate, END, Predicate } from '../types';
1
+ import { AbortableMapper, AbortablePredicate, END } from '../types';
2
2
  /**
3
3
  * Iterable2 is a wrapper around Iterable that implements "Iterator Helpers proposal":
4
4
  * https://github.com/tc39/proposal-iterator-helpers
@@ -15,7 +15,9 @@ export declare class Iterable2<T> implements Iterable<T> {
15
15
  [Symbol.iterator](): Iterator<T>;
16
16
  toArray(): T[];
17
17
  forEach(cb: (v: T, i: number) => any | typeof END): void;
18
- find(cb: Predicate<T>): T | undefined;
18
+ some(cb: AbortablePredicate<T>): boolean;
19
+ every(cb: AbortablePredicate<T>): boolean;
20
+ find(cb: AbortablePredicate<T>): T | undefined;
19
21
  filter(cb: AbortablePredicate<T>): Iterable2<T>;
20
22
  map<OUT>(mapper: AbortableMapper<T, OUT>): Iterable2<OUT>;
21
23
  }
@@ -33,10 +33,26 @@ class Iterable2 {
33
33
  return;
34
34
  }
35
35
  }
36
+ some(cb) {
37
+ // eslint-disable-next-line unicorn/prefer-array-some
38
+ return !!this.find(cb);
39
+ }
40
+ every(cb) {
41
+ let i = 0;
42
+ for (const v of this.it) {
43
+ const r = cb(v, i++);
44
+ if (r === types_1.END || !r)
45
+ return false;
46
+ }
47
+ return true;
48
+ }
36
49
  find(cb) {
37
50
  let i = 0;
38
51
  for (const v of this.it) {
39
- if (cb(v, i++))
52
+ const r = cb(v, i++);
53
+ if (r === types_1.END)
54
+ return;
55
+ if (r)
40
56
  return v;
41
57
  }
42
58
  }
@@ -43,7 +43,7 @@ function commonLoggerMinLevel(logger, minLevel, mutate = false) {
43
43
  return exports.commonLoggerNoop;
44
44
  }
45
45
  return {
46
- log: index_1._noop,
46
+ log: index_1._noop, // otherwise it is "log everything" logger (same logger as input)
47
47
  warn: level <= exports.commonLogLevelNumber['warn'] ? logger.warn.bind(logger) : index_1._noop,
48
48
  error: logger.error.bind(logger), // otherwise it's "log nothing" logger (same as noopLogger)
49
49
  };
@@ -107,12 +107,13 @@ export function _uniqBy(arr, mapper) {
107
107
  * Returning `undefined` from the Mapper will EXCLUDE the item.
108
108
  */
109
109
  export function _by(items, mapper) {
110
- return items.reduce((map, item, index) => {
111
- const res = mapper(item, index);
112
- if (res !== undefined)
113
- map[res] = item;
114
- return map;
115
- }, {});
110
+ return Object.fromEntries(items.map((item, i) => [mapper(item, i), item]).filter(([k]) => k !== undefined));
111
+ }
112
+ /**
113
+ * Map an array of items by a key, that is calculated by a Mapper.
114
+ */
115
+ export function _mapBy(items, mapper) {
116
+ return new Map(items.map((item, i) => [mapper(item, i), item]).filter(([k]) => k !== undefined));
116
117
  }
117
118
  /**
118
119
  * const a = [1, 2, 3, 4, 5]
@@ -30,10 +30,26 @@ export class Iterable2 {
30
30
  return;
31
31
  }
32
32
  }
33
+ some(cb) {
34
+ // eslint-disable-next-line unicorn/prefer-array-some
35
+ return !!this.find(cb);
36
+ }
37
+ every(cb) {
38
+ let i = 0;
39
+ for (const v of this.it) {
40
+ const r = cb(v, i++);
41
+ if (r === END || !r)
42
+ return false;
43
+ }
44
+ return true;
45
+ }
33
46
  find(cb) {
34
47
  let i = 0;
35
48
  for (const v of this.it) {
36
- if (cb(v, i++))
49
+ const r = cb(v, i++);
50
+ if (r === END)
51
+ return;
52
+ if (r)
37
53
  return v;
38
54
  }
39
55
  }
@@ -40,7 +40,7 @@ export function commonLoggerMinLevel(logger, minLevel, mutate = false) {
40
40
  return commonLoggerNoop;
41
41
  }
42
42
  return {
43
- log: _noop,
43
+ log: _noop, // otherwise it is "log everything" logger (same logger as input)
44
44
  warn: level <= commonLogLevelNumber['warn'] ? logger.warn.bind(logger) : _noop,
45
45
  error: logger.error.bind(logger), // otherwise it's "log nothing" logger (same as noopLogger)
46
46
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.194.0",
3
+ "version": "14.196.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -114,11 +114,24 @@ export function _uniqBy<T>(arr: readonly T[], mapper: Mapper<T, any>): T[] {
114
114
  * Returning `undefined` from the Mapper will EXCLUDE the item.
115
115
  */
116
116
  export function _by<T>(items: readonly T[], mapper: Mapper<T, any>): StringMap<T> {
117
- return items.reduce((map, item, index) => {
118
- const res = mapper(item, index)
119
- if (res !== undefined) map[res] = item
120
- return map
121
- }, {} as StringMap<T>)
117
+ return Object.fromEntries(
118
+ items.map((item, i) => [mapper(item, i), item]).filter(([k]) => k !== undefined) as [any, T][],
119
+ )
120
+ }
121
+
122
+ /**
123
+ * Map an array of items by a key, that is calculated by a Mapper.
124
+ */
125
+ export function _mapBy<ITEM, KEY>(
126
+ items: readonly ITEM[],
127
+ mapper: Mapper<ITEM, KEY>,
128
+ ): Map<KEY, ITEM> {
129
+ return new Map(
130
+ items.map((item, i) => [mapper(item, i), item]).filter(([k]) => k !== undefined) as [
131
+ KEY,
132
+ ITEM,
133
+ ][],
134
+ )
122
135
  }
123
136
 
124
137
  /**
@@ -1,4 +1,4 @@
1
- import { AbortableMapper, AbortablePredicate, END, Predicate, SKIP } from '../types'
1
+ import { AbortableMapper, AbortablePredicate, END, SKIP } from '../types'
2
2
 
3
3
  /**
4
4
  * Iterable2 is a wrapper around Iterable that implements "Iterator Helpers proposal":
@@ -34,10 +34,26 @@ export class Iterable2<T> implements Iterable<T> {
34
34
  }
35
35
  }
36
36
 
37
- find(cb: Predicate<T>): T | undefined {
37
+ some(cb: AbortablePredicate<T>): boolean {
38
+ // eslint-disable-next-line unicorn/prefer-array-some
39
+ return !!this.find(cb)
40
+ }
41
+
42
+ every(cb: AbortablePredicate<T>): boolean {
43
+ let i = 0
44
+ for (const v of this.it) {
45
+ const r = cb(v, i++)
46
+ if (r === END || !r) return false
47
+ }
48
+ return true
49
+ }
50
+
51
+ find(cb: AbortablePredicate<T>): T | undefined {
38
52
  let i = 0
39
53
  for (const v of this.it) {
40
- if (cb(v, i++)) return v
54
+ const r = cb(v, i++)
55
+ if (r === END) return
56
+ if (r) return v
41
57
  }
42
58
  }
43
59
 
@@ -77,8 +77,8 @@ export function _leven(first: string, second: string): number {
77
77
  ? result + 1
78
78
  : temporary2
79
79
  : temporary2 > temporary
80
- ? temporary + 1
81
- : temporary2
80
+ ? temporary + 1
81
+ : temporary2
82
82
  }
83
83
  }
84
84
 
package/src/typeFest.ts CHANGED
@@ -30,8 +30,8 @@ export type IsEqual<T, U> = (<G>() => G extends T ? 1 : 2) extends <G>() => G ex
30
30
  type Filter<KeyType, ExcludeType> = IsEqual<KeyType, ExcludeType> extends true
31
31
  ? never
32
32
  : KeyType extends ExcludeType
33
- ? never
34
- : KeyType
33
+ ? never
34
+ : KeyType
35
35
 
36
36
  /**
37
37
  Create a type from an object type without certain keys.
@@ -94,12 +94,12 @@ export type Except<ObjectType, KeysType extends keyof ObjectType> = {
94
94
  export type ReadonlyDeep<T> = T extends Primitive | ((...args: any[]) => unknown)
95
95
  ? T
96
96
  : T extends ReadonlyMap<infer KeyType, infer ValueType>
97
- ? ReadonlyMapDeep<KeyType, ValueType>
98
- : T extends ReadonlySet<infer ItemType>
99
- ? ReadonlySetDeep<ItemType>
100
- : T extends object
101
- ? ReadonlyObjectDeep<T>
102
- : unknown
97
+ ? ReadonlyMapDeep<KeyType, ValueType>
98
+ : T extends ReadonlySet<infer ItemType>
99
+ ? ReadonlySetDeep<ItemType>
100
+ : T extends object
101
+ ? ReadonlyObjectDeep<T>
102
+ : unknown
103
103
 
104
104
  /**
105
105
  Same as `ReadonlyDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `ReadonlyDeep`.
@@ -171,8 +171,8 @@ export type Merge<Destination, Source> = {
171
171
  [Key in keyof OmitIndexSignature<Destination & Source>]: Key extends keyof Source
172
172
  ? Source[Key]
173
173
  : Key extends keyof Destination
174
- ? Destination[Key]
175
- : never
174
+ ? Destination[Key]
175
+ : never
176
176
  } & PickIndexSignature<Destination & Source>
177
177
 
178
178
  /**