array-group-to-record
Advanced tools
+78
| import hashSum from 'hash-sum'; | ||
| export interface IOptionsForRecord<T> | ||
| { | ||
| getKey?(item: T, index: number, arr: T[]): string | ||
| init?(): Record<string, T[]>, | ||
| } | ||
| export interface IOptionsForMap<T> | ||
| { | ||
| getKey?(item: T, index: number, arr: T[]): any | ||
| init?(): Map<any, T[]>, | ||
| } | ||
| export function handleOptions<O extends IOptionsForRecord<any> | IOptionsForMap<any>>(options: O): O | ||
| { | ||
| const getKey = options?.getKey ?? ((item) => hashSum(item)); | ||
| return { | ||
| ...options, | ||
| getKey, | ||
| } | ||
| } | ||
| export function arrayGroupToRecord<T>(arr: T[], options?: IOptionsForRecord<T>) | ||
| { | ||
| const { getKey, init } = handleOptions(options); | ||
| return arr.reduce((map, item, index, arr) => | ||
| { | ||
| const id = getKey(item, index, arr); | ||
| map[id] ??= []; | ||
| map[id].push(item); | ||
| return map | ||
| }, init?.() ?? {} as Record<string, T[]>); | ||
| } | ||
| export function arrayGroupToMap<T>(arr: T[], options?: IOptionsForMap<T>) | ||
| { | ||
| const { getKey, init } = handleOptions(options); | ||
| return arr.reduce((map, item, index, arr) => | ||
| { | ||
| const id = getKey(item, index, arr); | ||
| const a = map.get(id) ?? []; | ||
| a.push(item); | ||
| map.set(id, a); | ||
| return map | ||
| }, init?.() ?? new Map() as Map<any, T[]>); | ||
| } | ||
| export function sumGroup<T extends Record<any, any[]> | Map<any, any[]>>(group: T) | ||
| { | ||
| let len = 0; | ||
| if (typeof group.forEach === 'undefined') | ||
| { | ||
| // @ts-ignore | ||
| group = Object.values(group); | ||
| } | ||
| // @ts-ignore | ||
| group.forEach(b => len += b.length); | ||
| return len; | ||
| } | ||
| export default arrayGroupToRecord |
+10
-0
@@ -6,2 +6,12 @@ # Change Log | ||
| ## [1.0.8](https://github.com/bluelovers/ws-array/compare/array-group-to-record@1.0.7...array-group-to-record@1.0.8) (2022-10-10) | ||
| ### 🔖 Miscellaneous | ||
| * . ([2c4aa0a](https://github.com/bluelovers/ws-array/commit/2c4aa0ac4545a8f3be79a20835cb973690cfaac8)) | ||
| ## [1.0.7](https://github.com/bluelovers/ws-array/compare/array-group-to-record@1.0.6...array-group-to-record@1.0.7) (2022-09-26) | ||
@@ -8,0 +18,0 @@ |
+2
-2
| { | ||
| "name": "array-group-to-record", | ||
| "version": "1.0.7", | ||
| "version": "1.0.8", | ||
| "description": "", | ||
@@ -68,3 +68,3 @@ "keywords": [ | ||
| "packageManager": "yarn@^1.22.11", | ||
| "gitHead": "fcebb3831d3a25e50d06481d9d4e9a56b3d3036d" | ||
| "gitHead": "c83428b467812ee10e5e6f72b46ff79a64d763a2" | ||
| } |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
31950
5.68%16
6.67%238
30.05%