Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

omitDeep with function? #62

Closed
muuvmuuv opened this issue Jun 10, 2020 · 7 comments
Closed

omitDeep with function? #62

muuvmuuv opened this issue Jun 10, 2020 · 7 comments
Labels
question Further information is requested test Add potentially dangerous test cases

Comments

@muuvmuuv
Copy link

Is it possible to use a function in omitDeep? I would like to exclude an item and its children from an array but only if the id of an item matches. Please correct me if this isn't the correct function for this but with filterDeep I got an infinite loop (idk why).

This is what I expect:

omitDeep(
      items,
      (value) => {
          return value.id === itemId
      },
      {
          childrenPath: ['children'],
      }
 )

PS: Thanks for the great lib. This is a huge time saver for me in my current project.

@YuriGor
Copy link
Owner

YuriGor commented Jun 10, 2020

Hi, the only way I know to have infinite loop is to have circular references in your data - in this case, you need to enable checkCircular option.
If it's not the case then you found a bug, please share how to reproduce it.

Here is an example similar to what you need:
https://codepen.io/yurigor/pen/qBbbXxY?editors=0010

You can fork this pen, put your data and check if it's working for you.
But please share how did you got infinite loop.

@YuriGor YuriGor added the question Further information is requested label Jun 10, 2020
@muuvmuuv
Copy link
Author

Here is a reproducible example: https://stackblitz.com/edit/angular-ivy-b8uhnv?file=src%2Fapp%2Fhello.component.ts

I found out that there is a circular references inside self. Can I exclude this key explicitly?

@YuriGor
Copy link
Owner

YuriGor commented Jun 10, 2020

So if it's a circular ref then no bug here just enable circular check.
To exclude check keepCircular or replaceCircularBy filterDeep options

@YuriGor
Copy link
Owner

YuriGor commented Jun 10, 2020

As I see in your example - adding checkCircular:true helps
But it still looks strange for me why this 'self' property visited if childrenPath option specified,
So I will need to check it.
dump code here to not loose it if you will update your example:

var data = [
  {
    "id": "dh8ge0",
    "name": "Long it takes",
    "type": "distance",
    "self": this,
    "value": 2
  },
  {
    "id": "z765tghj",
    "name": "Capability 01",
    "type": "checkbox",
    "state": "active",
    "self": "COMPONENT",
    "changed": {
      "_isScalar": false,
      "observers": [],
      "closed": false,
      "isStopped": false,
      "hasError": false,
      "thrownError": null,
      "__isAsync": false
    }
  },
  {
    "id": "fh390hdas",
    "name": "Wildy",
    "self": "COMPONENT",
    "type": "wildcard"
  }
]

    console.clear()
    console.log(data)
    const newData = filterDeep(data, node => node.id !== 'z765tghj', {
      checkCircular: true,
      childrenPath: ['children'], 
      onFalse:{skipChildren:true}}
    );
    console.log(newData);

@YuriGor YuriGor added the test Add potentially dangerous test cases label Jun 10, 2020
@YuriGor
Copy link
Owner

YuriGor commented Jun 13, 2020

Ok, found, we need to checkCircular in filterDeep in case of childrenPath is specified and circular reference is not one of some children because of condensing.
Bu default filterDeep result is deeply condensed, and condenseDeep method does not support 'childrenPath' option, so it will go over all the properties.
So it's not a bug, but kind of inefficiency I will improve this in a separate task.

@muuvmuuv
Copy link
Author

Just for ur information I got it working by getting rid of the circular dependency which should not be there so thanks for giving me this hint.

This is my final solution:

      const newList: ZoneItem[] =
        filterDeep(
          this.zones[index].items,
          (value: ZoneItem): boolean => {
            return value.id !== itemId
          },
          {
            childrenPath: ['children'],
            onFalse: { skipChildren: true },
          }
        ) || []

@YuriGor
Copy link
Owner

YuriGor commented Jun 16, 2020

Yeah, looks good. If you can avoid circular refs - better to do it, because checking for circular references in filterDeep impacts performance.

@YuriGor YuriGor reopened this Mar 10, 2021
@YuriGor YuriGor closed this as completed Mar 10, 2021
Repository owner locked and limited conversation to collaborators Mar 10, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Further information is requested test Add potentially dangerous test cases
Projects
None yet
Development

No branches or pull requests

2 participants