char0n / ramda-adjunct

Ramda Adjunct is the most popular and most comprehensive set of functional utilities for use with Ramda, providing a variety of useful, well tested functions with excellent documentation.
https://char0n.github.io/ramda-adjunct/
BSD 3-Clause "New" or "Revised" License
682 stars 86 forks source link

intersectKeysWith + intersectKeys #366

Open Undistraction opened 6 years ago

Undistraction commented 6 years ago

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']

REPL

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.

char0n commented 6 years ago

http://ramdajs.com/docs/#pick, http://ramdajs.com/docs/#pickAll or http://ramdajs.com/docs/#pickBy doesn't satisfy your usecase ?

Undistraction commented 6 years ago

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]

char0n commented 6 years ago

Ahh I get it. You want intersection of the list of the possible keys and the real ones.

char0n commented 6 years ago

intersectKeysWith = curry(fn, keys, obj) => ...); IntersectKeys = intersectKeysWith(has);

Does it make sence ?

Undistraction commented 6 years ago

Makes sense: REPL

wojpawlik commented 6 years ago

Sounds to me like R.difference(oKeys, R.keys(o), or, as a function, R.useWith(R.difference, [R.identity, R.keys])