Skip to content

Commit

Permalink
fix: wrapEmoji fixes and improvements (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldps committed May 3, 2024
1 parent 6e260fa commit aca1750
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 37 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ts-check

import { html, wrapEmoji } from './src/html.js'
import { html } from './src/html.js'
import { strip } from './src/strip.js'
import { wrapEmoji } from './src/wrapEmoji.js'

export { html, strip, wrapEmoji }
16 changes: 11 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
},
"homepage": "https://github.com/webmuds/colors#readme",
"dependencies": {
"escape-html": "1.0.3"
"escape-html": "1.0.3",
"emoji-regex": "10.3.0"
},
"devDependencies": {
"@dimensionalpocket/development": "github:dimensionalpocket/development-js#1.3.0"
Expand Down
11 changes: 0 additions & 11 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,3 @@ export const COLOR_RX = new RegExp('({((?:' + COLORS + '))}((?:(?!{(' + COLORS +
* RegExp to strip all color tags.
*/
export const STRIP_RX = new RegExp('{(?:' + COLORS + '|/)}', 'gim')

/**
* RegExp to wrap all emoji in <span> tags.
*/
export const EMOJI_RX = /([\p{Emoji_Presentation}]+)/gu

/**
* Template to use when replacing emoji.
* (wmE = WebMUDs Emoji)
*/
export const EMOJI_REPLACEMENT_TEMPLATE = '<span class="wmE">$1</span>'
12 changes: 1 addition & 11 deletions src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

'use strict'

import { COLOR_RX, HTML_REPLACEMENT_TEMPLATE, EMOJI_RX, EMOJI_REPLACEMENT_TEMPLATE } from './constants.js'
import { COLOR_RX, HTML_REPLACEMENT_TEMPLATE } from './constants.js'
import escapeHtml from 'escape-html'

/**
Expand All @@ -21,13 +21,3 @@ export function html (text, escape = true) {
}
return escapedText.replace(COLOR_RX, HTML_REPLACEMENT_TEMPLATE)
}

/**
* Wraps all emoji in a custom <span> tag.
*
* @param {string} text
* @return {string}
*/
export function wrapEmoji (text) {
return text.replace(EMOJI_RX, EMOJI_REPLACEMENT_TEMPLATE)
}
39 changes: 39 additions & 0 deletions src/wrapEmoji.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// @ts-check

'use strict'

import emojiRegex from 'emoji-regex'

/**
* RegExp to wrap all emoji in <span> tags.
*/
const EMOJI_RX = emojiRegexPattern()

/**
* Template to use when replacing emoji.
* (wmE = WebMUDs Emoji)
*/
const EMOJI_REPLACEMENT_TEMPLATE = '<span class="wmE">$1</span>'

/**
* Wraps all emoji in a custom <span> tag.
*
* @param {string} text
* @return {string}
*/
export function wrapEmoji (text) {
return text.replace(EMOJI_RX, EMOJI_REPLACEMENT_TEMPLATE)
}

/**
* Helper that extracts the RegExp from emoji-regex
* and returns a new RegExp with capture enabled.
*/
function emojiRegexPattern () {
const patternFromLib = emojiRegex().toString()
const lastSlashIndex = patternFromLib.lastIndexOf('/')
const pattern = patternFromLib.slice(1, lastSlashIndex)
const flags = patternFromLib.slice(lastSlashIndex + 1)

return new RegExp(`((?:${pattern})+)`, flags)
}
3 changes: 2 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import { expect } from '@dimensionalpocket/development'
import { html, strip, wrapEmoji } from '../index.js'
import { html as htmlFromSrc, wrapEmoji as wrapEmojiFromSrc } from '../src/html.js'
import { html as htmlFromSrc } from '../src/html.js'
import { wrapEmoji as wrapEmojiFromSrc } from '../src/wrapEmoji.js'
import { strip as stripFromSrc } from '../src/strip.js'

describe('main require', function () {
Expand Down
22 changes: 15 additions & 7 deletions test/wrapEmoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,38 @@
'use strict'

import { expect } from '@dimensionalpocket/development'
import { wrapEmoji } from '../src/html.js'
import { wrapEmoji } from '../src/wrapEmoji.js'

describe('#wrapEmoji', function () {
it('wraps emojis in <span>', function () {
it('wraps unicode emoji', function () {
expect(wrapEmoji('πŸ˜…')).to.eq('<span class="wmE">πŸ˜…</span>')
})

it('wraps non-unicode emoji', function () {
expect(wrapEmoji('βš”')).to.eq('<span class="wmE">βš”</span>')
})

it('wraps unicode version of non-unicode emoji', function () {
expect(wrapEmoji('βš”οΈ')).to.eq('<span class="wmE">βš”οΈ</span>')
})

it('does not affect non-emoji', function () {
expect(wrapEmoji('123abcπŸ˜…def456#*')).to.eq('123abc<span class="wmE">πŸ˜…</span>def456#*')
})

it('tags all emojis', function () {
it('wraps all emoji', function () {
expect(wrapEmoji('abcπŸ˜…defπŸ˜‚')).to.eq('abc<span class="wmE">πŸ˜…</span>def<span class="wmE">πŸ˜‚</span>')
})

it('tags all emojis in multiline text', function () {
it('wraps all emoji in multiline text', function () {
expect(wrapEmoji('πŸ’― ab\ncπŸ˜…def\n πŸ˜‚')).to.eq('<span class="wmE">πŸ’―</span> ab\nc<span class="wmE">πŸ˜…</span>def\n <span class="wmE">πŸ˜‚</span>')
})

it('tags sequential emojis in a single tag', function () {
it('wraps sequential emojis in a single tag', function () {
expect(wrapEmoji('abc πŸ˜‚πŸ’―πŸ˜… def')).to.eq('abc <span class="wmE">πŸ˜‚πŸ’―πŸ˜…</span> def')
})

it('breaks down sequential emojis split by multiline', function () {
expect(wrapEmoji('abc πŸ˜‚\nπŸ’―πŸ˜… def')).to.eq('abc <span class="wmE">πŸ˜‚</span>\n<span class="wmE">πŸ’―πŸ˜…</span> def')
it('breaks down sequential emoji split by multiline', function () {
expect(wrapEmoji('abc πŸ˜‚\nπŸ’―πŸ˜± def')).to.eq('abc <span class="wmE">πŸ˜‚</span>\n<span class="wmE">πŸ’―πŸ˜±</span> def')
})
})

0 comments on commit aca1750

Please sign in to comment.