webapp/src/utils/translate.ts
Untone 333d7c5961
Some checks failed
deploy / test (push) Failing after 59s
deploy / Update templates on Mailgun (push) Has been skipped
no-actions-context-value
2024-02-04 20:40:15 +03:00

40 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Author } from '../graphql/schema/core.gen'
import { capitalize } from './capitalize'
import { translit } from './ru2en'
export const isCyrillic = (s: string): boolean => {
const cyrillicRegex = /[\u0400-\u04FF]/ // Range for Cyrillic characters
return cyrillicRegex.test(s)
}
export const translateAuthor = (author: Author, lng: string) =>
lng === 'en' && isCyrillic(author.name)
? capitalize(translit(author.name.replace(/ё/, 'e').replace(/ь/, '')).replace(/-/, ' '), true)
: author.name
export const authorLetterReduce = (acc, author: Author, lng: string) => {
let letter = ''
if (!letter && author && author.name) {
const name = translateAuthor(author, lng)
.replace(/[^\dA-zА-я]/, ' ')
.trim()
const nameParts = name.trim().split(' ')
const found = nameParts.filter(Boolean).pop()
if (found && found.length > 0) {
letter = found[0].toUpperCase()
}
}
if (/[^ËА-яё]/.test(letter) && lng === 'ru') letter = '@'
if (/[^A-z]/.test(letter) && lng === 'en') letter = '@'
if (!acc[letter]) acc[letter] = []
author.name = translateAuthor(author, lng)
acc[letter].push(author)
// Sort authors within each letter group alphabetically by name
acc[letter].sort((a, b) => a.name.localeCompare(b.name))
return acc
}