-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Support key exclusions #9
Comments
Makes sense 👍🏻 |
@Richienb this feature is available through below function being passed as predicate, also has a flexibility for keys as well as values:
|
@faizanu94 This will be used for sindresorhus/query-string#282 (comment) |
I would say then exclusions should be available for values as well, @sindresorhus your thoughts on this? |
@faizanu94 I don't think that is as common of a need. If someone needs that, they can propose it then or just use the filter. I'm mostly just interested in the API sugar for keys. |
Please correct if I'm wrong, but I believe, since we are using string matching and not globbing patterns, it would not make sense to mix excluded keys with included keys, such as: filterObject({a: 1, b: 1, c: 1}, ['!a', 'b']); Since it would have the same behavior as passing only Based on this, if a user wants to exclude keys, they would probably want to prepend filterObject({a: 1, b: 1, c: 1}, ['!a', '!b', '!c', '!d', '!e', '!f']); Also, behavior might be ambiguous when both including and excluding the same key. Either should have priority, which would need to be documented and might create some confusion among some users. filterObject({a: 1, b: 1, c: 1}, ['!a', 'a']); I am wondering whether it would be simpler instead to export a separate function negating the predicate? It would also be more performant since the logic would need to do fewer argument checks and could be optimized for each case. Finally, it would also work with predicate functions. import { include, exclude } from 'filter-obj';
include({a: 1, b: 2, c: 3}, ['a']); // {a: 1}
exclude({a: 1, b: 2, c: 3}, ['a']); // {b: 2, c: 3}
include({a: 1, b: 2, c: 3}, (key, value) => value === 1); // {a: 1}
exclude({a: 1, b: 2, c: 3}, (key, value) => value === 1); // {b: 2, c: 3} |
Yes, that is a problem I immediately stumbled upon as soon as I began to think of how to implement it. If you're suggesting to create separate
|
I was actually going to suggest this too! Meaning: breaking it into three methods instead: two with array of keys (with/without negation) and one with the function predicate. When it comes to the names, I like them too, with two additional questions:
|
The
Sure, though I'm not sure whether the names need to conform with other Sindre modules. @sindresorhus What do you think? |
I prefer |
…eKeys()` Fixes sindresorhus#9 Signed-off-by: Richie Bendall <richiebendall@gmail.com>
👍 To summarize it, this would be: import { filterObject, includeKeys, excludeKeys } from 'filter-obj';
filterObject({a: 1, b: 2, c: 3}, (key, value) => value === 1); // {a: 1}
filterObject({a: 1, b: 2, c: 3}, ['a']); // Error. This is a breaking change.
includeKeys({a: 1, b: 2, c: 3}, ['a']); // {a: 1}
excludeKeys({a: 1, b: 2, c: 3}, ['a']); // {b: 2, c: 3} Is this correct? |
I merged import {includeKeys, excludeKeys} from 'filter-obj';
const object = {
foo: true,
bar: false
};
const newObject = includeKeys(object, (key, value) => value === true);
//=> {foo: true}
const newObject2 = includeKeys(object, ['bar']);
//=> {bar: false}
const newObject = excludeKeys(object, (key, value) => value === true);
//=> {bar: false}
const newObject3 = excludeKeys(object, ['bar']);
//=> {foo: true} |
This works. 👍 This would also be close to this stage 1 ES proposal for @sindresorhus What are your thoughts? |
Thanks @Richienb for adding this feature! ❤️ |
Something like:
Source: sindresorhus/query-string#282 (comment)
The text was updated successfully, but these errors were encountered: