webapp/src/utils/dummyFilter.ts

45 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-02-04 09:03:15 +00:00
import type { Author, Topic } from '../graphql/schema/core.gen'
import { isAuthor } from './isAuthor'
import { translit } from './ru2en'
2024-02-04 09:03:15 +00:00
const prepareQuery = (searchQuery: string, lang: string) => {
const q = searchQuery.toLowerCase()
if (q.length === 0) return ''
return lang === 'ru' ? translit(q) : q
}
2024-02-04 09:03:15 +00:00
const stringMatches = (str: string, q: string, lang: string) => {
const preparedStr = lang === 'ru' ? translit(str.toLowerCase()) : str.toLowerCase()
return preparedStr.split(' ').some((word) => word.startsWith(q))
}
export const dummyFilter = <T extends Topic | Author>(
data: T[],
searchQuery: string,
lang: 'ru' | 'en',
): T[] => {
const q = prepareQuery(searchQuery, lang)
if (q.length === 0) {
return data
}
return data.filter((item) => {
2024-02-04 09:03:15 +00:00
const slugMatches = item.slug?.split('-').some((w) => w.startsWith(q))
if (slugMatches) return true
if ('title' in item) {
return stringMatches(item.title, q, lang)
}
if (isAuthor(item)) {
return stringMatches(item.name, q, lang) || (item.bio && stringMatches(item.bio, q, lang))
}
// If it does not match any of the 'slug', 'title', 'name' , 'bio' fields
// current element should not be included in the filtered array
return false
})
}