Skip to content

Instantly share code, notes, and snippets.

@hantv15
Forked from bluzky/slug.js
Created December 29, 2022 02:43
Show Gist options
  • Select an option

  • Save hantv15/279a2e79b9f067e6912bc7d2a889d3d4 to your computer and use it in GitHub Desktop.

Select an option

Save hantv15/279a2e79b9f067e6912bc7d2a889d3d4 to your computer and use it in GitHub Desktop.

Revisions

  1. @bluzky bluzky revised this gist Jun 3, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions slug.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    function stringToSlug(str) {
    // remove accents
    var from = "àáãảạăằắẳẵặâầấẩẫậèéẻẽẹêềếểễệđùúủũụưừứửữựòóỏõọôồốổỗộơờớởỡợìíỉĩịäëïîöüûñç",
    to = "aaaaaaaaaaaaaaaaaeeeeeeeeeeeduuuuuuuuuuuoooooooooooooooooiiiiiaeiiouunc";
    var from = "àáãảạăằắẳẵặâầấẩẫậèéẻẽẹêềếểễệđùúủũụưừứửữựòóỏõọôồốổỗộơờớởỡợìíỉĩịäëïîöüûñçýỳỹỵỷ",
    to = "aaaaaaaaaaaaaaaaaeeeeeeeeeeeduuuuuuuuuuuoooooooooooooooooiiiiiaeiiouuncyyyyy";
    for (var i=0, l=from.length ; i < l ; i++) {
    str = str.replace(RegExp(from[i], "gi"), to[i]);
    }
  2. @bluzky bluzky revised this gist Nov 9, 2017. No changes.
  3. @bluzky bluzky created this gist Nov 9, 2017.
    15 changes: 15 additions & 0 deletions slug.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    function stringToSlug(str) {
    // remove accents
    var from = "àáãảạăằắẳẵặâầấẩẫậèéẻẽẹêềếểễệđùúủũụưừứửữựòóỏõọôồốổỗộơờớởỡợìíỉĩịäëïîöüûñç",
    to = "aaaaaaaaaaaaaaaaaeeeeeeeeeeeduuuuuuuuuuuoooooooooooooooooiiiiiaeiiouunc";
    for (var i=0, l=from.length ; i < l ; i++) {
    str = str.replace(RegExp(from[i], "gi"), to[i]);
    }

    str = str.toLowerCase()
    .trim()
    .replace(/[^a-z0-9\-]/g, '-')
    .replace(/-+/g, '-');

    return str;
    }