Skip to content

Add more conformant unicode handling #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First argument should be the actual, second expected, this threw me off 😉

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I get this confused a lot :)

})
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'
},
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests cases later also contained duplicate, to ensure those tests are in sync with the referred repo, I changes these here instead.

{
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',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope this naming convention of using numbers here is OK.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally fine with this!

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'
}
]