Last active
July 6, 2021 08:48
-
-
Save drozdzynski/00da736362720932f86526273a3b8672 to your computer and use it in GitHub Desktop.
Laravel 5 slug function port to JavaScript
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
export default class Str { | |
static slug(value, sep) { | |
var separator = sep || '-'; | |
value = Str.ascii(value); | |
var flip = separator == '-' ? '_' : '-'; | |
value = value.replace(new RegExp(Str.pregQuote(flip), 'g'), separator); | |
value = value.toLowerCase().replace(new RegExp('[^' + Str.pregQuote(separator) + '\\d\\w\\s]', 'ug'), ''); | |
value = value.replace(new RegExp('[' + Str.pregQuote(separator) + '\\s]+', 'ug'), separator); | |
return value.trim(); | |
} | |
static ascii(value) { | |
_.forEach(Str.charsArray(), (val, key) => { | |
value = Str.replaceArray(value, val, key); | |
}); | |
return value.replace(/[^\x20-\x7E]/u, ''); | |
} | |
static pregQuote(str, delimiter) { | |
return (str + '').replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&'); | |
} | |
static replaceArray(string, find, replace) { | |
var replaceString = string; | |
for (var i = 0; i < find.length; i++) { | |
replaceString = replaceString.replace(new RegExp(find[i], 'g'), replace); | |
} | |
return replaceString; | |
}; | |
static charsArray() { | |
return { | |
'0' : ['°', '₀', '۰'], | |
'1' : ['¹', '₁', '۱'], | |
'2' : ['²', '₂', '۲'], | |
'3' : ['³', '₃', '۳'], | |
'4' : ['⁴', '₄', '۴', '٤'], | |
'5' : ['⁵', '₅', '۵', '٥'], | |
'6' : ['⁶', '₆', '۶', '٦'], | |
'7' : ['⁷', '₇', '۷'], | |
'8' : ['⁸', '₈', '۸'], | |
'9' : ['⁹', '₉', '۹'], | |
'a' : ['à', 'á', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ằ', 'ẳ', 'ẵ', 'ặ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'ā', 'ą', 'å', 'α', 'ά', 'ἀ', 'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'а', 'أ', 'အ', 'ာ', 'ါ', 'ǻ', 'ǎ', 'ª', 'ა', 'अ', 'ا'], | |
'b' : ['б', 'β', 'Ъ', 'Ь', 'ب', 'ဗ', 'ბ'], | |
'c' : ['ç', 'ć', 'č', 'ĉ', 'ċ'], | |
'd' : ['ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д', 'δ', 'د', 'ض', 'ဍ', 'ဒ', 'დ'], | |
'e' : ['é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ', 'ệ', 'ë', 'ē', 'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ', 'ἐ', 'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', 'έ', 'е', 'ё', 'э', 'є', 'ə', 'ဧ', 'ေ', 'ဲ', 'ე', 'ए', 'إ', 'ئ'], | |
'f' : ['ф', 'φ', 'ف', 'ƒ', 'ფ'], | |
'g' : ['ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ဂ', 'გ', 'گ'], | |
'h' : ['ĥ', 'ħ', 'η', 'ή', 'ح', 'ه', 'ဟ', 'ှ', 'ჰ'], | |
'i' : ['í', 'ì', 'ỉ', 'ĩ', 'ị', 'î', 'ï', 'ī', 'ĭ', 'į', 'ı', 'ι', 'ί', 'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ', 'ἵ', 'ἶ', 'ἷ', 'ὶ', 'ί', 'ῐ', 'ῑ', 'ῒ', 'ΐ', 'ῖ', 'ῗ', 'і', 'ї', 'и', 'ဣ', 'ိ', 'ီ', 'ည်', 'ǐ', 'ი', 'इ'], | |
'j' : ['ĵ', 'ј', 'Ј', 'ჯ', 'ج'], | |
'k' : ['ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك', 'က', 'კ', 'ქ', 'ک'], | |
'l' : ['ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل', 'လ', 'ლ'], | |
'm' : ['м', 'μ', 'م', 'မ', 'მ'], | |
'n' : ['ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن', 'န', 'ნ'], | |
'o' : ['ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'ø', 'ō', 'ő', 'ŏ', 'ο', 'ὀ', 'ὁ', 'ὂ', 'ὃ', 'ὄ', 'ὅ', 'ὸ', 'ό', 'о', 'و', 'θ', 'ို', 'ǒ', 'ǿ', 'º', 'ო', 'ओ'], | |
'p' : ['п', 'π', 'ပ', 'პ', 'پ'], | |
'q' : ['ყ'], | |
'r' : ['ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر', 'რ'], | |
's' : ['ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص', 'စ', 'ſ', 'ს'], | |
't' : ['ť', 'ţ', 'т', 'τ', 'ț', 'ت', 'ط', 'ဋ', 'တ', 'ŧ', 'თ', 'ტ'], | |
'u' : ['ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'ứ', 'ừ', 'ử', 'ữ', 'ự', 'û', 'ū', 'ů', 'ű', 'ŭ', 'ų', 'µ', 'у', 'ဉ', 'ု', 'ူ', 'ǔ', 'ǖ', 'ǘ', 'ǚ', 'ǜ', 'უ', 'उ'], | |
'v' : ['в', 'ვ', 'ϐ'], | |
'w' : ['ŵ', 'ω', 'ώ', 'ဝ', 'ွ'], | |
'x' : ['χ', 'ξ'], | |
'y' : ['ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'ÿ', 'ŷ', 'й', 'ы', 'υ', 'ϋ', 'ύ', 'ΰ', 'ي', 'ယ'], | |
'z' : ['ź', 'ž', 'ż', 'з', 'ζ', 'ز', 'ဇ', 'ზ'], | |
'aa' : ['ع', 'आ', 'آ'], | |
'ae' : ['ä', 'æ', 'ǽ'], | |
'ai' : ['ऐ'], | |
'at' : ['@'], | |
'ch' : ['ч', 'ჩ', 'ჭ', 'چ'], | |
'dj' : ['ђ', 'đ'], | |
'dz' : ['џ', 'ძ'], | |
'ei' : ['ऍ'], | |
'gh' : ['غ', 'ღ'], | |
'ii' : ['ई'], | |
'ij' : ['ij'], | |
'kh' : ['х', 'خ', 'ხ'], | |
'lj' : ['љ'], | |
'nj' : ['њ'], | |
'oe' : ['ö', 'œ', 'ؤ'], | |
'oi' : ['ऑ'], | |
'oii' : ['ऒ'], | |
'ps' : ['ψ'], | |
'sh' : ['ш', 'შ', 'ش'], | |
'shch' : ['щ'], | |
'ss' : ['ß'], | |
'sx' : ['ŝ'], | |
'th' : ['þ', 'ϑ', 'ث', 'ذ', 'ظ'], | |
'ts' : ['ц', 'ც', 'წ'], | |
'ue' : ['ü'], | |
'uu' : ['ऊ'], | |
'ya' : ['я'], | |
'yu' : ['ю'], | |
'zh' : ['ж', 'ჟ', 'ژ'], | |
'(c)' : ['©'], | |
'A' : ['Á', 'À', 'Ả', 'Ã', 'Ạ', 'Ă', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ', 'Ặ', 'Â', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'Å', 'Ā', 'Ą', 'Α', 'Ά', 'Ἀ', 'Ἁ', 'Ἂ', 'Ἃ', 'Ἄ', 'Ἅ', 'Ἆ', 'Ἇ', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ', 'ᾍ',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: ["\xC2\xA0", "\xE2\x80\x80", "\xE2\x80\x81", "\xE2\x80\x82", "\xE2\x80\x83", "\xE2\x80\x84", "\xE2\x80\x85", "\xE2\x80\x86", "\xE2\x80\x87", "\xE2\x80\x88", "\xE2\x80\x89", "\xE2\x80\x8A", "\xE2\x80\xAF", "\xE2\x81\x9F", "\xE3\x80\x80"], | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i think this doesn't cover asian languages.
ex. "창업을 위한 기초 경영"