solodynamo / ng2-search-filter

Angular 2 / Angular 4 / Angular 5 custom pipe npm module to make a search filter on any input, 🔥 1Million+ downloads
https://www.npmjs.com/package/ng2-search-filter
MIT License
149 stars 40 forks source link

Added support to exclude the properties in the search #48

Closed prashantpimpale93 closed 5 years ago

prashantpimpale93 commented 5 years ago

Added support to exclude the properties while searching - #Fix-40

aVolpe commented 5 years ago

Since we now support filtering in nested objects, we need to be able to "exclude" the nested properties.

I think we can do this by passing a filtering the 'exclusionList' to the checkInside function and check there with ===.

prashantpimpale93 commented 5 years ago

@aVolpe Have added one more condition to the If block:

excludes.includes(property)

So if it returns true then it will ignore the current iteration.

Suggest If I missed anything!

EDIT: excludes is an array so have used includes

aVolpe commented 5 years ago

@prashantpimpale93 your test aren't passing in the latest version.

Failures:
1) Pipe: Default Ignore the property in the search
  Message:
    Expected $.length = 2 to equal 1.
    Expected $[1] = Object({ a: Object({ b: 'e', c: Object({ b: 'd' }) }) }) to equal undefined.
  Stack:
    Error: Expected $.length = 2 to equal 1.
    Expected $[1] = Object({ a: Object({ b: 'e', c: Object({ b: 'd' }) }) }) to equal undefined.
        at UserContext.<anonymous> (/Users/arturovolpe/develop/avolpe/ng2-search-filter/src/ng2-filter.pipe.spec.ts:38:50)

5 specs, 1 failure
Finished in 0.021 seconds

Changing to this in the pipe seems to fix it:

           if (checkInside(item[property], term)) {
             return true;
           }
-        }
-        if (item[property].toString().toLowerCase().includes(toCompare)) {
+        } else if (item[property].toString().toLowerCase().includes(toCompare)) {
           return true;
         }
prashantpimpale93 commented 5 years ago

@aVolpe okay will test it and commit again!

prashantpimpale93 commented 5 years ago

@aVolpe Updated the code and committed!

Have a look!

prashantpimpale93 commented 5 years ago

@aVolpe Did you review the code changes?

aVolpe commented 5 years ago

I think the changes are a first aproach to the issue, maybe in a future we can add more complex filters like:

"user.name", // to only filter  the name of the user
"user.ci*"
prashantpimpale93 commented 5 years ago

@aVolpe Thanks a lot!