Created
June 17, 2013 06:51
-
-
Save merkuriy/5795047 to your computer and use it in GitHub Desktop.
Converting string to the "Usability URL" format.
This file contains 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() { | |
var | |
$fldName = $('input[name=name]'), | |
$fldURI = $('input[name=uri]'); | |
function updateName () { | |
$fldName.val(TransNameUri($fldIn1.val())); | |
} | |
$fldIn1.on('change keypress keydown', function () {setTimeout(updateName, 2)}); | |
updateName(); | |
}); |
This file contains 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
TransNameUri = function (window, undefined) { | |
var | |
dict = dict = {"\u0430":"a","\u0431":"b","\u0432":"v","\u0433":"g","\u0434":"d","\u0435":"e","\u0451":"yo","\u0436":"j","\u0437":"z","\u0438":"i","\u0439":"y","\u043a":"k","\u043b":"l","\u043c":"m","\u043d":"n","\u043e":"o","\u043f":"p","\u0440":"r","\u0441":"s","\u0442":"t","\u0443":"u","\u0444":"f","\u0445":"h","\u0446":"c","\u0447":"ch","\u0448":"sh","\u0449":"sch","\u044a":"","\u044b":"yi","\u044c":"","\u044d":"e","\u044e":"yu","\u044f":"ya"}, | |
reg1 = /[^0-9a-z\u0430-\u044f\u0451]+/g, | |
reg2 = /[\u0430-\u044f\u0451]/g, | |
defLimit = 50; | |
function replace2 (char) { | |
return dict[char]; | |
} | |
function translate (str, limit) { | |
if (!str) return ''; | |
limit = limit || defLimit; | |
// replace | |
str = str.toLowerCase().replace(reg1, '-').replace(reg2, replace2); | |
// trim | |
str = str.slice((str[0] == '-' ? 1: 0), (str[str.length - 1] == '-' ? -1: str.length)); | |
// limiter | |
return (str.length > limit) ? str.slice(0, str.lastIndexOf('-', limit)) : str; | |
} | |
return translate; | |
}(window, undefined); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment