How to parameterize/transliterate in Javascript?
In Ruby on Rails you can easily convert "any" text into a format which would work for subdomains/pathnames.
1) "I am nobody." -> "i-am-nobody"
2) "Grünkohl is a german word." -> "grunkohl-is-a-german-word"
I'd like to do this on the client-side for high responsiveness (alternative would be via Ajax).
The last example is called transliteration (converting Umlauts and other non-latin alphabets letters into latin ones). Transliter开发者_运维技巧ation would be a nice2have feature (in such cases I could fallback to Ajax to let Iconv do it).
Anybody knows how to do this with JavaScript? My current code works fine but has issues with multiple blank spaces, and Tête-à-tête
becomes Tte--tte
which is just ugly.
When I needed this I used the Django javascript implementation for this wich covers most of this and even more :)
It can be found here: https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/static/admin/js/urlify.js
urlify for node.js npm package https://npmjs.org/package/parameterize
Here's a JS to transliterate the passed phrase Russian to English for URL needs. One can modify its data to apply for French or any other language. Anything besides letters and numbers is substituted with "-", double and ending "-" are removed.
function url(word, letterVersionOrder) {
var letters = 'a b v g d e ["zh","j"] z i y k l m n o p r s t u f h c ch sh ["shh","shch"] ~ y ~ e yu ya ~ ["jo","e"]'.split(' ');
var wordSeparator = '';
word = word.toLowerCase();
for (var i = 0; i < word.length; ++i) {
var charCode = word.charCodeAt(i);
var chars = (charCode >= 1072 ? letters[charCode - 1072] : word[i]);
if (chars.length < 3) {
wordSeparator += chars;
} else {
wordSeparator += eval(chars)[letterVersionOrder];
}
}
return(wordSeparator.
replace(/[^a-zA-Z0-9\-]/g, '-').
replace(/[-]{2,}/gim, '-').
replace(/^\-+/g, '').
replace(/\-+$/g, ''));
}
Here is a faster minified version:
function url(w, v) { var tr = 'a b v g d e ["zh","j"] z i y k l m n o p r s t u f h c ch sh ["shh","shch"] ~ y ~ e yu ya ~ ["jo","e"]'.split(' '); var ww = ''; w = w.toLowerCase(); for (var i = 0; i < w.length; ++i) { var cc = w.charCodeAt(i); var ch = (cc >= 1072 ? tr[cc - 1072] : w[i]); if (ch.length < 3) {ww += ch;} else {ww += eval(ch)[v];} } return(ww.replace(/[^a-zA-Z0-9\-]/g, '-').replace(/[-]{2,}/gim, '-').replace(/^\-+/g, '').replace(/\-+$/g, ''));}
Adopted from here.
精彩评论