How to parameterize / transliterate in Javascript?

In Ruby on Rails, you can easily convert “any” text to a format that will work for subdomains / paths.

1) "I am nobody." -> "i-am-nobody"
2) "Grünkohl is a german word." -> "grunkohl-is-a-german-word"

I would like to do this on the client side for high responsiveness (the alternative will be via Ajax).

The last example is called transliteration (converting Umlauts and other non-Latin letters of the alphabets to Latin). Transliteration would be a nice feature (in such cases, I could fall back to Ajax to let Iconv do this).

Does anyone know how to do this using JavaScript? My current code works fine, but has problems with a few spaces, and Tête-à-têteit becomes Tte--ttethat just ugly.

+3
source share
3 answers

When I needed it, I used the javascript implementation of Django for this, which covers most of this and even more :)

Here you can find: https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/static/admin/js/urlify.js

+9
source

urlify for node.js npm package https://npmjs.org/package/parameterize

0
source

JS URL-. , . , , "-", "-".

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, ''));
    }

:

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, ''));}

.

0

Source: https://habr.com/ru/post/1729794/


All Articles