-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathfilters.js
More file actions
325 lines (294 loc) · 7.96 KB
/
filters.js
File metadata and controls
325 lines (294 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Import Third-party Dependencies
import semver from "semver";
import sizeSatisfies from "@nodesecure/size-satisfies";
import { getManifestEmoji } from "@nodesecure/flags/web";
// CONSTANTS
export const FLAG_LIST = [...getManifestEmoji()].map(([name, emoji]) => {
return {
emoji,
name
};
});
export const SIZE_PRESETS = [
{ label: "< 10 kb", value: "<10kb" },
{ label: "10 – 100 kb", value: "10kb..100kb" },
{ label: "> 100 kb", value: ">100kb" }
];
export const VERSION_PRESETS = [
{ label: "0.x", value: "0.x" },
{ label: "≥ 1.0", value: ">=1.0.0" },
{ label: "< 1.0", value: "<1.0.0" }
];
export const FILTERS_NAME = new Set([
"package",
"version",
"flag",
"license",
"author",
"ext",
"builtin",
"size",
"highlighted",
"dep"
]);
export const PRESETS = [
{ id: "has_vulnerabilities", filter: "flag", value: "hasVulnerabilities" },
{ id: "has_scripts", filter: "flag", value: "hasScript" },
{ id: "no_license", filter: "flag", value: "hasNoLicense" },
{ id: "deprecated", filter: "flag", value: "isDeprecated" },
{ id: "large", filter: "size", value: ">100kb" }
];
// Filters that use a searchable text-based list (not a rich visual panel)
export const FILTER_HAS_HELPERS = new Set(["license", "ext", "builtin", "author", "dep"]);
// Filters where the mode persists after selection (multi-select)
export const FILTER_MULTI_SELECT = new Set(["flag"]);
// Filters that auto-confirm immediately on selection (no text input needed)
export const FILTER_INSTANT_CONFIRM = new Set(["highlighted"]);
/**
* Returns per-flag package counts across the full linker.
*
* @param {Map<number, object>} linker
* @returns {Map<string, number>}
*/
export function getFlagCounts(linker) {
const counts = new Map();
for (const { flags } of linker.values()) {
for (const flag of flags) {
counts.set(flag, (counts.get(flag) ?? 0) + 1);
}
}
return counts;
}
/**
* Returns per-value package counts for list-type filters.
*
* @param {Map<number, object>} linker
* @param {string} filterName
* @returns {Map<string, number>}
*/
export function getFilterValueCounts(linker, filterName) {
if (filterName === "dep") {
const counts = new Map();
for (const opt of linker.values()) {
const dependentCount = Object.keys(opt.usedBy).length;
if (dependentCount > 0) {
counts.set(opt.name, (counts.get(opt.name) ?? 0) + dependentCount);
}
}
return counts;
}
const counts = new Map();
for (const opt of linker.values()) {
for (const value of getValuesForCount(opt, filterName)) {
counts.set(value, (counts.get(value) ?? 0) + 1);
}
}
return counts;
}
function getValuesForCount(opt, filterName) {
switch (filterName) {
case "license":
return opt.uniqueLicenseIds ?? [];
case "ext":
return opt.composition.extensions.filter((ext) => ext !== "");
case "builtin":
return opt.composition.required_nodejs;
case "author": {
if (opt.author === null) {
return [];
}
const name = typeof opt.author === "string" ? opt.author : opt.author?.name;
return name ? [name] : [];
}
default:
return [];
}
}
/**
* @param {Map<number, object>} linker
* @param {string} filterName
* @returns {{ display: string, value: string }[]}
*/
export function getHelperValues(linker, filterName) {
switch (filterName) {
case "license": {
const items = new Set();
for (const { uniqueLicenseIds = [] } of linker.values()) {
for (const id of uniqueLicenseIds) {
items.add(id);
}
}
return [...items].map((licenseId) => {
return { display: licenseId, value: licenseId };
});
}
case "ext": {
const items = new Set();
for (const { composition } of linker.values()) {
for (const ext of composition.extensions) {
items.add(ext);
}
}
items.delete("");
return [...items].map((ext) => {
return { display: ext, value: ext };
});
}
case "builtin": {
const items = new Set();
for (const { composition } of linker.values()) {
for (const module of composition.required_nodejs) {
items.add(module);
}
}
return [...items].map((module) => {
return { display: module, value: module };
});
}
case "author": {
const items = new Set();
for (const { author } of linker.values()) {
if (author === null) {
continue;
}
if (typeof author === "string") {
items.add(author);
}
else if (Object.hasOwn(author, "name")) {
items.add(author.name);
}
}
return [...items].map((name) => {
return { display: name, value: name };
});
}
case "dep": {
const items = new Set();
for (const { name } of linker.values()) {
items.add(name);
}
return [...items].sort().map((name) => {
return { display: name, value: name };
});
}
default:
return [];
}
}
/**
* Returns the Set of package IDs (as strings) matching the given filter+value.
*
* @param {Map<number, object>} linker
* @param {string} filterName
* @param {string} inputValue
* @returns {Set<string>}
*/
export function computeMatches(linker, filterName, inputValue) {
if (filterName === "dep") {
return computeDepMatches(linker, inputValue);
}
const matchingIds = new Set();
for (const [id, opt] of linker) {
if (matchesFilter(opt, filterName, inputValue)) {
matchingIds.add(String(id));
}
}
return matchingIds;
}
/**
* Collect packages that depend on package matching inputValue
*
* @param {Map<number, object>} linker
* @param {string} inputValue
* @returns {Set<string>}
*/
function computeDepMatches(linker, inputValue) {
const matchingIds = new Set();
try {
const regex = new RegExp(inputValue, "i");
const dependentNames = new Set();
for (const opt of linker.values()) {
if (regex.test(opt.name)) {
for (const dependency of Object.keys(opt.usedBy)) {
dependentNames.add(dependency);
}
}
}
for (const [id, opt] of linker) {
if (dependentNames.has(opt.name)) {
matchingIds.add(String(id));
}
}
}
catch {
// invalid regex
}
return matchingIds;
}
function matchesFilter(opt, filterName, inputValue) {
switch (filterName) {
case "package": {
try {
return new RegExp(inputValue, "gi").test(opt.name);
}
catch {
return false;
}
}
case "version": {
try {
return semver.satisfies(opt.version, inputValue);
}
catch {
return false;
}
}
case "license": {
try {
const regex = new RegExp(inputValue, "gi");
return opt.uniqueLicenseIds.some((licenseId) => regex.test(licenseId));
}
catch {
return false;
}
}
case "ext": {
const extensions = new Set(opt.composition.extensions);
const wanted = inputValue.startsWith(".") ? inputValue : `.${inputValue}`;
return extensions.has(wanted.toLowerCase());
}
case "size": {
try {
return sizeSatisfies(inputValue, opt.size);
}
catch {
return false;
}
}
case "builtin": {
try {
const regex = new RegExp(inputValue, "gi");
return opt.composition.required_nodejs.some((mod) => regex.test(mod));
}
catch {
return false;
}
}
case "author": {
try {
const regex = new RegExp(inputValue, "gi");
return (typeof opt.author === "string" && regex.test(opt.author)) ||
(opt.author !== null && Object.hasOwn(opt.author, "name") && regex.test(opt.author.name));
}
catch {
return false;
}
}
case "flag":
return opt.flags.includes(inputValue);
case "highlighted":
return inputValue === "all" ? opt.isHighlighted === true : opt.isHighlighted !== true;
default:
return false;
}
}