2
0 Comments

Custom util to get weekday name or number

const getDayNameOrNo = (value) => {
  const mappings = [
    [0, "sunday"],
    [1, "monday"],
    [2, "tuesday"],
    [3, "wednesday"],
    [4, "thursday"],
    [5, "friday"],
    [6, "saturday"],
  ];

  const match = mappings.find(
    ([dayNo, name]) => dayNo === value || name === value
  );

  if (!match) return;

  const [matchedNo, matchedName] = match;
  return matchedNo === value ? matchedName : matchedNo;
};

console.log(getDayNameOrNo(0)); // sunday
console.log(getDayNameOrNo("sunday")); // 0
console.log(getDayNameOrNo("friday")); // 5
Trending on Indie Hackers
38% of remote employees work from bed. What about you? 14 comments Notion or Airtable for database and research based products? 10 comments Social media platforms are turning into discovery platforms 8 comments Twitter accounts directory 5 comments I started The Peak (Morning Brew for Canada) and sold $150,000 worth of ads in year 1. Here's how. 3 comments Launching a new feature: Video Reviews 2 comments