Skip to content

Commit

Permalink
make lowercase default in rfc3986 mode only
Browse files Browse the repository at this point in the history
fixes #32
  • Loading branch information
dodo committed Apr 18, 2015
1 parent b6c79c7 commit 86a73bf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
14 changes: 6 additions & 8 deletions slug.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ 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])
opts[key] = opts[key] || defaults[key];
['replacement','multicharmap','charmap','remove','lower'].forEach(function (key) {
opts[key] = (key in opts) ? opts[key] : defaults[key];
});
if ('undefined' === typeof opts.symbols)
opts.symbols = defaults.symbols;
Expand Down Expand Up @@ -60,15 +58,13 @@ function slug(string, opts) {
result = result.replace(/^\s+|\s+$/g, ''); // trim leading/trailing spaces
result = result.replace(/[-\s]+/g, opts.replacement); // convert spaces
result = result.replace(opts.replacement+"$",''); // remove trailing separator
if (opts.lowercase) {
result = result.toLowerCase()
}
if (opts.lower)
result = result.toLowerCase();
return result;
};

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

slug.multicharmap = slug.defaults.multicharmap = {
Expand Down Expand Up @@ -168,13 +164,15 @@ slug.defaults.modes = {
replacement: '-',
symbols: true,
remove: null,
lower: true,
charmap: slug.defaults.charmap,
multicharmap: slug.defaults.multicharmap,
},
pretty: {
replacement: '-',
symbols: true,
remove: /[.]/g,
lower: false,
charmap: slug.defaults.charmap,
multicharmap: slug.defaults.multicharmap,
},
Expand Down
37 changes: 19 additions & 18 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".toLowerCase()]
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz"]

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".toLowerCase()]
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz"]

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".toLowerCase()]
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz"]

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.toLowerCase()]
[slug "foo #{char} bar baz"].should.eql [expected]

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".toLowerCase()]
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz"]

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".toLowerCase()]
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz"]

it 'should replace latvian chars', ->
char_map = {
Expand All @@ -112,7 +112,7 @@ 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".toLowerCase()]
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz"]

it 'should replace vietnamese chars', ->
char_map = {
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".toLowerCase()]
[slug "foo #{char} bar baz"].should.eql ["foo-#{replacement}-bar-baz"]

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

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

it 'should remove ellipsis in pretty mode', ->
char_map = {
Expand Down Expand Up @@ -212,22 +213,22 @@ describe 'slug', ->
[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"
[slug(text, mode:'pretty')].should.eql [expected]
expected = "Its-your-journey-we-guide-you-through"
[slug text, mode:'pretty'].should.eql [expected]

it 'should default to lowercase', ->
it 'should default to lowercase in rfc3986 mode', ->
text = "It's Your Journey We Guide You Through."
expected = "its-your-journey-we-guide-you-through"
[slug(text)].should.eql [expected]
expected = "its-your-journey-we-guide-you-through."
[slug text, mode:'rfc3986'].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]
expected = "Its-Your-Journey-We-Guide-You-Through."
[slug text, mode:'rfc3986', lower:off].should.eql [expected]

0 comments on commit 86a73bf

Please sign in to comment.