Skip to content

Commit

Permalink
Merge pull request #8 from Flet/better-unicode-support
Browse files Browse the repository at this point in the history
Add more conformant unicode handling
  • Loading branch information
Flet committed Apr 26, 2016
2 parents 551a78f + 27fcdfc commit 1ef870f
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 12 deletions.
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var emoji = require('emoji-regex')

module.exports = BananaSlug

function BananaSlug () {
Expand Down Expand Up @@ -42,14 +44,19 @@ BananaSlug.prototype.reset = function () {

var whitespace = /\s/g

function lower (string) {
return string.toLowerCase()
}

function slugger (string) {
var re = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,.\/:;<=>?@\[\]^`{|}~]/g
var maintainCase = false
var replacement = '-'
var result

if (typeof string !== 'string') return ''
if (!maintainCase) string = string.toLowerCase()
result = string.trim().replace(re, '').replace(whitespace, replacement)
return result
if (!maintainCase) string = string.replace(/[A-Z]+/g, lower)
return string.trim()
.replace(re, '')
.replace(emoji(), '')
.replace(whitespace, replacement)
}
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
"description": "Generate a slug just like GitHub does for markdown headings.",
"version": "1.1.0",
"author": "Dan Flettre <[email protected]>",
"contributors": [
"Dan Flettre <[email protected]>",
"Titus Wormer <[email protected]> (http://wooorm.com)"
],
"bugs": {
"url": "https://github.com/Flet/github-slugger/issues"
},
"dependencies": {
"emoji-regex": "^6.0.0"
},
"devDependencies": {
"standard": "*",
"tap-spec": "^4.0.2",
Expand Down
108 changes: 100 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('github test cases', function (t) {
var slugger = new GithubSlugger()

testCases.forEach(function (test) {
t.equals(test.slug, slugger.slug(test.text), test.mesg)
t.equals(slugger.slug(test.text), test.slug, test.mesg)
})
t.end()
})
Expand Down Expand Up @@ -71,22 +71,114 @@ var testCases = [
},
{
mesg: 'deals with duplicates correctly',
text: 'duplicate',
slug: 'duplicate'
text: 'duplicates',
slug: 'duplicates'
},
{
mesg: 'deals with duplicates correctly-1',
text: 'duplicate',
slug: 'duplicate-1'
text: 'duplicates',
slug: 'duplicates-1'
},
{
mesg: 'deals with duplicates correctly-2',
text: 'duplicate',
slug: 'duplicate-2'
text: 'duplicates',
slug: 'duplicates-2'
},
{
mesg: 'deals with non-latin chars',
text: 'Привет',
slug: 'привет'
slug: 'Привет'
},
// https://github.com/wooorm/gh-and-npm-slug-generation
{
mesg: 'gh-and-npm-slug-generation-1',
text: 'I ♥ unicode',
slug: 'i--unicode'
},
{
mesg: 'gh-and-npm-slug-generation-2',
text: 'Dash-dash',
slug: 'dash-dash'
},
{
mesg: 'gh-and-npm-slug-generation-3',
text: 'en–dash!',
slug: 'endash'
},
{
mesg: 'gh-and-npm-slug-generation-4',
text: 'em–dash',
slug: 'emdash'
},
{
mesg: 'gh-and-npm-slug-generation-5',
text: '😄 unicode emoji',
slug: '-unicode-emoji'
},
{
mesg: 'gh-and-npm-slug-generation-6',
text: '😄-😄 unicode emoji',
slug: '--unicode-emoji'
},
{
mesg: 'gh-and-npm-slug-generation-7',
text: '😄_😄 unicode emoji',
slug: '_-unicode-emoji'
},
{
mesg: 'gh-and-npm-slug-generation-8',
text: '😄 - an emoji',
slug: '---an-emoji'
},
{
mesg: 'gh-and-npm-slug-generation-9',
text: ':smile: - a gemoji',
slug: 'smile---a-gemoji'
},
{
mesg: 'gh-and-npm-slug-generation-10',
text: ' Initial spaces',
slug: 'initial-spaces'
},
{
mesg: 'gh-and-npm-slug-generation-11',
text: 'Final spaces ',
slug: 'final-spaces'
},
{
mesg: 'gh-and-npm-slug-generation-12',
text: 'duplicate',
slug: 'duplicate'
},
{
mesg: 'gh-and-npm-slug-generation-13',
text: 'duplicate',
slug: 'duplicate-1'
},
{
mesg: 'gh-and-npm-slug-generation-14',
text: 'Привет non-latin 你好',
slug: 'Привет-non-latin-你好'
},
// https://github.com/chrisdickinson/emoji-slug-example
{
mesg: 'emoji-slug-example-1',
text: ':ok: No underscore',
slug: 'ok-no-underscore'
},
{
mesg: 'emoji-slug-example-2',
text: ':ok_hand: Single',
slug: 'ok_hand-single'
},
{
mesg: 'emoji-slug-example-3',
text: ':ok_hand::hatched_chick: Two in a row with no spaces',
slug: 'ok_handhatched_chick-two-in-a-row-with-no-spaces'
},
{
mesg: 'emoji-slug-example-4',
text: ':ok_hand: :hatched_chick: Two in a row',
slug: 'ok_hand-hatched_chick-two-in-a-row'
}
]

0 comments on commit 1ef870f

Please sign in to comment.