Javascript Ramda how to make R.find(R.propEq()) case insensitive?

Viewed 2061

Is there any way to make R.find(R.propEq()) case insensitive for an Object Tree? (Currently I am using the Ramda Libraries)

This is a piece of my obj tree:

  const objectTree = [ { __type: 'ix:ChecklistGridSection',
  For: 'QuestionAnswers',
  childNodes:
   [ { __type: 'Sorting', childNodes: [Array] },
     { __type: 'grouping', childNodes: [Array] },
     { __type: 'Tabs', childNodes: [Array] }, 
  ...

I have a function like this one that I can't change the parameters:

R.find(R.propEq('__type', 'ix:checklistgridsection'))(objectTree);

It will work only with 'ix:CheckListGridSection' as parameter and not with 'ix:checklistgridsection'. I need that it will work for also the others leafs of the tree.

I think that is a bad decision to make all tree lowercase. So I was thinking if there is any way to make R.find(R.propEq( )) case insensitive.

2 Answers

R.propEq equates the prop value with the passed value that is why used R.test.

So, You can use R.propSatisfies with R.test

R.find(R.propSatisfies(x => R.test(new RegExp('ix:checklistgridsection','i'), x), '__type'), objectTree)

You could use propSatisfies instead of propEq like this:

const eqInsensitive = R.curry(
  (a, b) => String(a).toLowerCase() === String(b).toLowerCase()
)

R.find(
  R.propSatisfies(eqInsensitive('ix:checklistgridsection'), '__type'), 
  objectTree
)

You can see this in action on the Ramda REPL.

That does not address this:

I need that it will work for also the others leafs of the tree.

I'm not sure what you want here. Ramda will not automatically walk your tree structure trying to find some child element that matches the predicate. You would need to build up that infrastructure yourself. But this same find should work against any level of the tree.

Related