Skip to content

Commit 8ba6acb

Browse files
committed
sup
1 parent a9a7657 commit 8ba6acb

2 files changed

Lines changed: 53 additions & 23 deletions

File tree

src/routes/(sup)/sup/projects/filter.svelte.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,31 @@ export class ProjectSearchFilter extends SearchFilter<ProjectData>
7171
];
7272
}
7373

74+
project_scorer(proj: ProjectData): number
75+
{
76+
return Math.max(
77+
partial_ratio(this.query, proj.name),
78+
partial_ratio(this.query, proj.tech.join(" ")),
79+
proj.desc ? partial_ratio(this.query, proj.desc) : 0,
80+
proj.tech ? partial_ratio(this.query, proj.tech.join(" ")) : 0,
81+
proj.tags ? partial_ratio(this.query, proj.tags.join(" ")) : 0,
82+
);
83+
}
84+
85+
group_scorer(group: string, projects: ProjectData[]): number
86+
{
87+
if (this.query) {
88+
return (
89+
projects
90+
.map(proj => proj._score_ ?? 0)
91+
.reduce((acc, n) => acc + n, 0)
92+
);
93+
}
94+
else {
95+
return projects.length;
96+
}
97+
}
98+
7499

75100
apply(projects: ProjectData[]): FilterResults<ProjectData>
76101
{
@@ -80,7 +105,7 @@ export class ProjectSearchFilter extends SearchFilter<ProjectData>
80105
out = this.#group_and_sort(out);
81106
}
82107
else if (this.query) {
83-
out = this.#sort(out);
108+
out = super.sort(out, this.project_scorer);
84109
}
85110

86111
return out;
@@ -134,23 +159,13 @@ export class ProjectSearchFilter extends SearchFilter<ProjectData>
134159

135160
#group_and_sort(projects: ProjectData[]): [string, ProjectData[]][]
136161
{
137-
return super.group(
138-
projects,
139-
proj => proj[this.group_by],
140-
group => 1,
141-
);
142-
}
143-
144-
#sort(projects: ProjectData[]): ProjectData[]
145-
{
146-
return super.sort(projects,
147-
proj => Math.max(
148-
partial_ratio(this.query, proj.name),
149-
partial_ratio(this.query, proj.tech.join(" ")),
150-
proj.desc ? partial_ratio(this.query, proj.desc) : 0,
151-
proj.tech ? partial_ratio(this.query, proj.tech.join(" ")) : 0,
152-
proj.tags ? partial_ratio(this.query, proj.tags.join(" ")) : 0,
153-
)
154-
);
162+
return super.group(projects, {
163+
grouper: proj => {
164+
let value = proj[this.group_by];
165+
return Array.isArray(value) ? value[0] : value;
166+
},
167+
entity_scorer: this.project_scorer.bind(this),
168+
group_scorer: this.group_scorer.bind(this),
169+
});
155170
}
156171
}

src/scripts/search-filter.svelte.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,30 @@ export class SearchFilter<Entity extends Searchable>
8686
*/
8787
group(
8888
source: Entity[],
89-
grouper: (entity: Entity) => string,
90-
sorter?: (group: string) => number,
89+
options: {
90+
grouper: (entity: Entity) => string,
91+
entity_scorer?: (entity: Entity) => number,
92+
group_scorer?: (group: string, entities: Entity[]) => number,
93+
},
9194
): [string, Entity[]][]
9295
{
96+
let { grouper, entity_scorer, group_scorer } = options;
97+
9398
let groups = Object.groupBy(source, grouper) as Groups<Entity>;
9499
let out = Object.entries(groups);
100+
101+
// TODO: reverse?
102+
if (entity_scorer) {
103+
out = out.map(
104+
([group, entity]) => [group, entity.toSorted(
105+
(e1, e2) => entity_scorer(e2) - entity_scorer(e1)
106+
)],
107+
this
108+
);
109+
}
95110

96-
if (sorter) {
97-
out.sort(([group1, _], [group2, __]) => sorter(group1) - sorter(group2));
111+
if (group_scorer) {
112+
out.sort(([g1, e1], [g2, e2]) => group_scorer(g2, e2) - group_scorer(g1, e1));
98113
}
99114

100115
if (this.reverse_group) {

0 commit comments

Comments
 (0)