ramda-adjunct icon indicating copy to clipboard operation
ramda-adjunct copied to clipboard

filterKeys

Open Undistraction opened this issue 6 years ago • 1 comments

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.

REPL

Undistraction avatar Apr 15 '18 16:04 Undistraction

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 featurelabel.

guillaumearm avatar Apr 22 '18 15:04 guillaumearm