benwinding / ngx-filemanager

File manager for Angular
https://benwinding.github.io/ngx-filemanager/
MIT License
9 stars 9 forks source link

Avoid use push permission to array #16

Closed congweibai closed 3 years ago

congweibai commented 3 years ago

Hi Ben, Currently, user is able to add permission to permission array with same id, so I add a filter to avoid users push the permission with same permission id.

Hopefully, this makes sense to you.

Thank you and have a nice day!

Wyatt

permission-id
benwinding commented 3 years ago

Hi @congweibai,

Thanks for that! Will merge it in, in the future you can make it even simpler though:

Instead of this:

  let canPush = true;
  this.control.value.forEach((item)=>{
    canPush = (item.id !== newVal.id) && canPush
  })
  if(canPush)
    this.control.value.push(newVal);

You could use Array method .every():

  let canPush = this.control.value.every(item => item.id != newVal.id)
  if(canPush)
    this.control.value.push(newVal);

Will merge it in now though. Cheers, Ben

congweibai commented 3 years ago

Thanks, Ben. That makes sense to me and I did little research on native array methods. I think this could work as well.

 let canNotPush = this.control.value.some(item => item.id != newVal.id)
 if(!canNotPush)
    this.control.value.push(newVal)