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

Array support #9

Open
petermorlion opened this issue Oct 28, 2020 · 2 comments
Open

Array support #9

petermorlion opened this issue Oct 28, 2020 · 2 comments

Comments

@petermorlion
Copy link

Would you be interested in a PR for array support?

Say we have this object:

{
    name: "Peter",
    familyName: "Morlion",
    hobbies: [{
        name: "Reading",
        since: 1989
    }, {
        name: "Games",
        since: 1994
    }]
}

If I want to filter with ["name", "hobbies.name"] I'll get:

{
    name: "Peter"
}

It would be nice to get:

{
    name: "Peter",
    hobbies: [{
        name: "Reading"
    }, {
        name: "Games"
    }]
}
@doowb
Copy link
Collaborator

doowb commented Oct 28, 2020

A PR would be great!

I think it will require updating stringify-keys in glob-object to the latest version first to ensure the array keys are referenced correctly.
Then some other updates to glob-object may be required for "rebuilding" the returned object.

I tried with the following code with the latest versions of micromatch and stringify-keys, and it seems to get the correct keys:

'use strict';

const mm = require('micromatch');
const stringify = require('stringify-keys');

const obj = {
  name: 'Peter',
  familyName: 'Morlion',
  hobbies: [{
    name: 'Reading',
    since: 1989
  }, {
    name: 'Games',
    since: 1994
  }]
};

const keys = stringify(obj, '/');
const patterns = ['name', 'hobbies/*/name'];
const matches = mm(keys, patterns);
console.log(matches);
All keys
[
  'name',
  'familyName',
  'hobbies/0/name',
  'hobbies/0/since',
  'hobbies/1/name',
  'hobbies/1/since'
]

patterns
[ 'name', 'hobbies/*/name' ]

matches
[ 'name', 'hobbies/0/name', 'hobbies/1/name' ]

@petermorlion
Copy link
Author

I had a deeper look and think the solution lies in the set-value library. In glob-object, we have keys like a.0.b, a.1.b, etc. The numbers indicate indexes in arrays. We then call the set function with these keys. So I created a PR that I think should fix this (instead of creating objects with numbers as keys): jonschlinkert/set-value#26.

If you're OK with that PR and it's merged, I can update glob-object and then filter-object to incorporate these changes :)

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

2 participants