| 1 | import assert from 'node:assert/strict'; |
| 2 | import { test } from 'node:test'; |
| 3 | import { isJumbo, segmentEmoji, twemojiUrl } from '../src/lib/twemoji.ts'; |
| 4 | |
| 5 | const emojiOf = (text: string) => segmentEmoji(text).filter((s) => s.emoji).map((s) => s.text); |
| 6 | |
| 7 | test('plain text takes the fast path', () => { |
| 8 | assert.deepEqual(segmentEmoji('hello world 123 #1 *'), [{ text: 'hello world 123 #1 *', emoji: false }]); |
| 9 | }); |
| 10 | |
| 11 | test('splits text and emoji', () => { |
| 12 | assert.deepEqual(segmentEmoji('hi 🎉 there'), [ |
| 13 | { text: 'hi ', emoji: false }, |
| 14 | { text: '🎉', emoji: true }, |
| 15 | { text: ' there', emoji: false } |
| 16 | ]); |
| 17 | }); |
| 18 | |
| 19 | test('keeps ZWJ sequences whole', () => { |
| 20 | assert.deepEqual(emojiOf('a 👩💻 b 👨👩👧👦'), ['👩💻', '👨👩👧👦']); |
| 21 | }); |
| 22 | |
| 23 | test('keeps skin tones whole', () => { |
| 24 | assert.deepEqual(emojiOf('👍🏽'), ['👍🏽']); |
| 25 | }); |
| 26 | |
| 27 | test('treats a regional indicator pair as one flag', () => { |
| 28 | assert.deepEqual(emojiOf('🇳🇿 and 🇯🇵'), ['🇳🇿', '🇯🇵']); |
| 29 | }); |
| 30 | |
| 31 | test('keycaps need FE0F and 20E3', () => { |
| 32 | assert.deepEqual(emojiOf('1️⃣ #️⃣'), ['1️⃣', '#️⃣']); |
| 33 | assert.deepEqual(emojiOf('1 # *'), []); |
| 34 | }); |
| 35 | |
| 36 | test('text presentation symbols stay text without FE0F', () => { |
| 37 | assert.deepEqual(emojiOf('© ® ☺'), []); |
| 38 | assert.deepEqual(emojiOf('☺️'), ['☺️']); |
| 39 | }); |
| 40 | |
| 41 | test('jumbo only for one to three emoji', () => { |
| 42 | assert.equal(isJumbo(segmentEmoji('🎉')), true); |
| 43 | assert.equal(isJumbo(segmentEmoji('🎉 🎉🎉')), true); |
| 44 | assert.equal(isJumbo(segmentEmoji('🎉🎉🎉🎉')), false); |
| 45 | assert.equal(isJumbo(segmentEmoji('hi 🎉')), false); |
| 46 | assert.equal(isJumbo(segmentEmoji('hello')), false); |
| 47 | }); |
| 48 | |
| 49 | test('twemojiUrl follows the filename rule', () => { |
| 50 | assert.equal(twemojiUrl('🎉'), '/twemoji/1f389.svg'); |
| 51 | assert.equal(twemojiUrl('❤️'), '/twemoji/2764.svg'); |
| 52 | assert.equal(twemojiUrl('👨⚕️'), '/twemoji/1f468-200d-2695-fe0f.svg'); |
| 53 | assert.equal(twemojiUrl('1️⃣'), '/twemoji/31-20e3.svg'); |
| 54 | }); |