-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #1550 - Slugify IDs with the most minimal scheme
- Loading branch information
1 parent
b5d1533
commit 0d1046e
Showing
4 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
define(function (require) { | ||
var _ = require('lodash'); | ||
return function (id) { | ||
if (id == null) return; | ||
|
||
var trans = { | ||
'/' : '-slash-', | ||
'\\?' : '-questionmark-', | ||
'\\&' : '-ampersand-', | ||
'=' : '-equal-' | ||
}; | ||
_.each(trans, function (val, key) { | ||
var regex = new RegExp(key); | ||
id = id.replace(regex, val); | ||
}); | ||
id = id.replace(/[\s]+/g, '_'); | ||
id = id.replace(/[_]+/g, '_'); | ||
return id; | ||
}; | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
define(function (require) { | ||
var _ = require('lodash'); | ||
var slugifyId = require('utils/slugify_id'); | ||
|
||
describe('slugifyId()', function () { | ||
|
||
var fixtures = [ | ||
['test/test', 'test-slash-test'], | ||
['test?test', 'test-questionmark-test'], | ||
['test=test', 'test-equal-test'], | ||
['test&test', 'test-ampersand-test'], | ||
['test / test', 'test_-slash-_test'], | ||
['test ? test', 'test_-questionmark-_test'], | ||
['test = test', 'test_-equal-_test'], | ||
['test & test', 'test_-ampersand-_test'], | ||
['test / ^test', 'test_-slash-_^test'], | ||
['test ? test', 'test_-questionmark-_test'], | ||
['test = test', 'test_-equal-_test'], | ||
['test & test', 'test_-ampersand-_test'] | ||
]; | ||
|
||
_.each(fixtures, function (fixture) { | ||
var msg = 'should convert ' + fixture[0] + ' to ' + fixture[1]; | ||
it(msg, function () { | ||
var results = slugifyId(fixture[0]); | ||
expect(results).to.be(fixture[1]); | ||
}); | ||
}); | ||
|
||
it('should do nothing if the id is undefined', function () { | ||
expect(slugifyId(undefined)).to.be(undefined); | ||
}); | ||
|
||
}); | ||
}); |