webapp/src/utils/dummyFilter.ts
Kosta 85e8533931
dates in notifications, lots of minor fixes (#271)
* dates in notifications, lots of minor fixes
Co-authored-by: Igor Lobanov <igor.lobanov@onetwotrip.com>
2023-10-18 13:56:41 +03:00

44 lines
1.2 KiB
TypeScript

import { translit } from './ru2en'
import { Author, Topic } from '../graphql/types.gen'
import { isAuthor } from './isAuthor'
const prepareQuery = (searchQuery, lang) => {
const q = searchQuery.toLowerCase()
if (q.length === 0) return ''
return lang === 'ru' ? translit(q) : q
}
const stringMatches = (str, q, lang) => {
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) => {
const slugMatches = item.slug && 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
})
}