@valkyriestudios/utils 12.5.0 → 12.6.1

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/README.md CHANGED
@@ -30,7 +30,7 @@ isNotEmptyArray([]); // FALSE
30
30
  isNotEmptyArray([0, 1, 2]); // TRUE
31
31
  ```
32
32
 
33
- - **mapKey(val:array[Object], key:string, opts:object={})**
33
+ - **mapKey(val:Record[], key:string, opts:object={})**
34
34
  Map a non-primitive object array into an object map by key
35
35
  ```js
36
36
  mapKey([
@@ -99,7 +99,7 @@ output:
99
99
  }
100
100
  ```
101
101
 
102
- - **mapFn(val:array[Object], key:Function, opts:object={})**
102
+ - **mapFn(val:Record[], key:Function, opts:object={})**
103
103
  Same behavior as mapKey but instead of a key, a function is passed to generate your own key. Eg:
104
104
 
105
105
  ```js
@@ -120,7 +120,7 @@ output:
120
120
 
121
121
  options are the same as the mapKey function
122
122
 
123
- - **mapPrimitive(val:any, opts:object={valtrim:false,keyround:false,valround:false})**
123
+ - **mapPrimitive(val:any[], opts:object={valtrim:false,keyround:false,valround:false})**
124
124
  Map an array of primitives (number/string)
125
125
  ```js
126
126
  mapPrimitive([1,2,3]); // {1: 1, 2: 2, 3: 3}
@@ -128,6 +128,62 @@ mapPrimitive(['hello', 'hello', 'foo', 'bar']); // {hello: 'hello', foo: 'foo',
128
128
  mapPrimitive(['hello', ' hello', 'foo', ' foo'], {valtrim: true}); // {hello: 'hello', foo: 'foo'}
129
129
  ```
130
130
 
131
+ - **groupBy(val:Record[], handler:Function|string)**
132
+ Return a grouped object from an array. This function **will automatically filter out any non/empty objects**.
133
+
134
+ Example usage when using a **function** as the handler
135
+ ```typescript
136
+ /* The output of the function will be what the key is on the map */
137
+ const group = groupBy([
138
+ {tally: 20, name: 'Peter'},
139
+ {tally: 40, name: 'Jake'},
140
+ {tally: 5, name: 'Bob'},
141
+ ], el => el.tally > 15);
142
+
143
+ /* Expected output: */
144
+ {
145
+ false: [{tally: 5, name: 'Bob'}],
146
+ true: [{tally: 20, name: 'Peter'}, {tally: 40, name: 'Jake'}],
147
+ }
148
+
149
+ /* Can also work with a property return */
150
+ const group = groupBy([
151
+ {role: 'user', name: 'Peter'},
152
+ {role: 'user', name: 'Jake'},
153
+ {role: 'guest', name: 'Bob'},
154
+ {name: 'Alice'},
155
+ ], el => el.role || 'other');
156
+
157
+ /* Expected output: */
158
+ {
159
+ user: [{role: 'user', name: 'Peter'}, {role: 'user', name: 'Jake'}],
160
+ guest: [{role: 'guest', name: 'Bob'}],
161
+ other: [{name: 'Alice'}],
162
+ }
163
+ ```
164
+ **Take note**: If the function returns an undefined or empty string the object will be added to a fallback group called '_'
165
+
166
+
167
+ Example usage when using a **string** as the handler to denote a grouping by a certain property name
168
+ ```typescript
169
+ const group = groupBy([
170
+ {role: 'user', name: 'Peter'},
171
+ {role: 'user', name: 'Jake'},
172
+ {role: 'guest', name: 'Bob'},
173
+ {name: 'Alice'},
174
+ ], 'role');
175
+
176
+ /* Expected output: */
177
+ {
178
+ user: [{role: 'user', name: 'Peter'}, {role: 'user', name: 'Jake'}],
179
+ guest: [{role: 'guest', name: 'Bob'}],
180
+ _: [{name: 'Alice'}],
181
+ }
182
+ ```
183
+
184
+ **Take note**: any object without the key will be added to a fallback group called '_'
185
+
186
+
131
187
  - **dedupe(val:Array)**
132
188
  Remove all duplicates from an array, behind the scenes it uses the fnv 1A hash algorithm to performantly do comparisons.
133
189
  ```js
@@ -0,0 +1,3 @@
1
+ type Handler<T> = (val: T) => string | number | boolean;
2
+ declare function groupBy<T extends Record<string, any>>(arr: T[], handler: Handler<T> | string): Record<string, T[]>;
3
+ export { groupBy, groupBy as default };
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = exports.groupBy = void 0;
4
+ const isNotEmpty_1 = require("../object/isNotEmpty");
5
+ const FALLBACK = '_';
6
+ const defaultHandler = () => FALLBACK;
7
+ function groupBy(arr, handler) {
8
+ if (!Array.isArray(arr))
9
+ return {};
10
+ const acc = {};
11
+ const n_handler = typeof handler === 'function'
12
+ ? handler
13
+ : typeof handler === 'string' && handler.length
14
+ ? ((el) => el[handler] ?? FALLBACK)
15
+ : defaultHandler;
16
+ let key;
17
+ const set = new Set();
18
+ for (const el of arr) {
19
+ if (!(0, isNotEmpty_1.isNotEmptyObject)(el))
20
+ continue;
21
+ key = n_handler(el);
22
+ if (key === undefined || (typeof key === 'string' && !key.length))
23
+ key = FALLBACK;
24
+ if (set.has(key)) {
25
+ acc[key].push(el);
26
+ continue;
27
+ }
28
+ acc[key] = [el];
29
+ set.add(key);
30
+ }
31
+ return acc;
32
+ }
33
+ exports.groupBy = groupBy;
34
+ exports.default = groupBy;
package/array/index.d.ts CHANGED
@@ -5,6 +5,7 @@ import { join } from './join';
5
5
  import { mapFn } from './mapFn';
6
6
  import { mapKey } from './mapKey';
7
7
  import { mapPrimitive } from './mapPrimitive';
8
+ import { groupBy } from './groupBy';
8
9
  import { shuffle } from './shuffle';
9
10
  import { sort } from './sort';
10
- export { dedupe, isArray, isArray as is, isNotEmptyArray, isNotEmptyArray as isNotEmpty, isNotEmptyArray as isNeArray, isNotEmptyArray as isNe, join, mapFn, mapKey, mapPrimitive, shuffle, sort };
11
+ export { dedupe, isArray, isArray as is, isNotEmptyArray, isNotEmptyArray as isNotEmpty, isNotEmptyArray as isNeArray, isNotEmptyArray as isNe, join, mapFn, mapKey, mapPrimitive, groupBy, shuffle, sort };
package/array/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sort = exports.shuffle = exports.mapPrimitive = exports.mapKey = exports.mapFn = exports.join = exports.isNe = exports.isNeArray = exports.isNotEmpty = exports.isNotEmptyArray = exports.is = exports.isArray = exports.dedupe = void 0;
3
+ exports.sort = exports.shuffle = exports.groupBy = exports.mapPrimitive = exports.mapKey = exports.mapFn = exports.join = exports.isNe = exports.isNeArray = exports.isNotEmpty = exports.isNotEmptyArray = exports.is = exports.isArray = exports.dedupe = void 0;
4
4
  const dedupe_1 = require("./dedupe");
5
5
  Object.defineProperty(exports, "dedupe", { enumerable: true, get: function () { return dedupe_1.dedupe; } });
6
6
  const is_1 = require("./is");
@@ -19,6 +19,8 @@ const mapKey_1 = require("./mapKey");
19
19
  Object.defineProperty(exports, "mapKey", { enumerable: true, get: function () { return mapKey_1.mapKey; } });
20
20
  const mapPrimitive_1 = require("./mapPrimitive");
21
21
  Object.defineProperty(exports, "mapPrimitive", { enumerable: true, get: function () { return mapPrimitive_1.mapPrimitive; } });
22
+ const groupBy_1 = require("./groupBy");
23
+ Object.defineProperty(exports, "groupBy", { enumerable: true, get: function () { return groupBy_1.groupBy; } });
22
24
  const shuffle_1 = require("./shuffle");
23
25
  Object.defineProperty(exports, "shuffle", { enumerable: true, get: function () { return shuffle_1.shuffle; } });
24
26
  const sort_1 = require("./sort");
package/index.d.ts CHANGED
@@ -61,16 +61,21 @@ declare module "array/mapPrimitive" {
61
61
  function mapPrimitive(arr: unknown[], opts?: mapOptions): mapReturn;
62
62
  export { mapPrimitive, mapPrimitive as default };
63
63
  }
64
- declare module "array/shuffle" {
65
- function shuffle(arr: unknown[]): void;
66
- export { shuffle, shuffle as default };
67
- }
68
64
  declare module "object/isNotEmpty" {
69
65
  function isNotEmptyObject(val: unknown): val is {
70
66
  [key: string]: any;
71
67
  };
72
68
  export { isNotEmptyObject, isNotEmptyObject as default };
73
69
  }
70
+ declare module "array/groupBy" {
71
+ type Handler<T> = (val: T) => string | number | boolean;
72
+ function groupBy<T extends Record<string, any>>(arr: T[], handler: Handler<T> | string): Record<string, T[]>;
73
+ export { groupBy, groupBy as default };
74
+ }
75
+ declare module "array/shuffle" {
76
+ function shuffle(arr: unknown[]): void;
77
+ export { shuffle, shuffle as default };
78
+ }
74
79
  declare module "array/sort" {
75
80
  interface sortOptions {
76
81
  filter_fn?: (el: any) => boolean;
@@ -92,9 +97,10 @@ declare module "array/index" {
92
97
  import { mapFn } from "array/mapFn";
93
98
  import { mapKey } from "array/mapKey";
94
99
  import { mapPrimitive } from "array/mapPrimitive";
100
+ import { groupBy } from "array/groupBy";
95
101
  import { shuffle } from "array/shuffle";
96
102
  import { sort } from "array/sort";
97
- export { dedupe, isArray, isArray as is, isNotEmptyArray, isNotEmptyArray as isNotEmpty, isNotEmptyArray as isNeArray, isNotEmptyArray as isNe, join, mapFn, mapKey, mapPrimitive, shuffle, sort };
103
+ export { dedupe, isArray, isArray as is, isNotEmptyArray, isNotEmptyArray as isNotEmpty, isNotEmptyArray as isNeArray, isNotEmptyArray as isNe, join, mapFn, mapKey, mapPrimitive, groupBy, shuffle, sort };
98
104
  }
99
105
  declare module "boolean/is" {
100
106
  function isBoolean(val: unknown): val is boolean;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = exports.isNotEmptyObject = void 0;
4
4
  function isNotEmptyObject(val) {
5
- return Object.prototype.toString.call(val) === '[object Object]' && Object.keys(val).length > 0;
5
+ return Object.prototype.toString.call(val) === '[object Object]' && Object.keys(val).length !== 0;
6
6
  }
7
7
  exports.isNotEmptyObject = isNotEmptyObject;
8
8
  exports.default = isNotEmptyObject;
package/package.json CHANGED
@@ -1 +1 @@
1
- { "name": "@valkyriestudios/utils", "version": "12.5.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" }
1
+ { "name": "@valkyriestudios/utils", "version": "12.6.1", "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" }