Skip to content

Commit

Permalink
allow multi char map replacement
Browse files Browse the repository at this point in the history
fixes #25
  • Loading branch information
dodo committed Sep 22, 2014
1 parent 22596f5 commit 3c0533e
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions slug.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,39 @@ function slug(string, opts) {
if ('string' === typeof opts)
opts = {replacement:opts};
opts.replacement = opts.replacement || slug.defaults.replacement;
opts.multicharmap = opts.multicharmap || slug.defaults.multicharmap;
opts.charmap = opts.charmap || slug.defaults.charmap;
if ('undefined' === typeof opts.symbols)
opts.symbols = slug.defaults.symbols;
var lengths = [];
Object.keys(opts.multicharmap).forEach(function (key) {
var len = key.length;
if (lengths.indexOf(len) === -1)
lengths.push(len);
});
var code, unicode, result = "";
for (var char, i = 0, len = string.length; i < len; i++) { char = string[i];
if (opts.charmap[char]) {
char = opts.charmap[char];
code = char.charCodeAt(0);
} else {
code = string.charCodeAt(i);
}
if (opts.symbols && (unicode = symbols(code))) {
char = unicode.name.toLowerCase();
for(var j = 0, rl = removelist.length; j < rl; j++) {
char = char.replace(removelist[j], '');
for (var char, i = 0, l = string.length; i < l; i++) { char = string[i];
if (!lengths.some(function (len) {
var str = string.substr(i, len);
if (opts.multicharmap[str]) {
i += len - 1;
char = opts.multicharmap[str];
return true;
} else return false;
})) {
if (opts.charmap[char]) {
char = opts.charmap[char];
code = char.charCodeAt(0);
} else {
code = string.charCodeAt(i);
}
if (opts.symbols && (unicode = symbols(code))) {
char = unicode.name.toLowerCase();
for(var j = 0, rl = removelist.length; j < rl; j++) {
char = char.replace(removelist[j], '');
}
char = char.replace(/^\s+|\s+$/g, '');
}
char = char.replace(/^\s+|\s+$/g, '');
}
char = char.replace(/[^\w\s\-\.\_~]/g, ''); // allowed
result += char;
Expand All @@ -46,6 +62,9 @@ slug.defaults = {
symbols: true,
};

slug.multicharmap = slug.defaults.multicharmap = {
'<3': 'love', '&&': 'and', '||': 'or', 'w/': 'with',
};

// https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/media/js/urlify.js
slug.charmap = slug.defaults.charmap = {
Expand Down

0 comments on commit 3c0533e

Please sign in to comment.