From 1f0d31ab850ff95ff2925d026f306948f3e5ca27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81bel=20Ny=C3=A9ki?= Date: Mon, 20 Feb 2023 16:28:56 +0100 Subject: [PATCH] Make initials ignore emojis --- addon/utils/initials.js | 5 ++++- tests/integration/components/ember-initials-test.js | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/addon/utils/initials.js b/addon/utils/initials.js index 8fb156c..6c3c7ed 100644 --- a/addon/utils/initials.js +++ b/addon/utils/initials.js @@ -16,6 +16,9 @@ export default function(name) { // Private +const EMOJI_RE = /\p{Emoji}/gu; + function capitalizedFirstLetter(word) { - return word ? word[0].toUpperCase() : ''; + const trimmed = (word || '').replace(EMOJI_RE, ''); + return trimmed ? trimmed[0].toUpperCase() : ''; } diff --git a/tests/integration/components/ember-initials-test.js b/tests/integration/components/ember-initials-test.js index 795b400..76dbf09 100644 --- a/tests/integration/components/ember-initials-test.js +++ b/tests/integration/components/ember-initials-test.js @@ -41,6 +41,14 @@ module('ember-initials', function(hooks) { await getImage(this).then((img) => assert.equal(img.textContent, '')); }); + test('when name starts with an emoji', async function(assert) { + assert.expect(1); + + await render(hbs`{{ember-initials name='🍕Super cool'}}`); + + await getImage(this).then((img) => assert.equal(img.textContent, 'SC')); + }); + test('when src is overridden by an image', async function(assert) { assert.expect(1);