|
1 | 1 | import { partial_ratio } from "fuzzball"; |
2 | 2 |
|
3 | | -import { SearchFilter } from "#scripts/search-filter.svelte"; |
| 3 | +import { SearchFilter, type FilterResults } from "#scripts/search-filter.svelte"; |
4 | 4 | import { any, all, get_enabled } from "#scripts/utils"; |
5 | 5 |
|
6 | 6 | import { Lang, Tool, Flavour, Kind, State } from "./projects"; |
@@ -72,8 +72,22 @@ export class ProjectSearchFilter extends SearchFilter<ProjectData> |
72 | 72 | } |
73 | 73 |
|
74 | 74 |
|
75 | | - apply(projects: ProjectData[]): ProjectData[] |
| 75 | + apply(projects: ProjectData[]): FilterResults<ProjectData> |
76 | 76 | { |
| 77 | + let out: FilterResults<ProjectData> = this.#filter(projects); |
| 78 | + |
| 79 | + if (this.group_by !== "default") { |
| 80 | + out = this.#group_and_sort(out); |
| 81 | + } |
| 82 | + else if (this.query) { |
| 83 | + out = this.#sort(out); |
| 84 | + } |
| 85 | + |
| 86 | + return out; |
| 87 | + } |
| 88 | + |
| 89 | + #filter(projects: ProjectData[]): ProjectData[] |
| 90 | + { |
77 | 91 | let out = projects.filter( |
78 | 92 | proj => { |
79 | 93 | proj._score_ = 0; |
@@ -115,18 +129,28 @@ export class ProjectSearchFilter extends SearchFilter<ProjectData> |
115 | 129 | out = projects; |
116 | 130 | } |
117 | 131 |
|
118 | | - if (this.query) { |
119 | | - out = super.sort(out, |
120 | | - proj => Math.max( |
121 | | - partial_ratio(this.query, proj.name), |
122 | | - partial_ratio(this.query, proj.tech.join(" ")), |
123 | | - proj.desc ? partial_ratio(this.query, proj.desc) : 0, |
124 | | - proj.tech ? partial_ratio(this.query, proj.tech.join(" ")) : 0, |
125 | | - proj.tags ? partial_ratio(this.query, proj.tags.join(" ")) : 0, |
126 | | - ) |
127 | | - ); |
128 | | - } |
129 | | - |
130 | 132 | return out; |
131 | 133 | } |
| 134 | + |
| 135 | + #group_and_sort(projects: ProjectData[]): [string, ProjectData[]][] |
| 136 | + { |
| 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 | + ); |
| 155 | + } |
132 | 156 | } |
0 commit comments