@valkyriestudios/utils 12.6.1 → 12.7.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.
- package/array/groupBy.d.ts +20 -0
- package/array/groupBy.js +6 -6
- package/package.json +1 -1
package/array/groupBy.d.ts
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
1
|
type Handler<T> = (val: T) => string | number | boolean;
|
|
2
|
+
/**
|
|
3
|
+
* Return a grouped object from an array.
|
|
4
|
+
* Take Note: This function will automatically filter out any non/empty objects from the array
|
|
5
|
+
*
|
|
6
|
+
* Example:
|
|
7
|
+
* const group = groupBy([
|
|
8
|
+
* {tally: 20, name: 'Peter'},
|
|
9
|
+
* {tally: 40, name: 'Jake'},
|
|
10
|
+
* {tally: 5, name: 'Bob'},
|
|
11
|
+
* ], el => el.tally > 15);
|
|
12
|
+
* Output:
|
|
13
|
+
* {
|
|
14
|
+
* false: [{tally: 5, name: 'Bob'}],
|
|
15
|
+
* true: [{tally: 20, name: 'Peter'}, {tally: 40, name: 'Jake'}],
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* @param {T[]} arr - Array to group
|
|
19
|
+
* @param {Handler<T>|string} handler - String or a function, determines what to group by
|
|
20
|
+
* @returns {Record<string, T[]>}
|
|
21
|
+
*/
|
|
2
22
|
declare function groupBy<T extends Record<string, any>>(arr: T[], handler: Handler<T> | string): Record<string, T[]>;
|
|
3
23
|
export { groupBy, groupBy as default };
|
package/array/groupBy.js
CHANGED
|
@@ -10,23 +10,23 @@ function groupBy(arr, handler) {
|
|
|
10
10
|
const acc = {};
|
|
11
11
|
const n_handler = typeof handler === 'function'
|
|
12
12
|
? handler
|
|
13
|
-
: typeof handler === 'string'
|
|
14
|
-
? ((el) => el[handler]
|
|
13
|
+
: typeof handler === 'string'
|
|
14
|
+
? ((el) => el[handler])
|
|
15
15
|
: defaultHandler;
|
|
16
16
|
let key;
|
|
17
|
-
|
|
18
|
-
for (
|
|
17
|
+
let el;
|
|
18
|
+
for (let i = 0; i < arr.length; i++) {
|
|
19
|
+
el = arr[i];
|
|
19
20
|
if (!(0, isNotEmpty_1.isNotEmptyObject)(el))
|
|
20
21
|
continue;
|
|
21
22
|
key = n_handler(el);
|
|
22
23
|
if (key === undefined || (typeof key === 'string' && !key.length))
|
|
23
24
|
key = FALLBACK;
|
|
24
|
-
if (
|
|
25
|
+
if (Array.isArray(acc[key])) {
|
|
25
26
|
acc[key].push(el);
|
|
26
27
|
continue;
|
|
27
28
|
}
|
|
28
29
|
acc[key] = [el];
|
|
29
|
-
set.add(key);
|
|
30
30
|
}
|
|
31
31
|
return acc;
|
|
32
32
|
}
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{ "name": "@valkyriestudios/utils", "version": "12.
|
|
1
|
+
{ "name": "@valkyriestudios/utils", "version": "12.7.0", "description": "A collection of single-function utilities for common tasks", "author": { "name": "Peter Vermeulen", "url": "https://www.linkedin.com/in/petervermeulen1/" }, "keywords": [ "utility", "library", "javascript", "js", "node", "bun" ], "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/ValkyrieStudios/utils.git" }, "bugs": { "url": "https://github.com/ValkyrieStudios/utils/issues" }, "homepage": "https://github.com/ValkyrieStudios/utils#readme", "types": "index.d.ts" }
|