ramda-adjunct
ramda-adjunct copied to clipboard
intersectKeysWith + intersectKeys
Is your feature request related to a problem? Please describe.
Intersect object keys with the list of provided key names.
Describe the solution you'd like
const intersectKeysWith = curry((fn, keys, o) => compose(filter(flip(fn)(o)))(keys))
const intersectKeys = intersectKeysWith(has)
const o = {
a: 1,
b: 2,
c: 3,
}
const oKeys = [
'a', 'c', 'd', 'e', 'f'
]
intersectKeys(oKeys, o) // ["a", "c"]
Describe alternatives you've considered
--
Additional context
Original issue description: filterKeys :: [String] => Object => [String]
Ramda currently offers props, but there is no equivalent for keys, so how about keysOf
:
const filterKeys = curry((keys, o) => compose(filter(flip(has)(o)))(keys))
const o = {
a: 1,
b: 2,
c: 3,
}
const oKeys = [
'a', 'c', 'd', 'e', 'f'
]
filterKeys(oKeys, o) // ['a', 'c']
Note: Ramda's props returns undefined
values for any values that aren't matched, but I don't think that makes sense here as these are keys, not the values.
Usecase: I need to make sure an object doesn't include more than one of a set of exclusive keys.
http://ramdajs.com/docs/#pick, http://ramdajs.com/docs/#pickAll or http://ramdajs.com/docs/#pickBy doesn't satisfy your usecase ?
http://ramdajs.com/docs/#pick, http://ramdajs.com/docs/#pickAll or http://ramdajs.com/docs/#pickBy doesn't satisfy your usecase ?
No. They all have a very different intent - they are focused on copying key value pairs from an object to a new object - Object -> ... -> Object
. The function I'm talking about uses an object to filter a list of keys [String] -> ... -> [String]
Ahh I get it. You want intersection of the list of the possible keys and the real ones.
intersectKeysWith = curry(fn, keys, obj) => ...); IntersectKeys = intersectKeysWith(has);
Does it make sence ?
Makes sense: REPL
Sounds to me like R.difference(oKeys, R.keys(o)
, or, as a function, R.useWith(R.difference, [R.identity, R.keys])