score: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 :)

score: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';
}

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

Related Query

More Query from same tag