1

While developing I am having lint error for this function

Do not nest ternary expressions.eslintno-nested-ternary

How can I convert the below funciton into switch case? Or any other such that I solve the es lint error?

getDaySuffix = day => (day === (1 || 21 || 31) ? 'st' : day === (2 || 22) ? 'nd' : day === (3 || 23) ? 'rd' : 'th');

Also Can anyone help me with this function too,in below function I am getting an error of

Assignment to property of function parameter 'carry'.

How can I solve this above error with below function? How can I re-write the function without changing the function logic? And solve the eslint error too.

const filter = selectedFilter[0].conditions.reduce(
  (carry, current) => {
    if (current.field === 'access_group_uuid') {
      // eslint-disable-next-line in next line getting error at carry
      carry[current.field] = (carry[current.field] || []).concat(
        current.value,
      );
    } else {
      // eslint-disable-next-line in next line getting error at carry
      carry[current.field] = carry[current.field] ?
        [carry[current.field], current.value] :
        current.value;
    }
    return carry;
  }, {},
);

Edit -- For second function with example

const data = { 
    executed:[
    {_id: "5f23d394cd 480e300", field: "name", value: "Jolly", operator: "equal"},
    {_id: "5f30d39f4cd8d0e301", field: "status", value: "EXPIRED", operator: "equal"},
    {_id: "5f230d39001480e302", field: "grp", value: "874-3-11-4-56", operator: "equal"},
    {_id: "59f4cd8d001480e303", field: "grp", value: "873-5-12-4-77", operator: "equal"}
    ],
    created_at: "2020-07-30T18:11:05.992Z",
    name: "Kind Find",
    _id: "1f230d39f4cd8d441480e2dd"
}

console.log(
    data.executed.reduce((carry, current) => {
        if (current.field === 'grp') {
            // eslint-disable-next-line no-param-reassig
            carry[current.field] = (carry[current.field] || []).concat(current.value);
        } else {
            // eslint-disable-next-line no-param-reassig
            carry[current.field] = carry[current.field] ? [carry[current.field], current.value] : current.value;
        }
        return carry;
    }, {})
);
        
Zeus Carl
  • 139
  • 4
  • 12
  • 2
    btw, `day === (1 || 21 || 31)` is equivalent to `day === 1`, it doesn't check if day is equal to 1, 21 or 31 – Nick Parsons Sep 23 '20 at 05:03
  • Does this answer your question? [Javascript: The prettiest way to compare one value against multiple values](https://stackoverflow.com/questions/9121395/javascript-the-prettiest-way-to-compare-one-value-against-multiple-values) – Sebastian Simon Sep 24 '20 at 08:42

3 Answers3

2

Instead of switch, you can use object lookups:

const getSuffix = day => {
  const map = {
    '1': 'st',
    '21': 'st',
    '31': 'st',
    '2': 'nd',
    '22': 'nd',
    '3': 'rd',
    '23': 'rd'
  };

  return map[day] || 'th';
}

But if you are looking for ordinal indicator conversion, you can use Intl.PluralRules to achieve that:

const pr = new Intl.PluralRules('en-US', { type: 'ordinal' });
const map = {
  other: 'th',
  one: 'st',
  two: 'nd',
  few: 'rd',
};

const ordinal = num => num + map[pr.select(num)];


console.log(ordinal(1));
console.log(ordinal(2));
console.log(ordinal(3));
console.log(ordinal(13));
console.log(ordinal(24));
console.log(ordinal(42));

For the second function, you can convert the ternary expressions into if else:

const filter = selectedFilter[0].conditions.reduce((carry, current) => {
  if (current.field === 'access_group_uuid') {
    // eslint-disable-next-line in next line getting error at carry
    if (!carry[current.field]) {
      carry[current.field] = [];
    }
    carry[current.field] = carry[current.field].concat(current.value);
  } else {

    // eslint-disable-next-line in next line getting error at carry
    if (carry[current.field]) {
      carry[current.field] = [carry[current.field], current.value];
    } else {
      carry[current.field] = current.value;
    }
  }
  return carry;
}, {});
Hao Wu
  • 17,573
  • 6
  • 28
  • 60
1

Here is the simple switch case solution:

switch (day) {
    case 1: case 21: case 31:
        getDaySuffix = 'st';
      break;
    case 2: case 22:
        getDaySuffix = 'nd';
      break;
    case 3: case 23:
        getDaySuffix = 'rd';
    break;
    default:
        getDaySuffix = 'th';
}
kavigun
  • 2,219
  • 2
  • 14
  • 33
0

I think it's more efficient to have your own function which does the same work.

const nth = function(val) {
  var d = +val.toString().split('').pop();
  if (d > 3 && d < 21) return 'th';
  switch (d % 10) {
    case 1:  return "st";
    case 2:  return "nd";
    case 3:  return "rd";
    default: return "th";
  }
}

var day = 23;

console.log(`${day}${nth(day)}`);

Here the nth function takes the day as an argument and returns the day suffix as a string. So you can use it more flexibly. Just simply pass the day into it and get your suffix as a string.

Happy Coding :)

auro
  • 49
  • 1
  • 5
  • Can you help me with another function? below that one? – Zeus Carl Sep 23 '20 at 06:27
  • `Assignment to property of function parameter 'carry'.` This eeror function – Zeus Carl Sep 23 '20 at 06:29
  • please elaborate on your question. what do you want and specify those variables – auro Sep 23 '20 at 06:57
  • You are trying to assign some value to carry which is not applicable as it is the function parameter. You only can use its value but can't assign anything to it. Check the usage of reduce() . Find some other way to solve that or explain your question then only we guys can help you. – auro Sep 23 '20 at 07:12
  • I have updated the question with example of usage of that function. Can you help? – Zeus Carl Sep 23 '20 at 07:17