Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
How to pass multiple parameters to R.cond ramda function?

Here is the ramda function that accept a string parameter

const decodeBarcode => R.cond([
  [x => R.equals(x.length, 12), decodeUPACBarcode],
  [x => R.equals(x.length, 13), decode13DigitBarcode],
  [x => R.gt(x.length, 20), decodeLengthGT20Barcode],
  [R.T, R.identity],
]);

How to pass another parameter to improve condition?

another parameter barcodeType to be included

if (barcode.length > 20 or barcodeType = "DATAMATRIX") 

Tried the below, not working !!

 const decodeBarcode = (barcode, barcodeType) => R.cond([
      [x => R.equals(x.length, 12), decodeUPACBarcode],
      [x => R.equals(x.length, 13), decode13DigitBarcode],
      [x => R.or(R.propEq('DATAMATRIX', barcodeType), R.gt(x.length, 20)), decodeLengthGT20Barcode],
      [R.T, R.identity],
    ])(barcode);

RamdaJS REPL

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use R.anyPass to run a list of predicates against a value, and return true is any of the predicate returns true:

const { cond, propEq, anyPass, propSatisfies, lte, T, identity } = R

const decodeLength13Barcode = () => console.log('Its 13 digit barcode')
const decodeLengthGT20Barcode = () => console.log('Its > 20 digit barcode')

const decodeBarcode = (barcode, barcodeType) => cond([
  [propEq('length', 13), decodeLength13Barcode],
  [anyPass([
    propEq('DATAMATRIX', barcodeType),
    propSatisfies(lte(20), 'length')
  ]), decodeLengthGT20Barcode],
  [T, identity],
])(barcode);

console.log(decodeBarcode('0272889010009'));
console.log(decodeBarcode('02728890100099898989'));
console.log(decodeBarcode({ DATAMATRIX: '20d' }, '20d'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js" integrity="sha512-t0vPcE8ynwIFovsylwUuLPIbdhDj6fav2prN9fEu/VYBupsmrmk9x43Hvnt+Mgn2h5YPSJOk7PMo9zIeGedD1A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

Since R.cond passes all parameters to the predicates, you can remove the arrow function (point-free). However, you pass bardcode, barcodeType, while R. propEq expects barcodeType, barcode, so you'll need to flip it after stating the property (DATAMATRIX):

const { cond, propEq, anyPass, flip, propSatisfies, lte, T, identity } = R

const decodeLength13Barcode = () => console.log('Its 13 digit barcode')
const decodeLengthGT20Barcode = () => console.log('Its > 20 digit barcode')

const decodeBarcode = cond([
  [propEq('length', 13), decodeLength13Barcode],
  [anyPass([
    flip(propEq('DATAMATRIX')),
    propSatisfies(lte(20), 'length')
  ]), decodeLengthGT20Barcode],
  [T, identity],
]);

console.log(decodeBarcode('0272889010009'));
console.log(decodeBarcode('02728890100099898989'));
console.log(decodeBarcode({ DATAMATRIX: '20d' }, '20d'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js" integrity="sha512-t0vPcE8ynwIFovsylwUuLPIbdhDj6fav2prN9fEu/VYBupsmrmk9x43Hvnt+Mgn2h5YPSJOk7PMo9zIeGedD1A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

about 4 years ago · Juan Pablo Isaza Report

0

If I understand correctly, you want something like this:

const decodeBarcode = cond ([
  [propEq ('length', 12), decodeUPACBarcode],
  [propEq ('length', 13), decode13DigitBarcode],
  [(barcode, barcodeType) => barcode.length > 20 || barcodeType == "DATAMATRIX" , decodeLengthGT20Barcode],
  [T, identity],
]);

const testCases = [
  ['123456789012', 'TYPEA'], 
  ['1234567890123'], 
  ['123456789012345678901'],
  ['12345678901234567', 'DATAMATRIX'],
  ['12345678901324567']
]

testCases .forEach (xs => console .log (
  `decodeBarcode ('${xs .join(`', '`)}')\n//==> ${decodeBarcode (...xs)}`
))
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js"></script>
<script> 
const {cond, propEq, T, identity} = R
const decodeUPACBarcode = (barcode, type = '') => `calling decodeUPACBarcode('${barcode}', '${type}')`
const decode13DigitBarcode = (barcode, type = '') => `calling decode13DigitBarcode('${barcode}', '${type}')`
const decodeLengthGT20Barcode = (barcode, type = '') => `calling decodeLengthGT20Barcode('${barcode}', '${type}')`
</script>

While we always have a way to make a function point-free, and it was worth doing for the first two conditions, the third one is going to be ugly enough that I wouldn't do it except as academic exercise. It might end up being something like this:

pipe (
  unapply (zipWith (call) ([propSatisfies (gt (__, 20), 'length'), equals ('DATAMATRIX')])),
  apply (or)
)

and that is much less readable than the arrow function currently used.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!