Skip to content

Commit

Permalink
Add cleanDiacritics
Browse files Browse the repository at this point in the history
  • Loading branch information
esamattis committed Aug 24, 2015
1 parent 85edbe5 commit fc8705e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
15 changes: 15 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,21 @@ clean(" foo bar ");
// => "foo bar"
```

#### cleanDiacritics(string) => string

Replace [diacritic][d] characters with closest ASCII equivalents. Check the
[source][s] for supported characters. [Pull requests][p] welcome for missing
characters!

[d]: https://en.wikipedia.org/wiki/Diacritic
[s]: https://github.com/epeli/underscore.string/blob/master/cleanDiacritics.js
[p]: https://github.com/epeli/underscore.string/blob/master/CONTRIBUTING.markdown

```javascript
cleanDiacritics("ääkkönen");
// => "aakkonen"
```

#### chars(string) => array

```javascript
Expand Down
15 changes: 15 additions & 0 deletions cleanDiacritics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

var makeString = require('./helper/makeString');

var from = "ąàáäâãåæăćčĉęèéëêĝĥìíïîĵłľńňòóöőôõðøśșšŝťțŭùúüűûñÿýçżźž",
to = "aaaaaaaaaccceeeeeghiiiijllnnoooooooossssttuuuuuunyyczzz";

from += from.toUpperCase();
to += to.toUpperCase();

module.exports = function cleanDiacritics(str) {
return makeString(str).replace(/.{1}/g, function(c){
var index = from.indexOf(c);
return index === -1 ? c : to.charAt(index);
});
};
12 changes: 2 additions & 10 deletions slugify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@ var makeString = require('./helper/makeString');
var defaultToWhiteSpace = require('./helper/defaultToWhiteSpace');
var trim = require('./trim');
var dasherize = require('./dasherize');
var cleanDiacritics = require("./cleanDiacritics");

module.exports = function slugify(str) {
var from = "ąàáäâãåæăćčĉęèéëêĝĥìíïîĵłľńňòóöőôõðøśșšŝťțŭùúüűûñÿýçżźž",
to = "aaaaaaaaaccceeeeeghiiiijllnnoooooooossssttuuuuuunyyczzz",
regex = new RegExp(defaultToWhiteSpace(from), 'g');

str = makeString(str).toLowerCase().replace(regex, function(c){
var index = from.indexOf(c);
return to.charAt(index) || '-';
});

return trim(dasherize(str.replace(/[^\w\s-]/g, '-')), '-');
return trim(dasherize(cleanDiacritics(str).replace(/[^\w\s-]/g, '-')), '-');
};
21 changes: 21 additions & 0 deletions tests/cleanDiacritics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

var equal = require('assert').equal;
var cleanDiacritics = require('../cleanDiacritics');

var from = "ąàáäâãåæăćčĉęèéëêĝĥìíïîĵłľńňòóöőôõðøśșšŝťțŭùúüűûñÿýçżźž",
to = "aaaaaaaaaccceeeeeghiiiijllnnoooooooossssttuuuuuunyyczzz";

test('#cleanDiacritics', function() {

equal(cleanDiacritics(from), to);
equal(cleanDiacritics(from.toUpperCase()), to.toUpperCase());


equal(cleanDiacritics('ä'), 'a');
equal(cleanDiacritics('Ä Ø'), 'A O');
equal(cleanDiacritics('1 foo ääkkönen'), '1 foo aakkonen');
equal(cleanDiacritics('Äöö ÖÖ'), 'Aoo OO');
equal(cleanDiacritics(' ä '), ' a ');
equal(cleanDiacritics('- " , £ $ ä'), '- " , £ $ a');
});

0 comments on commit fc8705e

Please sign in to comment.