Skip to content

Commit

Permalink
Update string-prototypes.js
Browse files Browse the repository at this point in the history
  • Loading branch information
christianjames committed Aug 13, 2015
1 parent f21063e commit 1c77aa0
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions string-prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,23 @@ String.prototype.ucwords = function () {
});

}

String.prototype.createSlug = function () {
var str = this;
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();

// remove accents, swap ñ for n, etc
var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
var to = "aaaaeeeeiiiioooouuuunc------";
for (var i=0, l=from.length ; i<l ; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}

str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes

return str;

}

0 comments on commit 1c77aa0

Please sign in to comment.