slugify.js 781 B

123456789101112131415161718192021222324
  1. var toString = require('../lang/toString');
  2. var replaceAccents = require('./replaceAccents');
  3. var removeNonWord = require('./removeNonWord');
  4. var trim = require('./trim');
  5. /**
  6. * Convert to lower case, remove accents, remove non-word chars and
  7. * replace spaces with the specified delimeter.
  8. * Does not split camelCase text.
  9. */
  10. function slugify(str, delimeter){
  11. str = toString(str);
  12. if (delimeter == null) {
  13. delimeter = "-";
  14. }
  15. str = replaceAccents(str);
  16. str = removeNonWord(str);
  17. str = trim(str) //should come after removeNonWord
  18. .replace(/ +/g, delimeter) //replace spaces with delimeter
  19. .toLowerCase();
  20. return str;
  21. }
  22. module.exports = slugify;