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

Consider dynamic parent/child matching fn() #22

Closed
tremendus opened this issue Oct 31, 2018 · 1 comment
Closed

Consider dynamic parent/child matching fn() #22

tremendus opened this issue Oct 31, 2018 · 1 comment

Comments

@tremendus
Copy link

return parentNode.get(key_id) === item[key_parent];

I have a need to generate a tree from a collection (array of objects). But my paths are nested - eg:

{
  id: 100,
  field: 'val',
  // ... 
  meta: {
    parent: 'abc-123'
  }
}

I need to either pass dot-notation paths (and use a third party lib, like _ or object-path) or, to avoid dependencies, perhaps consider passing a matching fn to the constructor, which I can use third party libs without affecting deps on this module.

// list-to-tree constructor - add new param to end
 constructor(list, options = {}, matcher) {
    const _list = list.map((item) => item);

    options = Object.assign({}, defaultOptions, options);
    this.options = options;
    const { key_id, key_parent } = options;

    sortBy(_list, key_parent, key_id);
    const tree = new IronTree({ [key_id]: 0 });
    _list.forEach((item, index) => {
      tree.add((parentNode) => {
       // crude example below:
        return matcher ? matcher(item, key_id, parentNode, key_parent) : parentNode.get(key_id) === item[key_parent];
      }, item);
    });

    this.tree = tree;
  }

Then you could pass whatever id-to-id matching fns you need.

// my application code
const objectPath = require('object-path')
matcher = function (item, key_id, parentNode, key_parent) {
  return objectPath.get(parentNode, key_id) === objectPath.get(item, key_parent)
}

Now I can use:

new Tree(list, {
  key_id: 'id',
  key_parent: 'meta.parent'
}, matcher)

Thoughts?

@DenQ
Copy link
Owner

DenQ commented Nov 1, 2018

@tremendus good idea!
I have a lot of work right now. I can do this next week.
Or you can transform your list now.

...
{
  meta: {
    parent: 'abc-123'
  }
}

to

...
{
  parent: 'abs-123',
  meta: {
    parent: 'abc-123'
  }
}

@tremendus tremendus closed this as not planned Won't fix, can't repro, duplicate, stale Mar 15, 2023
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