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;
    }, {})
);
        
2

3 Answers 3

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;
}, {});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Can you also help with second function
@ZeusCarl This is directly translated from your code, not sure what you're trying to do and why your lint is complaining.
At second function error is Assignment to property of function parameter 'carry'
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';
}

2 Comments

Can you help with that another function?
@ZeusCarl Stack Overflow rules are such that you can only ask one question per post. Please make another post for the second question.
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 :)

5 Comments

Can you help me with another function? below that one?
Assignment to property of function parameter 'carry'. This eeror function
please elaborate on your question. what do you want and specify those variables
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.
I have updated the question with example of usage of that function. Can you help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.