Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details →
Socket
Book a DemoInstallSign in
Socket

array-group-to-record

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-group-to-record - npm Package Compare versions

Comparing version
1.0.7
to
1.0.8
+78
src/index.ts
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"
}