Skip to content

Commit

Permalink
Merge remote-tracking branch 'contrib-LegitTalon/default-lowercase'
Browse files Browse the repository at this point in the history
Conflicts:
	test/slug.test.coffee
  • Loading branch information
dodo committed Apr 18, 2015
2 parents d987b49 + 3fce2fa commit b6c79c7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ print(slug('i ♥ unicode', '_')) // If you prefer something else then `-` as se
// > i_love_unicode

slug.charmap[''] = 'freaking love' // change default charmap or use option {charmap:{…}} as 2. argument
print(slug('I ♥ UNICODE').toLowerCase()) // If you prefer lower case
// > i-freaking-love-unicode
print(slug('I ♥ UNICODE', {lowercase: false})) // If you prefer not lower case
// > I-freaking-love-UNICODE

print(slug('i <3 unicode'))
// > i-love-unicode
Expand Down
8 changes: 7 additions & 1 deletion slug.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function slug(string, opts) {
opts = {replacement:opts};
opts = opts || {};
opts.mode = opts.mode || slug.defaults.mode;
opts.lowercase = opts.lowercase === false? false : slug.defaults.lowercase;
var defaults = slug.defaults.modes[opts.mode];
['replacement','multicharmap','charmap','remove'].forEach(function (key) {
if ('replacement' !== key || 'string' !== typeof opts[key])
Expand Down Expand Up @@ -58,11 +59,16 @@ function slug(string, opts) {
}
result = result.replace(/^\s+|\s+$/g, ''); // trim leading/trailing spaces
result = result.replace(/[-\s]+/g, opts.replacement); // convert spaces
return result.replace(opts.replacement+"$",''); // remove trailing separator
result = result.replace(opts.replacement+"$",''); // remove trailing separator
if (opts.lowercase) {
result = result.toLowerCase()
}
return result;
};

slug.defaults = {
mode: 'pretty',
lowercase: true
};

slug.multicharmap = slug.defaults.multicharmap = {
Expand Down
40 changes: 23 additions & 17 deletions test/slug.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe 'slug', ->
'ý': 'y', 'þ': 'th', 'ÿ': 'y', '': 'SS'
}
for char, replacement of char_map
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz"]
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz".toLowerCase()]

it 'should replace greek chars', ->
char_map = {
Expand All @@ -59,15 +59,15 @@ describe 'slug', ->
'Ϋ':'Y'
}
for char, replacement of char_map
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz"]
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz".toLowerCase()]

it 'should replace turkish chars', ->
char_map = {
'ş':'s', 'Ş':'S', 'ı':'i', 'İ':'I', 'ç':'c', 'Ç':'C', 'ü':'u', 'Ü':'U',
'ö':'o', 'Ö':'O', 'ğ':'g', 'Ğ':'G'
}
for char, replacement of char_map
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz"]
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz".toLowerCase()]

it 'should replace cyrillic chars', ->
char_map = {
Expand All @@ -85,7 +85,7 @@ describe 'slug', ->
for char, replacement of char_map
expected = "foo-#{replacement}-bar-baz"
expected = "foo-bar-baz" if not replacement
[slug "foo #{char} bar baz"].should.eql [expected]
[slug "foo #{char} bar baz"].should.eql [expected.toLowerCase()]

it 'should replace czech chars', ->
char_map = {
Expand All @@ -94,7 +94,7 @@ describe 'slug', ->
'Ů':'U', 'Ž':'Z'
}
for char, replacement of char_map
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz"]
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz".toLowerCase()]

it 'should replace polish chars', ->
char_map = {
Expand All @@ -103,7 +103,7 @@ describe 'slug', ->
'Ź':'Z', 'Ż':'Z'
}
for char, replacement of char_map
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz"]
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz".toLowerCase()]

it 'should replace latvian chars', ->
char_map = {
Expand All @@ -112,8 +112,8 @@ describe 'slug', ->
'Ķ':'K', 'Ļ':'L', 'Ņ':'N', 'Š':'S', 'Ū':'U', 'Ž':'Z'
}
for char, replacement of char_map
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz"]
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz".toLowerCase()]

it 'should replace vietnamese chars', ->
char_map = {
'': 'A', '': 'A', '': 'A', '': 'A', '': 'A', '': 'A', '': 'A',
Expand All @@ -133,7 +133,7 @@ describe 'slug', ->
'': 'y', 'đ': 'd'
}
for char, replacement of char_map
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz"]
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz".toLowerCase()]

it 'should replace currencies', ->
char_map = {
Expand All @@ -159,7 +159,7 @@ describe 'slug', ->
}
for char, replacement of char_map
[slug "foo #{char} bar baz",
mode: "rfc3986"].should.eql ["foo-#{replacement}-bar-baz"]
mode: "rfc3986"].should.eql ["foo-#{replacement}-bar-baz".toLowerCase()]

it 'should replace symbols in pretty mode', ->
char_map = {
Expand All @@ -170,7 +170,7 @@ describe 'slug', ->
'<': 'less', '>': 'greater'
}
for char, replacement of char_map
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz"]
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz".toLowerCase()]

it 'should remove ellipsis in pretty mode', ->
char_map = {
Expand Down Expand Up @@ -205,23 +205,29 @@ describe 'slug', ->
for char in char_map
[slug "foo #{char} bar baz", symbols:no].should.eql ["foo-bar-baz"]

it 'should allow forcing lowercase slugs', ->
[slug('FOO Bar baZ').toLowerCase()].should.eql ['foo-bar-baz']
[slug('FOO Bar baZ', replacement:'_').toLowerCase()].should.eql ['foo_bar_baz']

it 'should allow altering the charmap', ->
charmap = {
'f': 'ph', 'o':'0', 'b':'8', 'a':'4', 'r':'2', 'z':'5'
}
[slug("foo bar baz", {charmap}).toUpperCase()].should.eql ['PH00-842-845']

it 'should replace lithuanian characters', ->
slug('ąčęėįšųūžĄČĘĖĮŠŲŪŽ').should.eql 'aceeisuuzACEEISUUZ'
slug('ąčęėįšųūžĄČĘĖĮŠŲŪŽ').should.eql 'aceeisuuzaceeisuuz'

it 'should replace multichars', ->
[slug "w/ <3 && sugar || ☠"].should.eql ['with-love-and-sugar-or-skull-and-bones']

it 'should be flavourable', ->
text = "It's your journey ... we guide you through."
expected = "Its-your-journey-we-guide-you-through"
expected = "its-your-journey-we-guide-you-through"
[slug(text, mode:'pretty')].should.eql [expected]

it 'should default to lowercase', ->
text = "It's Your Journey We Guide You Through."
expected = "its-your-journey-we-guide-you-through"
[slug(text)].should.eql [expected]

it 'should allow disabling of lowercase', ->
text = "It's Your Journey We Guide You Through."
expected = "Its-Your-Journey-We-Guide-You-Through"
[slug(text, lowercase: false)].should.eql [expected]

0 comments on commit b6c79c7

Please sign in to comment.