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

Allow functions as keys #313

Closed
matt-tingen opened this issue Jun 8, 2019 · 5 comments
Closed

Allow functions as keys #313

matt-tingen opened this issue Jun 8, 2019 · 5 comments
Assignees
Milestone

Comments

@matt-tingen
Copy link

matt-tingen commented Jun 8, 2019

#261 discusses the issues with the type definitions with nested keys, but the resolution is not type safe. To make it type safe, functions could be allowed as keys (e.g. keys: [item => item.foo.bar]) as described by @ceymard.

This also allows for simplifying the case of searching derived fields such as full name e.g. item => `${item.firstName} ${item.lastName}` which faces issues similar to those described in #302. Obviously, an intermediate object could be used for this, but that seems excessive compared to having functional keys to search.

I'd be happy to implement a PR for this if the behavior is desired.

@krisk
Copy link
Owner

krisk commented Jun 21, 2019

This sounds interesting, @matt-tingen. Let's have this in a PR and take a look!

@krisk krisk added the feature label Jun 21, 2019
@matt-tingen
Copy link
Author

After messing around with it some, I ended up on the same solution as @ceymard which can be summed up with the following:

  export type FuseKey<T> =
    | keyof T
    | { name: keyof T; weight: number }
    | { name: string, weight?: number, getFn: (obj: T) => string }

We can't trivially use a bare function as the key because of how the weights are stored. There's a couple of techniques involving Maps which could work around this, but that would increase the minimum browser/node version which makes them non-starters in my opinion.

Unless @ceymard is still interested in this change and wants to champion it, I'll pull in their changes, update it, and make a PR later this weekend.

@ceymard
Copy link
Contributor

ceymard commented Jun 21, 2019 via email

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label Mar 25, 2020
@krisk krisk removed the Stale label Mar 28, 2020
@krisk krisk self-assigned this Mar 28, 2020
@krisk krisk added this to the v5.3.0 milestone Apr 13, 2020
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

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

Successfully merging a pull request may close this issue.

3 participants