webapp/src/utils/groupby.ts

40 lines
835 B
TypeScript
Raw Normal View History

2022-10-07 19:35:53 +00:00
import type { Author, Shout, Topic } from '../graphql/types.gen'
export const groupByName = (arr: Author[]) => {
2022-09-09 11:53:35 +00:00
return arr.reduce(
(acc, tt) => {
let c = (tt.name || '')
.replace(/[^\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': []
}
)
}
2022-10-07 19:35:53 +00:00
export const groupByTitle = (arr: (Shout | Topic)[]) => {
2022-09-09 11:53:35 +00:00
return arr.reduce(
(acc, tt) => {
let c = (tt.title || '')
.replace(/[^\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': []
}
)
}