Skip to content

Commit

Permalink
chore: update config
Browse files Browse the repository at this point in the history
  • Loading branch information
tuanductran authored Dec 15, 2024
1 parent edee7fd commit ef39fff
Showing 1 changed file with 25 additions and 81 deletions.
106 changes: 25 additions & 81 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,30 @@
export function slugify(str: string): string {
let formattedSlug = str.toLowerCase()
export function slugify(input: string): string {
if (typeof input !== 'string' || input.trim() === '') return '';

const accentMapping: { [key: string]: string } = {
á: 'a',
à: 'a',
ă: 'a',
ắ: 'a',
ằ: 'a',
ẵ: 'a',
ẳ: 'a',
â: 'a',
ấ: 'a',
ầ: 'a',
ẫ: 'a',
ẩ: 'a',
ã: 'a',
ả: 'a',
ạ: 'a',
ặ: 'a',
ậ: 'a',
đ: 'd',
é: 'e',
è: 'e',
ê: 'e',
ế: 'e',
ề: 'e',
ễ: 'e',
ể: 'e',
ẽ: 'e',
ẻ: 'e',
ẹ: 'e',
ệ: 'e',
í: 'i',
ì: 'i',
ĩ: 'i',
ỉ: 'i',
ị: 'i',
ó: 'o',
ò: 'o',
ô: 'o',
ố: 'o',
ồ: 'o',
ỗ: 'o',
ổ: 'o',
õ: 'o',
ỏ: 'o',
ơ: 'o',
ớ: 'o',
ờ: 'o',
ỡ: 'o',
ở: 'o',
ợ: 'o',
ọ: 'o',
ộ: 'o',
ú: 'u',
ù: 'u',
ũ: 'u',
ủ: 'u',
ư: 'u',
ứ: 'u',
ừ: 'u',
ữ: 'u',
ử: 'u',
ự: 'u',
ụ: 'u',
ý: 'y',
ỳ: 'y',
ỹ: 'y',
ỷ: 'y',
ỵ: 'y',
}
const accentMapping = new Map<string, string>([
['á', 'a'], ['à', 'a'], ['ă', 'a'], ['ắ', 'a'], ['ằ', 'a'], ['ẵ', 'a'], ['ẳ', 'a'],
['â', 'a'], ['ấ', 'a'], ['ầ', 'a'], ['ẫ', 'a'], ['ẩ', 'a'], ['ã', 'a'], ['ả', 'a'],
['ạ', 'a'], ['ặ', 'a'], ['ậ', 'a'], ['đ', 'd'], ['é', 'e'], ['è', 'e'], ['ê', 'e'],
['ế', 'e'], ['ề', 'e'], ['ễ', 'e'], ['ể', 'e'], ['ẽ', 'e'], ['ẻ', 'e'], ['ẹ', 'e'],
['ệ', 'e'], ['í', 'i'], ['ì', 'i'], ['ĩ', 'i'], ['ỉ', 'i'], ['ị', 'i'], ['ó', 'o'],
['ò', 'o'], ['ô', 'o'], ['ố', 'o'], ['ồ', 'o'], ['ỗ', 'o'], ['ổ', 'o'], ['õ', 'o'],
['ỏ', 'o'], ['ơ', 'o'], ['ớ', 'o'], ['ờ', 'o'], ['ỡ', 'o'], ['ở', 'o'], ['ợ', 'o'],
['ọ', 'o'], ['ộ', 'o'], ['ú', 'u'], ['ù', 'u'], ['ũ', 'u'], ['ủ', 'u'], ['ư', 'u'],
['ứ', 'u'], ['ừ', 'u'], ['ữ', 'u'], ['ử', 'u'], ['ự', 'u'], ['ụ', 'u'], ['ý', 'y'],
['ỳ', 'y'], ['ỹ', 'y'], ['ỷ', 'y'], ['ỵ', 'y']
]);

for (const [accentedChar, replacement] of Object.entries(accentMapping)) {
const regex = new RegExp(accentedChar, 'g')
formattedSlug = formattedSlug.replace(regex, replacement)
}
let slug = input.toLowerCase();

formattedSlug = formattedSlug
.replaceAll(/[^0-9a-z]+/g, '-')
.replaceAll(/\s+/g, '-')
.replaceAll(/-+/g, '-')
.replaceAll(/^-+|-+$/g, '')
slug = slug
.split('')
.map(char => accentMapping.get(char) || char)
.join('');

return formattedSlug
slug = slug
.replace(/[^0-9a-z]+/g, '-')
.replace(/-+/g, '-')
.replace(/^-+|-+$/g, '');

return slug;
}

0 comments on commit ef39fff

Please sign in to comment.