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

fix: GroupingGetterFunction should be allowed to return arbitrary value #957

Merged
merged 1 commit into from
Dec 29, 2023

Conversation

jesenko
Copy link
Contributor

@jesenko jesenko commented Dec 29, 2023

Underlying slickgrid logic only takes into account equality between values returned for grouping individial rows. Getter thus does not need to return string but might return arbitrary value (e.g. string, number, Date).

Typing return value as unknown will force underlying logic using grouping getter to take into consideration that user might specify grouping function that returns arbitrary value type for individual row. With previous getter signature, user was forced to specify function that returned string, which is however not currenly needed by internal slickgrid logic.

@ghiscoding
Copy link
Collaborator

I again disagree with this change, the GroupingGetterFunction is meant to be used by the getter property to get the column name which should always be a string. If I am wrong in my assumption then prove me wrong or else I will refuse this PR

@jesenko
Copy link
Contributor Author

jesenko commented Dec 29, 2023

We are currently trying to port our quite extensive usage of the original, slightly modified SlickGrid to this fork and this surfaced when typechecking our codebase.

I guess that getter might contain

a) field of the row which is used for grouping (e.g. getter: "some_field" will result in row["some_field"] being used for grouping), OR
b) function, that takes row as argument and returns value to be used for grouping (not column / field name). E.g. getter: (row) => row["some_field"] || row["other_field"]. Note that return type of the function will be of whatever row["some_field"] or row["other_field"] contains, not string.

I belive relevant logic is here: https://github.com/6pac/SlickGrid/blob/508aa7b872408c9371f77267921bb053d1f7e290/src/slick.dataview.ts#L905C6-L905C6

@ghiscoding
Copy link
Collaborator

both a & b are already covered by the interface

type GroupingGetterFunction<T = any> = (value: T) => string;

interface Grouping<T = any> {
   // 
   getter?: string | GroupingGetterFunction<T>; can be a string or a function returning a string
}

so again, the interface seems correct as it is

@ghiscoding
Copy link
Collaborator

ghiscoding commented Dec 29, 2023

ahh actually I see what you mean, when it's a function it returns the item value directly. in that case I think any is better since that is what is used for the value type as shown below (unless you also change the value type to unknown)

protected extractGroups(rows: any[], parentGroup?: SlickGroup_) {
let group: SlickGroup_;
let val: any;

@ghiscoding ghiscoding changed the title GroupingGetterFunction should be allowed to return arbitrary value fix: GroupingGetterFunction should be allowed to return arbitrary value Dec 29, 2023
@jesenko
Copy link
Contributor Author

jesenko commented Dec 29, 2023

Agreed - will change return type of function to any, probably can be refactored latter to also more fully typecheck underlying logic. Thank you!

Underlying slickgrid logic only takes into account equality between values
returned for grouping individial rows. Getter thus does not need to
return `string` but might return arbitrary value (e.g. string, number, Date).
@jesenko jesenko force-pushed the grouping-getter-function-typing branch from b99b512 to ae8cb9b Compare December 29, 2023 19:21
@@ -3,7 +3,7 @@ import { GroupingComparerItem } from "./groupingComparerItem.interface";
import { GroupingFormatterItem } from "./groupingFormatterItem.interface";
import { SortDirectionNumber } from "./sortDirectionNumber.enum";

export type GroupingGetterFunction<T = any> = (value: T) => string;
export type GroupingGetterFunction<T = any> = (value: T) => any;
Copy link
Collaborator

@ghiscoding ghiscoding Dec 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now thinking about it and I think it should actually be returning type T when the Generic Type is provided for the item datacontext type (which defaults to any when not provided), it is for the value so that should be the correct type to use. Sorry for asking to change it so many time, I just realized it now 😅

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

damn I think I'm wrong again and this is the value not the datacontext, so it could really be anything

@ghiscoding ghiscoding changed the title fix: GroupingGetterFunction should be allowed to return arbitrary value fix: GroupingGetterFunction should return value Generic Type Dec 29, 2023
@ghiscoding ghiscoding changed the title fix: GroupingGetterFunction should return value Generic Type fix: GroupingGetterFunction should be allowed to return arbitrary value Dec 29, 2023
@ghiscoding ghiscoding merged commit 72af590 into 6pac:master Dec 29, 2023
2 checks passed
@ghiscoding
Copy link
Collaborator

This is now available in the latest release v5.7.0, thanks again for your contributions

@jesenko
Copy link
Contributor Author

jesenko commented Dec 29, 2023

Thank you - for merging this and also for maintaining this great lib and making it future-proof!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants