Created
July 2, 2016 13:29
-
-
Save carlohcs/e1ab2089e9b9b2b4b687f32970178b6d to your computer and use it in GitHub Desktop.
A helpert to create a basic slug from some string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(namespace, Handlebars) { | |
/** | |
* Retorna uma string no formato "uma-texto-a-ser-formatado" | |
* | |
* @param {String} text | |
* @param {Sttring} separador | |
* @return {String} | |
*/ | |
function slug(text, separator) { | |
var | |
slugged; | |
separator = separator || '-'; | |
slugged = text.toLowerCase(); | |
slugged = slugged.replace(/\-/g, ''); // Remove '-' | |
slugged = slugged.replace(/\s/g, separator); // Adiciona separador nos espaços | |
// Remove acentos | |
slugged = slugged.replace(/[áàã]/g, 'a'); | |
slugged = slugged.replace(/é/g, 'e'); | |
slugged = slugged.replace(/í/g, 'i'); | |
slugged = slugged.replace(/ó/g, 'o'); | |
slugged = slugged.replace(/ú/g, 'u'); | |
slugged = slugged.replace(/ç/g, 'c'); | |
return slugged; | |
} | |
namespace.slug = slug; | |
Handlebars.registerHelper('slug', function(context) { | |
return slug(context, '-'); | |
}); | |
})(app.helpers = app.helpers || {}, Handlebars); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment