ramda-adjunct
ramda-adjunct copied to clipboard
filterKeys
Is your feature request related to a problem? Please describe.
Filter keys of the object by evaluating the value.
Describe the solution you'd like
const o = {
a: 'alpha',
b: 'bravo',
c: 'alpha',
d: 'charlie',
e: 'delta',
f: 'alpha'
}
const reduceObjIndexed = curry((reducer, acc, value) =>
compose(reduce(reducer, acc), toPairs)(value)
)
const filterKeys = curry((predicate, obj) => reduceObjIndexed((keys, [key, value]) =>
when( () => predicate(value), append(key))(keys), [], obj)
)
filterKeys(equals('alpha'), o) //=> ['a', 'c', 'f]
filterKeys(equals('delta'), o) //=> ['e']
filterKeys(equals('foxtrot'), o) //=> []
Describe alternatives you've considered
--
Additional context
rejectKeys
could be implemented also. @guillaumearm has a good point of evaling also the key not just a value. Functions like filterKeysWith
or rejectsKeysWith
can be created to specialize the behavior.
I like the idea that user can filter keys using values, but I think filtering keys by keys have sense too.
What about an arity 2 predicate ? predicate(value, key)
Anyway, this issue should definitely had the feature
label.