-
-
Save kimtrien/21b071cb1034ac364d7dded635b3f8bf to your computer and use it in GitHub Desktop.
str_slug js fuction export from laravel framework
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 trim(str, charlist) { | |
// example 1: trim(' Kevin van Zonneveld ') | |
// returns 1: 'Kevin van Zonneveld' | |
// example 2: trim('Hello World', 'Hdle') | |
// returns 2: 'o Wor' | |
// example 3: trim(16, 1) | |
// returns 3: '6' | |
let whitespace = [ | |
' ', | |
'\n', | |
'\r', | |
'\t', | |
'\f', | |
'\x0b', | |
'\xa0', | |
'\u2000', | |
'\u2001', | |
'\u2002', | |
'\u2003', | |
'\u2004', | |
'\u2005', | |
'\u2006', | |
'\u2007', | |
'\u2008', | |
'\u2009', | |
'\u200a', | |
'\u200b', | |
'\u2028', | |
'\u2029', | |
'\u3000' | |
].join(''); | |
str += ''; | |
if (charlist) { | |
whitespace = (charlist + '').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^:])/g, '$1'); | |
} | |
let l = str.length; | |
let i; | |
for (i = 0; i < l; i++) { | |
if (whitespace.indexOf(str.charAt(i)) === -1) { | |
str = str.substring(i); | |
break; | |
} | |
} | |
l = str.length; | |
for (i = l - 1; i >= 0; i--) { | |
if (whitespace.indexOf(str.charAt(i)) === -1) { | |
str = str.substring(0, i + 1); | |
break; | |
} | |
} | |
return whitespace.indexOf(str.charAt(0)) === -1 ? str : ''; | |
} | |
function pregQuote(str, delimiter) { | |
// example 1: pregQuote("$40") | |
// returns 1: '\\$40' | |
// example 2: pregQuote("*RRRING* Hello?") | |
// returns 2: '\\*RRRING\\* Hello\\?' | |
// example 3: pregQuote("\\.+*?[^]$(){}=!<>|:") | |
// returns 3: '\\\\\\.\\+\\*\\?\\[\\^\\]\\$\\(\\)\\{\\}\\=\\!\\<\\>\\|\\:' | |
return (str + '') | |
.replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&'); | |
} | |
function strSlug($title, $separator = '-') { | |
if (!$title)return ''; | |
$title = $title.toLocaleLowerCase(); | |
const charsArray = { | |
'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': ['ц', 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H': ['Ч'], | |
'DJ': ['Ђ'], | |
'DZ': ['Џ'], | |
'GX': ['Ĝ'], | |
'HX': ['Ĥ'], | |
'IJ': ['IJ'], | |
'JX': ['Ĵ'], | |
'KH': ['Х'], | |
'LJ': ['Љ'], | |
'NJ': ['Њ'], | |
'OE': ['Ö', 'Œ'], | |
'PS': ['Ψ'], | |
'SH': ['Ш'], | |
'SHCH': ['Щ'], | |
'SS': ['ẞ'], | |
'TH': ['Þ'], | |
'TS': ['Ц'], | |
'UE': ['Ü'], | |
'YA': ['Я'], | |
'YU': ['Ю'], | |
'ZH': ['Ж'], | |
' ': ['\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'], | |
}; | |
for (let $key in charsArray) { | |
$title = $title.replace(new RegExp(`${[].concat(charsArray[$key]).join('|')}`, 'gi'), $key); | |
} | |
$title = $title.replace(/[^\x20-\x7E]/giu, ''); | |
// Convert all dashes/underscores into separator | |
const $flip = $separator == '-' ? '_' : '-'; | |
$title = $title.replace(new RegExp('[' + pregQuote($flip) + ']+', 'g'), $separator); | |
// Remove all characters that are not the separator, letters, numbers, or whitespace. | |
$title = $title.replace(new RegExp('[^' + pregQuote($separator) + 'a-z0-9\\s]+', 'gi'), $separator); | |
// Replace all separator characters and whitespace by a single separator | |
$title = $title.replace(new RegExp('[' + pregQuote($separator) + '\\s]+', 'g'), $separator); | |
return trim($title, $separator); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment