Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/group list modifier #56

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@togglecorp/fujs",
"version": "2.1.1",
"version": "2.2.0",
"description": "Friendly Utility for JS",
"files": [
"/build"
Expand Down
37 changes: 33 additions & 4 deletions src/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
52 changes: 47 additions & 5 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ type ListModifier<T, Q, K extends OptionKey> = (
index: number,
acc: Q[]
) => Q;
type GroupListModifier<T, Q, K extends OptionKey> = (
type GroupItemModifier<T, Q, K extends OptionKey> = (
element: T,
key: K,
index: number,
acc: Partial<Record<K, Q[]>>
) => Q;
type GroupListModifier<T, R> = (
elements: T[],
// FIXME: we need to pass key of type K
key: string,
) => R;
export type KeySelector<T, K extends OptionKey> = (element: T, index: number) => K;

type NewKeySelector<T, K extends OptionKey> = (key: string, element: T) => K;
Expand Down Expand Up @@ -172,17 +177,42 @@ export function listToGroupList<T, K extends OptionKey>(
export function listToGroupList<T, Q, K extends OptionKey>(
list: T[],
keySelector: KeySelector<T, K>,
modifier: GroupListModifier<T, Q, K>,
modifier: GroupItemModifier<T, Q, K>,
): Obj<Q[]>;
export function listToGroupList<T, Q, K extends OptionKey>(
list: Maybe<T[]>,
keySelector: KeySelector<T, K>,
modifier: GroupListModifier<T, Q, K>,
modifier: GroupItemModifier<T, Q, K>,
): Obj<Q[]> | undefined;
export function listToGroupList<T, Q, K extends OptionKey>(
export function listToGroupList<T, Q, K extends OptionKey, R>(
list: T[],
keySelector: KeySelector<T, K>,
modifier: GroupItemModifier<T, Q, K>,
groupModifier: GroupListModifier<Q, R>,
): Obj<R>;
export function listToGroupList<T, Q, K extends OptionKey, R>(
list: Maybe<T[]>,
keySelector: KeySelector<T, K>,
modifier?: GroupListModifier<T, Q, K>,
modifier: GroupItemModifier<T, Q, K>,
groupModifier: GroupListModifier<Q, R>,
): Obj<R> | undefined;
export function listToGroupList<T, K extends OptionKey, R>(
list: T[],
keySelector: KeySelector<T, K>,
modifier: undefined,
groupModifier: GroupListModifier<T, R>,
): Obj<R>;
export function listToGroupList<T, K extends OptionKey, R>(
list: Maybe<T[]>,
keySelector: KeySelector<T, K>,
modifier: undefined,
groupModifier: GroupListModifier<T, R>,
): Obj<R> | undefined;
export function listToGroupList<T, Q, K extends OptionKey, R>(
list: Maybe<T[]>,
keySelector: KeySelector<T, K>,
modifier?: GroupItemModifier<T, Q, K>,
groupModifier?: GroupListModifier<T | Q, R>,
) {
if (isNotDefined(list)) {
return undefined;
Expand All @@ -205,5 +235,17 @@ export function listToGroupList<T, Q, K extends OptionKey>(
},
{},
);
if (groupModifier) {
return Object.keys(val).reduce<Partial<Record<K, R>>>(
(acc, key) => {
const value = val[key as K];
if (value) {
acc[key as K] = groupModifier(value, key);
}
return acc;
},
{},
);
}
return val;
}
Loading