Skip to content

Instantly share code, notes, and snippets.

@alex-cory
Last active November 9, 2023 16:23
Show Gist options
  • Save alex-cory/b807a07dee2c4c410435783a94213ec6 to your computer and use it in GitHub Desktop.
Save alex-cory/b807a07dee2c4c410435783a94213ec6 to your computer and use it in GitHub Desktop.
const toEmoji = (num) => {
if (num === null || num === undefined || Number.isNaN(num)) return '⛔';
if (num === 10) return '🔟';
if (!isFinite(num)) return '🔁';
return String(num)
.replace('-', '−')
.replace('.', '⏺️')
.replace(/\d/g, digit => `${digit}️⃣`)
.replace(/⏺️(\d{0,2})\d*/, (match, p1) => `⏺️${p1}`);
};
// Examples
// console.log(toEmoji(-1)); // '−1️⃣'
// console.log(toEmoji(Infinity)); // '🔁'
// console.log(toEmoji(10)); // '🔟'
// console.log(toEmoji(0)); // '0️⃣'
// console.log(toEmoji(3.14159)); // '3️⃣⏺️14'
// console.log(toEmoji(NaN)); // '⛔'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment