@naturalcycles/js-lib 14.194.0 → 14.195.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
|
*
|
package/dist/array/array.util.js
CHANGED
|
@@ -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.
|
|
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
|
*
|
|
@@ -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.
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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]
|
package/package.json
CHANGED
package/src/array/array.util.ts
CHANGED
|
@@ -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
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
/**
|