Created
March 14, 2018 22:05
-
-
Save gus3inov/2e0374b461c22cf0627f0d2a3671ad43 to your computer and use it in GitHub Desktop.
String Helper
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
console.clear() | |
function StringHelper(){ | |
let self = this | |
this.lat = [ | |
'a', 'b', 'v', 'g', 'd', 'e', | |
'e', 'gh', 'z', 'i', 'y', 'k', 'l', 'm', 'n', 'o', | |
'p', 'r', 's', 't', 'u', 'f', 'h', 'c', 'ch', 'sh', | |
'sch', 'y', 'y', 'y', 'e', 'yu', 'ya' | |
] | |
this.rus = [ | |
'а', 'б', 'в', 'г', 'д', 'е', 'ё', | |
'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', | |
'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', | |
'ъ', 'ы', 'ь', 'э', 'ю', 'я' | |
] | |
function basename(path, suffix = ''){ | |
let pathReg = path.replace(/^.*[\/\\]/g, ''); | |
if (typeof(suffix) == 'string' && pathReg.substr(pathReg.length - suffix.length) == suffix) { | |
pathReg = pathReg.substr(0, pathReg.length - suffix.length); | |
} | |
return pathReg; | |
} | |
function dirname(path){ | |
let pathReg = path.replace(/^.*[\/\\]/g, ''), | |
pos = path.indexOf(pathReg) | |
if(pos !== false){ | |
return path.substr(0, pos) | |
} | |
return '' | |
} | |
function translit(string){ | |
let stringArr = string.split(''), | |
indexString = [], | |
index = 0, | |
translitWord = [], | |
spacePos = [] | |
while ((index = string.indexOf(' ', index + 1)) > 0) { | |
spacePos.push(index); | |
} | |
console.log(spacePos) | |
stringArr.forEach(function(elem){ | |
return self.rus.find(function(elemT, index, array){ | |
if(elem === elemT){ | |
indexString.push(index) | |
} | |
}) | |
}) | |
indexString.forEach(index =>{ | |
translitWord.push(self.lat[index]) | |
}) | |
if(spacePos !== []){ | |
} | |
return translitWord.join('') | |
} | |
return Object.freeze({ | |
translit, | |
basename, | |
dirname | |
}) | |
} | |
let helper = new StringHelper() | |
/* console.log(helper.basename('/www/site/home.htm')) | |
console.log(helper.dirname('bla/lol/www/site/home.htm')) */ | |
console.log(helper.translit('пиздец какой то')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment