webapp/src/utils/groupby.ts
Untone bd1b0025e7
Some checks failed
deploy / test (push) Failing after 1m43s
deploy / deploy (push) Has been skipped
types-fixed
2023-12-24 16:08:04 +03:00

40 lines
851 B
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 type { Author, Shout, Topic } from '../graphql/schema/core.gen'
export const groupByName = (arr: Author[]) => {
return arr.reduce(
(acc, tt) => {
let c = (tt.name || '')
.replaceAll(/[^\d A-Za-zА-я]/g, '')
.split(' ')
.pop()
.slice(0, 1)
.toUpperCase()
if (/[^А-я]/.test(c)) c = 'A-Z'
else if (!acc[c]) acc[c] = []
acc[c].push(tt)
return acc
},
{
'A-Z': [],
},
)
}
export const groupByTitle = (arr: (Shout | Topic)[]) => {
return arr.reduce(
(acc, tt) => {
let c = (tt.title || '')
.replaceAll(/[^\d A-Za-zА-я]/g, '')
.slice(0, 1)
.toUpperCase()
if (/[^А-я]/.test(c)) c = 'A-Z'
else if (!acc[c]) acc[c] = []
acc[c].push(tt)
return acc
},
{
'A-Z': [],
},
)
}