JavaScript Ramda: whereAny?

I'm using Ramda as my functional programming helper library to build a React app.

I'm trying to build myself whereAny. Ramda exposes where which checks if every prop it is given satisfies the respective predicate.

I want to build a function that accepts an array of keys, an object and a search term and if any of the keys (which values are strings) includes the search term it should return true. If not it should return false.

Here is my current workaround:

export const whereAny = keys => obj => searchTerm =>
  R.any(
    R.pipe(
      R.toLower,
      R.includes(R.toLower(searchTerm))
    ),
    R.values(R.pick(keys, obj))
  );
export const contactNameIncludesSearchValue = whereAny(['firstName', 'lastName']);

As you can see I use it to see if some values of a contact include the search term (IRL I use more than just firstName and lastName).

My question is how can you build such a whereAny function? I looked into the Ramda cookbook and on Google and couldn't find a recipe for whereAny.

The problem with my workaround (apart from the fact that it's probably not optimal) is that you can't specify different functions just like in where. So ideally my api would look like this:

const pred = searchValue => R.whereAny({
  a: R.includes(searchValue),
  b: R.equals(searchValue),
  x: R.gt(R.__, 3),
  y: R.lt(R.__, 3)
});
Answer:1

I think you should curry your functions using Ramda's curry.

Why?

With this form of currying, you have to call it this way and this way only:

const abc = a => b => c => a + b + c;
abc(1)(2)(3); // 6

Whereas with this form you are more flexible:

const abc = curry((a, b, c) => a + b + c);
abc(1, 2, 3); // 6
abc(1, 2)(3); // 6
abc(1)(2)(3); // 6

Also note that Ramda functions tend to accept the data it operates on as the last parameter:

Ramda functions are automatically curried. This allows you to easily build up new functions from old ones simply by not supplying the final parameters.

The parameters to Ramda functions are arranged to make it convenient for currying. The data to be operated on is generally supplied last.

The last two points together make it very easy to build functions as sequences of simpler functions, each of which transforms the data and passes it along to the next. Ramda is designed to support this style of coding.

Cf project homepage

I think we can still use this approach to solve your problem:

const whereAny = curry((keys, search, obj) =>
  compose(includes(search), props(keys))
    (obj));
    
const whereNameIsFoo = whereAny(['firstName', 'lastName'], 'foo');

console.log(whereNameIsFoo({firstName: 'john', lastName: 'foo'}));
console.log(whereNameIsFoo({firstName: 'bar', lastName: 'baz'}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>
<script>const {curry, compose, flip, includes, props} = R;</script>
Answer:2

I have a form on a PHP page : <form name="SendEmail01" method="post"> form items here... </form> I then process it like this: if(isset($_POST["Submit"])) { All tasks here... $...

I have a form on a PHP page : <form name="SendEmail01" method="post"> form items here... </form> I then process it like this: if(isset($_POST["Submit"])) { All tasks here... $...

the problem is simple, document.getElementsByTagName('*') does not select the SVG tag and in the console it gives an error. But if I erase the SVG labels, works correctly. My Code: var Master = ...

the problem is simple, document.getElementsByTagName('*') does not select the SVG tag and in the console it gives an error. But if I erase the SVG labels, works correctly. My Code: var Master = ...

  1. document getelementsbytagname java
  2. document getelementsbytagname innerhtml
  3. document getelementsbytagname w3schools
  4. document getelementsbytagname multiple tags
  5. document getelementsbytagname radio button
  6. document.getelementsbytagname('video') 0 .request picture in picture()
  7. document.getelementsbytagname(name)
  8. document.getelementsbytagname is not a function
  9. document.getelementsbytagname('body')
  10. document.getelementsbytagnamens
  11. document.getelementsbytagname(...).addeventlistener is not a function
  12. document.getelementsbytagname( input )
  13. document.getelementsbytagname foreach
  14. document.getelementsbytagname(...).appendchild is not a function
  15. document.getelementsbytagname( body ) 0
  16. document.getelementsbytagname('head') 0 .appendchild(script)
  17. document.getelementsbytagname jquery
  18. document.getelementsbytagname js
  19. document.getelementsbytagname('base') 0 .href
  20. document.getelementsbytagname('script') 0

There is a function that returns an array according to the second argument (if the country is Japan, it will return only brands of cars from Japan). Is it possible to improve the function using only ...

There is a function that returns an array according to the second argument (if the country is Japan, it will return only brands of cars from Japan). Is it possible to improve the function using only ...

  1. method filter javascript
  2. method filter js
  3. method filter does not exist
  4. method filter java
  5. method filter python
  6. method filter aris
  7. method filter scala
  8. method filter array
  9. method filter swift
  10. method filter in r
  11. filter method feature selection
  12. filter method of ssb generation
  13. filter method in java 8
  14. filter method in typescript
  15. filter method in jquery
  16. filter method in angular 6
  17. filter method in react
  18. filter method in angular
  19. filter method in react js
  20. filter method array javascript

I am working on ListControl and one of the columns has Delete link which I am formatting using HTMLTemplate as follows: <a href="javascript: app.showConfirmation()" >Delete</a> My ...

I am working on ListControl and one of the columns has Delete link which I am formatting using HTMLTemplate as follows: <a href="javascript: app.showConfirmation()" >Delete</a> My ...