Created
July 22, 2018 04:44
-
-
Save Kerrick/d936d4afb7192ac127474c2093883043 to your computer and use it in GitHub Desktop.
Slugify with Emoji Support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const emojiMap: { [key: string]: string } = { | |
'๐ฏ': '100', | |
'๐ข': '1234', | |
'๐': 'grinning', | |
'๐ฌ': 'grimacing', | |
'๐': 'grin', | |
'๐': 'joy', | |
// ... | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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