From 005b625490531c86dfcfd3fa3c171d0b2648be38 Mon Sep 17 00:00:00 2001 From: tnagorra Date: Sun, 4 Aug 2024 12:12:58 +0545 Subject: [PATCH 1/2] Add list modifier on listToGroupList --- src/transform.test.ts | 37 ++++++++++++++++++++++++++---- src/transform.ts | 52 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 80 insertions(+), 9 deletions(-) diff --git a/src/transform.test.ts b/src/transform.test.ts index 343f96f..aad68da 100644 --- a/src/transform.test.ts +++ b/src/transform.test.ts @@ -129,8 +129,37 @@ test('group list into map', () => { 'shyam', ], }; - expect(listToGroupList(input, (e) => e.id, (e) => e.name)).toEqual(outputTwo); - expect( - listToGroupList(undefined, (e: Input) => e.id, (e: Input) => e.name), - ).toEqual(undefined); + expect(listToGroupList( + input, + (e) => e.id, + (e) => e.name, + )).toEqual(outputTwo); + + const outputThree = { + 1: { key: '1', count: 3 }, + 2: { key: '2', count: 1 }, + }; + expect(listToGroupList( + input, + (e) => e.id, + (e) => e.name, + (ez, k) => ({ key: k, count: ez.length }), + )).toEqual(outputThree); + + const outputFour = { + 1: { key: '1', count: 3 }, + 2: { key: '2', count: 1 }, + }; + expect(listToGroupList( + input, + (e) => e.id, + undefined, + (ez, k) => ({ key: k, count: ez.length }), + )).toEqual(outputFour); + + expect(listToGroupList( + undefined, + (e: Input) => e.id, + (e: Input) => e.name, + )).toEqual(undefined); }); diff --git a/src/transform.ts b/src/transform.ts index 37e9098..39be3af 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -15,12 +15,17 @@ type ListModifier = ( index: number, acc: Q[] ) => Q; -type GroupListModifier = ( +type GroupItemModifier = ( element: T, key: K, index: number, acc: Partial> ) => Q; +type GroupListModifier = ( + elements: T[], + // FIXME: we need to pass key of type K + key: string, +) => R; export type KeySelector = (element: T, index: number) => K; type NewKeySelector = (key: string, element: T) => K; @@ -172,17 +177,42 @@ export function listToGroupList( export function listToGroupList( list: T[], keySelector: KeySelector, - modifier: GroupListModifier, + modifier: GroupItemModifier, ): Obj; export function listToGroupList( list: Maybe, keySelector: KeySelector, - modifier: GroupListModifier, + modifier: GroupItemModifier, ): Obj | undefined; -export function listToGroupList( +export function listToGroupList( + list: T[], + keySelector: KeySelector, + modifier: GroupItemModifier, + groupModifier: GroupListModifier, +): Obj; +export function listToGroupList( list: Maybe, keySelector: KeySelector, - modifier?: GroupListModifier, + modifier: GroupItemModifier, + groupModifier: GroupListModifier, +): Obj | undefined; +export function listToGroupList( + list: T[], + keySelector: KeySelector, + modifier: undefined, + groupModifier: GroupListModifier, +): Obj; +export function listToGroupList( + list: Maybe, + keySelector: KeySelector, + modifier: undefined, + groupModifier: GroupListModifier, +): Obj | undefined; +export function listToGroupList( + list: Maybe, + keySelector: KeySelector, + modifier?: GroupItemModifier, + groupModifier?: GroupListModifier, ) { if (isNotDefined(list)) { return undefined; @@ -205,5 +235,17 @@ export function listToGroupList( }, {}, ); + if (groupModifier) { + return Object.keys(val).reduce>>( + (acc, key) => { + const value = val[key as K]; + if (value) { + acc[key as K] = groupModifier(value, key); + } + return acc; + }, + {}, + ); + } return val; } From 4718f59ce67c703ac7f8f30838b9c94b413ecfb7 Mon Sep 17 00:00:00 2001 From: tnagorra Date: Sun, 4 Aug 2024 12:13:36 +0545 Subject: [PATCH 2/2] 2.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b731aac..e6d4588 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@togglecorp/fujs", - "version": "2.1.1", + "version": "2.2.0", "description": "Friendly Utility for JS", "files": [ "/build"