Skip to content

Instantly share code, notes, and snippets.

@juliomarcos
Last active August 29, 2015 14:06
Show Gist options
  • Save juliomarcos/cf40cd50e622bee4f3e5 to your computer and use it in GitHub Desktop.
Save juliomarcos/cf40cd50e622bee4f3e5 to your computer and use it in GitHub Desktop.
Generate Name Slug
var _ = require('lodash');
var removeDiacritics = require('diacritics').remove;
var replaceUnicodeSymbols = function(slug, lang) {
if (lang == undefined) lang = 'pt';
var enCharMap = {
'☢':"radioactive",'☠':"skull-and-bones",'☤':"caduceus",
'☣':"biohazard",'☭':"hammer-and-sickle", '☯':"yin-yang",'☮':"peace",
'☏':"telephone",'☔':"umbrella-with-rain-drops",'☎':"telephone",
'☀':"sun-with-rays",'★':"star",'☂':"umbrella",'☃':"snowman",
'✈':"airplane",'✉':"envelope",'✊':"raised-fist",
'©':'(c)', 'œ': 'oe', 'Œ': 'OE', '∑': 'sum', '®': '(r)', '†': '+',
'“': '"', '”': '"', '‘': "'", '’': "'", '∂': 'd', 'ƒ': 'f', '™': 'tm',
'℠': 'sm', '…': '...', '˚': 'o', 'º': 'o', 'ª': 'a', '•': '*',
'∆': 'delta','∞': 'infinity', '♥': 'love', '&': 'and', '|': 'or',
'<': 'less', '>': 'greater',
};
var ptCharMap = {
'☢':"radioativo",'☠':"caveira-e-ossos",'☤':"caduceu",
'☣':"perigo-biologico",'☭':"martelo-e-foice", '☯':"yin-yang",'☮':"paz",
'☏':"telefone",'☔':"guarda-chuva-com-gotas",'☎':"telefone",
'☀':"sol-com-raios",'★':"estrela",'☂':"guarda-chuva",'☃':"boneco-de-neve",
'✈':'aviao','✉':'envelope','✊':'punho-erguido',
'©':'(c)', 'œ': 'oe', 'Œ': 'OE', '∑': 'sum', '®': '(r)', '†': '+',
'“': '"', '”': '"', '‘': "'", '’': "'", '∂': 'd', 'ƒ': 'f', '™': 'tm',
'℠': 'sm', '…': '...', '˚': 'o', 'º': 'o', 'ª': 'a', '•': '*',
'∆': 'delta', '∞': 'infinito', '♥': 'amor', '&': 'e', '|': 'ou',
'<': 'menor', '>': 'maior',
};
var langToCharMapMap = {
'en' : enCharMap,
'pt' : ptCharMap
}
var charMap = langToCharMapMap[lang];
for (var i=0;i<slug.length;i++) {
Object.keys(charMap).forEach(function(uni){
if (slug[i]==uni) {
slug = slug.replace(uni, charMap[uni]);
}
});
}
return slug;
}
var genSlug = function(name, replaceUnicode) {
name = removeDiacritics(name);
name = name.trim();
name = name.replace(/\s+/g, ' ');
name = name.replace(/ /g, '-');
name = name.toLowerCase();
name = (replaceUnicode ? replaceUnicodeSymbols(name) : name);
name = encodeURIComponent(name);
return name;
}
var genNextSlugName = function(slugOriginal, c) {
var splitted = slugOriginal.split('-');
var last = _.last(splitted);
var isLastANumber = !(_.isNaN(parseInt(last)));
if (!isLastANumber) {
return slugOriginal + '-2';
} else {
var lastAsNumber = parseInt(last);
lastAsNumber++;
splitted.pop();
if (splitted.length > 0) {
return splitted.join('-') + '-' + lastAsNumber;
} else {
return lastAsNumber;
}
}
}
/**
* You may opt to replace unicode symbols, ex:
* slug('Raaax Liz ★ ', {replaceUnicode : true})
* >> raax-liz-star
* You can also specify that you want the next iteration
* of this slug, ex:
* slug.next('monster')
* >> monster-2
*/
var frontEnd = function (name, opts) {
opts = opts || {
replaceUnicode : true
};
return genSlug(name, opts.replaceUnicode);
}
frontEnd.next = genNextSlugName;
module.exports = frontEnd;
var should = require('should');
var sluglib = require('../utils/slug');
describe('SlugUtils', function () {
describe('genSlug', function () {
it('should generate url friendly slugs without replacing unicode', function() {
var inOut = [
['', ''],
[' --- j- ', '----j-'],
['78 22 @ xx', '78-22-%40-xx'],
['Julio Marcos', 'julio-marcos'],
[' julio marco ', 'julio-marco'],
['julio-3-marcos', 'julio-3-marcos'],
[' Júlio Açentís 8 7 ', 'julio-acentis-8-7'],
['★', '%E2%98%85'],
['jaa★liii','jaa%E2%98%85liii']
];
inOut.forEach(function(v, i){
var input = v[0];
var expected = v[1];
var got = sluglib(input, { replaceUnicode : false });
expected.should.eql(got);
});
});
});
describe('genSlug', function () {
it('should generate url friendly slugs replacing unicode symbols', function() {
var inOut = [
['', ''],
[' --- j- ', '----j-'],
['78 22 @ xx', '78-22-%40-xx'],
['Julio Marcos', 'julio-marcos'],
[' julio marco ', 'julio-marco'],
['julio-3-marcos', 'julio-3-marcos'],
[' Júlio Açentís 8 7 ', 'julio-acentis-8-7'],
['★', 'estrela'],
['★', 'estrela'],
['★ ♥', 'estrela-amor'],
['jaa★liii','jaaestrelaliii']
];
inOut.forEach(function(v, i){
var input = v[0];
var expected = v[1];
var got = sluglib(input);
expected.should.eql(got);
});
});
});
describe('genNextSlugName', function () {
it('should generate the next slug', function() {
var inOut = [
['', '-2'],
['jamenda', 'jamenda-2'],
['criancas-brincando-na-rua', 'criancas-brincando-na-rua-2'],
['criancas-brincando-na-rua-2', 'criancas-brincando-na-rua-3'],
['leia-1', 'leia-2'],
['hrozi-llik-3-jaak', 'hrozi-llik-3-jaak-2'],
['1', '2'],
['699', '700'],
['-2', '-3'],
['---2', '---3'],
];
inOut.forEach(function(v, i){
var input = v[0];
var expected = v[1];
var got = sluglib.next(input);
expected.should.eql(got);
});
});
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment