ael
ael

Reputation: 43

Ramda 'when' not producing expected result

I'm expecting arrayTester to look at each array within the array and if both elements are empty strings for them to be removed from the array. Instead I am getting back the original array.

Expected result = [[], ['string', '']]

Actual result = [['',''], ['string', '']]

Any ideas?

let data = [['',''], ['string', '']]
const emptyString = R.equals('')
const removeEmptyElementsFunc = R.reject(R.isEmpty)
const arrayTester = (data) => {
 R.forEach(
        R.when(
          R.all(emptyString),
          R.map(removeEmptyElementsFunc)
        )(data))
        return data
      }

arrayTester(data)

Upvotes: 0

Views: 75

Answers (1)

customcommander
customcommander

Reputation: 18961

If you need to map an array of empty strings to an empty array, the only thing you need to do is to make sure that it is indeed only made of empty strings. (The "transformation" is rather easy: just return an empty array.)

all(isEmpty) will return true if an array is made of empty stuff. always([]) will always return an empty array.

const arrayTester = map(when(all(isEmpty), always([])));

console.log(

  arrayTester([['', ''], ['a', 'b', '']]),
  arrayTester([['', '', ''], ['a', 'b', '']]),
  arrayTester([['', 'x', ''], ['a', 'b', '']])
  
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>
<script>const {map, when, all, isEmpty, always} = R;</script>

Upvotes: 1

Related Questions