BorisTB
BorisTB

Reputation: 1806

Ramda filter - number contains number

I'm trying to make filter with ramda. Everything works fine except for filtering numbers by user input. It works only with strings. How can I tell ramda to convert number value to string when filtering?

const data = [
  {
    col: 123,
    foo: 'sfdfds'
  },
  {
    col: 624,
    foo: 'dsfsdd'
  },
  {
    col: '98523',
    foo: 'sdfsdf'
  }
];

const searchedValue = 23;

const filteredData = R.filter(R.where({ col: R.contains(searchedValue) }))(data)

console.log(filteredData)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>

Full code here: https://jsfiddle.net/1qwnem2u/1/

Upvotes: 0

Views: 729

Answers (1)

Scott Sauyet
Scott Sauyet

Reputation: 50807

Ramda's contains is not meant to work cross-types like this. But you can easily add your own:

const data = [
  {col: 123, foo: 'sfdfds'},
  {col: 624, foo: 'dsfsdd'},
  {col: '98523', foo: 'sdfsdf'}
];

const searchedValue = 23;

const stringContains = R.curry(
  (needle, haystack) =>  R.contains(String(needle), String(haystack))
)

const filteredData = R.filter(R.where(
  { col: stringContains(searchedValue) }
))(data)

console.log(filteredData)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>

Upvotes: 1

Related Questions