@trackunit/shared-utils 0.0.13 → 0.0.14
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.
- package/index.cjs.js +12 -8
- package/index.esm.js +12 -8
- package/package.json +1 -1
- package/src/groupBy/groupBy.d.ts +8 -4
package/index.cjs.js
CHANGED
|
@@ -423,24 +423,28 @@ searchTerm) => {
|
|
|
423
423
|
/**
|
|
424
424
|
* Group an array of items by a key.
|
|
425
425
|
*
|
|
426
|
-
* @
|
|
427
|
-
* @
|
|
428
|
-
* @
|
|
429
|
-
* @
|
|
426
|
+
* @template T The type of items in the list.
|
|
427
|
+
* @template K The type of the key.
|
|
428
|
+
* @param {T[]} list The array of items to group.
|
|
429
|
+
* @param {(item: T) => K} getKey A function to get the key to group by.
|
|
430
|
+
* @returns {Map<K, T[]>} A map of the items grouped by the key.
|
|
431
|
+
* @example
|
|
432
|
+
* groupBy([{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }], p => p.surname)
|
|
433
|
+
* Returns: Map { "Doe" => [{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }] }
|
|
430
434
|
*/
|
|
431
435
|
function groupBy(list, getKey) {
|
|
432
|
-
const
|
|
436
|
+
const groupedMap = new Map();
|
|
433
437
|
list.forEach(item => {
|
|
434
438
|
const key = getKey(item);
|
|
435
|
-
const collection =
|
|
439
|
+
const collection = groupedMap.get(key);
|
|
436
440
|
if (!collection) {
|
|
437
|
-
|
|
441
|
+
groupedMap.set(key, [item]);
|
|
438
442
|
}
|
|
439
443
|
else {
|
|
440
444
|
collection.push(item);
|
|
441
445
|
}
|
|
442
446
|
});
|
|
443
|
-
return
|
|
447
|
+
return groupedMap;
|
|
444
448
|
}
|
|
445
449
|
|
|
446
450
|
const UUID_HYPHEN_POSITIONS = [8, 12, 16, 20];
|
package/index.esm.js
CHANGED
|
@@ -419,24 +419,28 @@ searchTerm) => {
|
|
|
419
419
|
/**
|
|
420
420
|
* Group an array of items by a key.
|
|
421
421
|
*
|
|
422
|
-
* @
|
|
423
|
-
* @
|
|
424
|
-
* @
|
|
425
|
-
* @
|
|
422
|
+
* @template T The type of items in the list.
|
|
423
|
+
* @template K The type of the key.
|
|
424
|
+
* @param {T[]} list The array of items to group.
|
|
425
|
+
* @param {(item: T) => K} getKey A function to get the key to group by.
|
|
426
|
+
* @returns {Map<K, T[]>} A map of the items grouped by the key.
|
|
427
|
+
* @example
|
|
428
|
+
* groupBy([{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }], p => p.surname)
|
|
429
|
+
* Returns: Map { "Doe" => [{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }] }
|
|
426
430
|
*/
|
|
427
431
|
function groupBy(list, getKey) {
|
|
428
|
-
const
|
|
432
|
+
const groupedMap = new Map();
|
|
429
433
|
list.forEach(item => {
|
|
430
434
|
const key = getKey(item);
|
|
431
|
-
const collection =
|
|
435
|
+
const collection = groupedMap.get(key);
|
|
432
436
|
if (!collection) {
|
|
433
|
-
|
|
437
|
+
groupedMap.set(key, [item]);
|
|
434
438
|
}
|
|
435
439
|
else {
|
|
436
440
|
collection.push(item);
|
|
437
441
|
}
|
|
438
442
|
});
|
|
439
|
-
return
|
|
443
|
+
return groupedMap;
|
|
440
444
|
}
|
|
441
445
|
|
|
442
446
|
const UUID_HYPHEN_POSITIONS = [8, 12, 16, 20];
|
package/package.json
CHANGED
package/src/groupBy/groupBy.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Group an array of items by a key.
|
|
3
3
|
*
|
|
4
|
-
* @
|
|
5
|
-
* @
|
|
6
|
-
* @
|
|
7
|
-
* @
|
|
4
|
+
* @template T The type of items in the list.
|
|
5
|
+
* @template K The type of the key.
|
|
6
|
+
* @param {T[]} list The array of items to group.
|
|
7
|
+
* @param {(item: T) => K} getKey A function to get the key to group by.
|
|
8
|
+
* @returns {Map<K, T[]>} A map of the items grouped by the key.
|
|
9
|
+
* @example
|
|
10
|
+
* groupBy([{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }], p => p.surname)
|
|
11
|
+
* Returns: Map { "Doe" => [{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }] }
|
|
8
12
|
*/
|
|
9
13
|
export declare function groupBy<T, K>(list: T[], getKey: (item: T) => K): Map<K, T[]>;
|