diff --git a/lib/speakingurl.js b/lib/speakingurl.js index 4a62bc0..cdca907 100644 --- a/lib/speakingurl.js +++ b/lib/speakingurl.js @@ -1418,6 +1418,8 @@ var markChars = ['.', '!', '~', '*', "'", '(', ')'].join(''); + var isNormalizeFeaturePresent = typeof ''.normalize === 'function'; + /** * getSlug * @param {string} input input string @@ -1529,6 +1531,11 @@ // escape all necessary chars allowedChars = escapeChars(allowedChars); + // normalize unicode chars + if (isNormalizeFeaturePresent) { + input = input.normalize('NFC'); + } + // trim whitespaces input = input.replace(/(^\s+|\s+$)/g, ''); @@ -1686,4 +1693,4 @@ } } catch (e) {} } -})(this); \ No newline at end of file +})(this); diff --git a/test/test-unicode-normalization.js b/test/test-unicode-normalization.js new file mode 100644 index 0000000..9fce433 --- /dev/null +++ b/test/test-unicode-normalization.js @@ -0,0 +1,22 @@ +/* global describe,it */ + +var getSlug = require('../lib/speakingurl'); + +describe('getSlug unicode normalization', function () { + 'use strict'; + + it('should normalize unicode before applying transformations', function (done) { + // given + var wordWithUmlautCanonical = 'klärung'; + var wordWithUmlautCombined = 'kla\u0308rung'; + + // when + var slugifiedCanonical = getSlug(wordWithUmlautCanonical); + var slugifiedCombined = getSlug(wordWithUmlautCombined); + + // expect + getSlug(slugifiedCanonical).should.eql(slugifiedCombined); + + done(); + }); +});