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

Supporting wildcards #1912

Open
SunPj opened this issue Oct 1, 2022 · 8 comments
Open

Supporting wildcards #1912

SunPj opened this issue Oct 1, 2022 · 8 comments

Comments

@SunPj
Copy link

SunPj commented Oct 1, 2022

Supporting wildcards

It would be useful to be able to pass wildcards (*) in path for setIn removeIn, updateIn methods. A wildcard should match all elements in an array or all props in an object.

Example

{
    a: {
        b: 1,
        c: [
            {x: 1, y: 2}
        ]
    },
    b: {
        b: 1,
        c: [
            {x: 1, y: 2}
        ]
    }
}
deleteIn(x, ['*', 'c', 'x'])

Out

{
    a: {
        b: 1,
        c: [
            {y: 2}
        ]
    },
    b: {
        b: 1,
        c: [
            {y: 2}
        ]
    }
}
@jdeniau
Copy link
Member

jdeniau commented Oct 1, 2022

Seems interesting, but how do you handle * as, real key?

@bdurrer
Copy link
Contributor

bdurrer commented Oct 2, 2022

maybe that would need extra functions, e.g. setAll, deleteAll,... or a matcher function. On the other hand, it's proabably easier to just do forEach with set than that, I imagine merging all the changes across multiple layers in one go would be difficult

@SunPj
Copy link
Author

SunPj commented Oct 3, 2022

Seems interesting, but how do you handle * as, real key?

What if we use come object type or constant value instead of plan string there?
For example predefined ANY type const any = {}; so path looks like [any, 'c', 'a']

@SunPj
Copy link
Author

SunPj commented Oct 3, 2022

On the other hand, it's proabably easier to just do forEach with set

In case we have nested structure where array or map elements have similar structure, it becomes too cumbersome to write such update methods with forEach and Set.
Each * replace one one nested forEach.

Say we want to delete people's sensitive data when authenticated user signs out, so no sensitive data is displayed on the site page. If user is nested structure and we have something like city -> street -> house -> user then we will end up having 4 nested update functions, where the syntax with wildcard will be pretty expressive ['*', '*', '*', 'user', 'email']

@jdeniau
Copy link
Member

jdeniau commented Oct 3, 2022

After some though, it may be possible to inject a RegExp object in the path. What do you think?

@SunPj
Copy link
Author

SunPj commented Oct 3, 2022

After some though, it may be possible to inject a RegExp object in the path. What do you think?

Yes, this is a good idea and provides even more flexibility

@bdurrer
Copy link
Contributor

bdurrer commented Oct 3, 2022

RegEx could be keys too. Having a Map or Set with RegEx keys is unlikely, but not impossible.
The problem with existing functions is, that anything can be a key. The "ANY" special constant would work, but might be clunky

@jdeniau
Copy link
Member

jdeniau commented Oct 26, 2024

@bdurrer fou are right, anything can be a key.

@SunPj I think that we might work with a special exported ANY symbol. Are you willing to work on this?

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

No branches or pull requests

3 participants