use typescript
This commit is contained in:
78
src/model.ts
Normal file
78
src/model.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { ring } from "./config"
|
||||
|
||||
export type ItemAttributes = {
|
||||
name: string
|
||||
ring: ring
|
||||
quadrant: string
|
||||
title: string
|
||||
featured: boolean
|
||||
}
|
||||
|
||||
export type Item = ItemAttributes & {
|
||||
featured: boolean
|
||||
body: string
|
||||
info: string
|
||||
flag: 'new' | 'changed' | 'default'
|
||||
revisions: Revision[]
|
||||
}
|
||||
|
||||
export type Revision = ItemAttributes & {
|
||||
body: string
|
||||
fileName: string
|
||||
release: string
|
||||
}
|
||||
|
||||
export type Quadrant = {
|
||||
[name: string]: Item[]
|
||||
}
|
||||
|
||||
export type Radar = {
|
||||
items: Item[]
|
||||
releases: string[]
|
||||
}
|
||||
|
||||
export type Group = {
|
||||
[quadrant: string]: Quadrant
|
||||
}
|
||||
|
||||
export const featuredOnly = (items: Item[]) => items.filter(item => item.featured);
|
||||
|
||||
export const groupByQuadrants = (items: Item[]): Group =>
|
||||
items.reduce(
|
||||
(quadrants, item: Item) => ({
|
||||
...quadrants,
|
||||
[item.quadrant]: addItemToQuadrant(quadrants[item.quadrant], item),
|
||||
}),
|
||||
{} as {[k: string]: Quadrant},
|
||||
);
|
||||
|
||||
export const groupByFirstLetter = (items: Item[]) => {
|
||||
const index = items.reduce(
|
||||
(letterIndex, item) => ({
|
||||
...letterIndex,
|
||||
[getFirstLetter(item)]: addItemToList(
|
||||
letterIndex[getFirstLetter(item)],
|
||||
item,
|
||||
),
|
||||
}),
|
||||
{} as {[k: string]: Item[]},
|
||||
);
|
||||
|
||||
return Object.keys(index)
|
||||
.sort()
|
||||
.map(letter => ({
|
||||
letter,
|
||||
items: index[letter],
|
||||
}));
|
||||
};
|
||||
|
||||
const addItemToQuadrant = (quadrant: Quadrant = {}, item: Item): Quadrant => ({
|
||||
...quadrant,
|
||||
[item.ring]: addItemToRing(quadrant[item.ring], item),
|
||||
});
|
||||
|
||||
const addItemToList = (list: Item[] = [], item: Item) => [...list, item];
|
||||
|
||||
const addItemToRing = (ring: Item[] = [], item: Item) => [...ring, item];
|
||||
|
||||
export const getFirstLetter = (item: Item) => item.title.substr(0, 1).toUpperCase();
|
||||
Reference in New Issue
Block a user