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

intersectKeysWith + intersectKeys

Open Undistraction opened this issue 7 years ago • 6 comments

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.

Undistraction avatar Feb 14 '18 17:02 Undistraction

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

char0n avatar Feb 15 '18 20:02 char0n

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]

Undistraction avatar Feb 15 '18 20:02 Undistraction

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

char0n avatar Feb 15 '18 21:02 char0n

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

Does it make sence ?

char0n avatar Feb 15 '18 21:02 char0n

Makes sense: REPL

Undistraction avatar Feb 15 '18 21:02 Undistraction

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

wojpawlik avatar Feb 24 '18 20:02 wojpawlik