Skip to content

Instantly share code, notes, and snippets.

@szekelygobe
Last active April 10, 2024 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szekelygobe/c4e9d7084ffbae0748930b9b682e7336 to your computer and use it in GitHub Desktop.
Save szekelygobe/c4e9d7084ffbae0748930b9b682e7336 to your computer and use it in GitHub Desktop.
Vue.js very basic composable for slugifying text
// inspired by https://codepen.io/mrrudrud/pen/QVyZze
export function useSlugify(){
function slugify(text){
let slug = text.toLowerCase();
// Letter "e"
slug = slug.replace(/e|é|è|ẽ|ẻ|ẹ|ê|ế|ề|ễ|ể|ệ/gi, 'e');
// Letter "a"
slug = slug.replace(/a|á|à|ã|ả|ạ|ă|ắ|ằ|ẵ|ẳ|ặ|â|ấ|ầ|ẫ|ẩ|ậ/gi, 'a');
// Letter "o"
slug = slug.replace(/o|ó|ò|õ|ỏ|ọ|ô|ố|ồ|ỗ|ổ|ộ|ơ|ớ|ờ|ỡ|ở|ợ/gi, 'o');
// Letter "u"
slug = slug.replace(/u|ú|ù|ũ|ủ|ụ|ư|ứ|ừ|ữ|ử|ự/gi, 'u');
// Letter "s"
slug = slug.replace(/s|ş|ș/gi, 's');
// Letter "t"
slug = slug.replace(/t|ţ|ț/gi, 't');
// Letter "i"
slug = slug.replace(/i|í|î/gi, 'i');
// Letter "d"
slug = slug.replace(/đ/gi, 'd');
// Trim the last whitespace
slug = slug.replace(/\s*$/g, '');
// Change whitespace to "_"
slug = slug.replace(/\s+/g, '-');
return slug;
}
return {slugify}
}
@clopezpro
Copy link

because use _ ?

@szekelygobe
Copy link
Author

because use _ ?

You mean why do I use _ the underscore ? That is what you connect the words with in a slug.
You have Some text to slugify and you get some_text_to_slugify as a slug.
You can give a parameter to the function and provide other character to glue the words.

@clopezpro
Copy link

because use _ ?

You mean why do I use _ the underscore ? That is what you connect the words with in a slug. You have Some text to slugify and you get some_text_to_slugify as a slug. You can give a parameter to the function and provide other character to glue the words.

There is no problem if I use the -?, I wanted to ask why I change from - to _ and if it was not advisable to use the hyphen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment