diff --git a/index.js b/index.js index 5ac202b..eb31696 100644 --- a/index.js +++ b/index.js @@ -7,12 +7,22 @@ 'use strict'; +var re = { + capital: /[A-Z]|[\u00C0-\u00D6\u00D8-\u00DD]/, + small: /[a-z]|[\u00DF-\u00F6\u00F8-\u00FF]/, + space: /[ \t]/, + nonWord: /[^A-Za-z0-9\u00C0-\u00D6\u00D8-\u00DD\u00DF-\u00F6\u00F8-\u00FF]/ +}; + +var camelCase = new RegExp('(' + re.small.source + ')(' + re.capital.source + ')', 'g'); +var spaceOrNonWord = new RegExp(re.space.source + '|' + re.nonWord.source, 'g'); + module.exports = function dashify(str) { if (typeof str !== 'string') { throw new TypeError('expected a string'); } - str = str.replace(/([a-z])([A-Z])/g, '$1-$2'); - str = str.replace(/[ \t\W]/g, '-'); + str = str.replace(camelCase, '$1-$2'); + str = str.replace(spaceOrNonWord, '-'); str = str.replace(/^-+|-+$/g, ''); return str.toLowerCase(); }; diff --git a/package.json b/package.json index 8cde6a7..07e1ff3 100644 --- a/package.json +++ b/package.json @@ -17,9 +17,10 @@ "node": ">=0.10.0" }, "scripts": { - "test": "mocha" + "test": "eslint index.js & mocha" }, "devDependencies": { + "eslint": "^2.11.1", "gulp-format-md": "^0.1.9", "mocha": "^2.5.1" }, diff --git a/test.js b/test.js index 04a1cba..0be1efc 100644 --- a/test.js +++ b/test.js @@ -48,6 +48,11 @@ describe('dashify', function() { it('should work when leading character is uppercase:', function() { assert.strictEqual(dashify('Foo barBaz quux'), 'foo-bar-baz-quux'); }); + + it('should keep Latin Unicode characters in the transformed string:', function() { + assert.strictEqual(dashify('São Tomé and Príncipe'), 'são-tomé-and-príncipe'); + assert.strictEqual(dashify('æçÐo Test Ø'), 'æç-ðo-test-ø'); + }); it('should throw an error if a string is not passed:', function(cb) { try {