Skip to content

Instantly share code, notes, and snippets.

@Kerrick
Created July 22, 2018 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kerrick/d936d4afb7192ac127474c2093883043 to your computer and use it in GitHub Desktop.
Save Kerrick/d936d4afb7192ac127474c2093883043 to your computer and use it in GitHub Desktop.
Slugify with Emoji Support
export const emojiMap: { [key: string]: string } = {
'๐Ÿ’ฏ': '100',
'๐Ÿ”ข': '1234',
'๐Ÿ˜€': 'grinning',
'๐Ÿ˜ฌ': 'grimacing',
'๐Ÿ˜': 'grin',
'๐Ÿ˜‚': 'joy',
// ...
};
import slugify from 'my-app/utils/slugify';
import { module, test } from 'qunit';
module('Unit | Utility | slugify', function(hooks) {
test('it dasherizes and removes all non SEO-friendly characters', function(assert) {
assert.expect(4);
assert.equal(slugify('Sales & Marketing'), 'sales-marketing');
assert.equal(
slugify('New - Today Only! The #1 way!'),
'new-today-only-the-1-way'
);
assert.equal(slugify('---- Select One ----'), 'select-one');
assert.equal(slugify('Roll the ๐ŸŽฒ emoji'), 'roll-the-game-die-emoji');
});
});
import { dasherize } from '@ember/string';
import { emojiMap } from 'my-app/utils/emoji';
export default function slugify(original: string): string {
const dasherized = dasherize(original);
let replacement = '';
for (let char of dasherized) {
if (emojiMap[char]) {
replacement += dasherize(emojiMap[char]);
} else if (/[A-Za-z0-9]/.test(char)) {
replacement += char;
} else if (
char === '-' &&
replacement.length !== 0 &&
replacement[replacement.length - 1] !== '-'
) {
replacement += char;
}
}
return replacement.replace(/-$/, '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment