-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathuseStructuredFilters.ts
More file actions
493 lines (428 loc) · 14.7 KB
/
useStructuredFilters.ts
File metadata and controls
493 lines (428 loc) · 14.7 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/**
* Filter pipeline and sorting logic for package lists
*/
import type { NpmSearchResult } from '#shared/types/npm-registry'
import type {
DownloadRange,
FilterChip,
SearchScope,
SecurityFilter,
SortOption,
StructuredFilters,
UpdatedWithin,
} from '#shared/types/preferences'
import {
DEFAULT_FILTERS,
DOWNLOAD_RANGES,
parseSortOption,
UPDATED_WITHIN_OPTIONS,
} from '#shared/types/preferences'
/**
* Parsed search operators from text input
*/
export interface ParsedSearchOperators {
name?: string[]
description?: string[]
keywords?: string[]
text?: string // Remaining text without operators
}
/**
* Parse search operators from text input.
* Supports: name:, desc:/description:, kw:/keyword:
* Multiple values can be comma-separated: kw:foo,bar
* Remaining text is treated as a general search term.
*
* Example: "name:react kw:typescript,hooks some text"
* Returns: { name: ['react'], keywords: ['typescript', 'hooks'], text: 'some text' }
*/
export function parseSearchOperators(input: string): ParsedSearchOperators {
const result: ParsedSearchOperators = {}
// Regex to match operators: name:value, desc:value, description:value, kw:value, keyword:value
// Value continues until whitespace or next operator
const operatorRegex = /\b(name|desc|description|kw|keyword):(\S+)/gi
let remaining = input
let match
while ((match = operatorRegex.exec(input)) !== null) {
const [fullMatch, operator, value] = match
if (!operator || !value) continue
const values = value
.split(',')
.map(v => v.trim())
.filter(Boolean)
const normalizedOp = operator.toLowerCase()
if (normalizedOp === 'name') {
result.name = [...(result.name ?? []), ...values]
} else if (normalizedOp === 'desc' || normalizedOp === 'description') {
result.description = [...(result.description ?? []), ...values]
} else if (normalizedOp === 'kw' || normalizedOp === 'keyword') {
result.keywords = [...(result.keywords ?? []), ...values]
}
// Remove matched operator from remaining text
remaining = remaining.replace(fullMatch, '')
}
// Clean up remaining text
const cleanedText = remaining.trim().replace(/\s+/g, ' ')
if (cleanedText) {
result.text = cleanedText
}
return result
}
/**
* Check if parsed operators has any content
*/
export function hasSearchOperators(parsed: ParsedSearchOperators): boolean {
return !!(parsed.name?.length || parsed.description?.length || parsed.keywords?.length)
}
interface UseStructuredFiltersOptions {
packages: Ref<NpmSearchResult[]>
searchQueryModel?: Ref<string>
initialFilters?: Partial<StructuredFilters>
initialSort?: SortOption
}
// Pure filter predicates (no closure dependencies)
function matchesKeywords(pkg: NpmSearchResult, keywords: string[]): boolean {
if (keywords.length === 0) return true
const pkgKeywords = new Set((pkg.package.keywords ?? []).map(k => k.toLowerCase()))
// AND logic: package must have ALL selected keywords (case-insensitive)
return keywords.every(k => pkgKeywords.has(k.toLowerCase()))
}
function matchesSecurity(pkg: NpmSearchResult, security: SecurityFilter): boolean {
if (security === 'all') return true
const hasWarnings = (pkg.flags?.insecure ?? 0) > 0
if (security === 'secure') return !hasWarnings
if (security === 'warnings') return hasWarnings
return true
}
/**
* Composable for structured filtering and sorting of package lists
*
*/
export function useStructuredFilters(options: UseStructuredFiltersOptions) {
const route = useRoute()
const router = useRouter()
const { packages, initialFilters, initialSort, searchQueryModel } = options
const { t } = useI18n()
const searchQuery = shallowRef(normalizeSearchParam(route.query.q))
watch(
() => route.query.q,
urlQuery => {
const value = normalizeSearchParam(urlQuery)
if (searchQuery.value !== value) {
searchQuery.value = value
}
},
)
// Filter state
const filters = ref<StructuredFilters>({
...DEFAULT_FILTERS,
...initialFilters,
})
// Sort state
const sortOption = shallowRef<SortOption>(initialSort ?? 'updated-desc')
// Available keywords extracted from all packages
const availableKeywords = computed(() => {
const keywordCounts = new Map<string, number>()
for (const pkg of packages.value) {
const keywords = pkg.package.keywords ?? []
for (const keyword of keywords) {
keywordCounts.set(keyword, (keywordCounts.get(keyword) ?? 0) + 1)
}
}
// Sort by count descending
return Array.from(keywordCounts.entries())
.sort((a, b) => b[1] - a[1])
.map(([keyword]) => keyword)
})
// Filter predicates
function matchesTextFilter(pkg: NpmSearchResult, text: string, scope: SearchScope): boolean {
if (!text) return true
const pkgName = pkg.package.name.toLowerCase()
const pkgDescription = (pkg.package.description ?? '').toLowerCase()
const pkgKeywords = (pkg.package.keywords ?? []).map(k => k.toLowerCase())
// When scope is 'all', parse and handle operators
if (scope === 'all') {
const parsed = parseSearchOperators(text)
// If operators are present, use structured matching
if (hasSearchOperators(parsed)) {
// All specified operators must match (AND logic between operator types)
// Within each operator, any value can match (OR logic within operator)
if (parsed.name?.length) {
const nameMatches = parsed.name.some(n => pkgName.includes(n.toLowerCase()))
if (!nameMatches) return false
}
if (parsed.description?.length) {
const descMatches = parsed.description.some(d => pkgDescription.includes(d.toLowerCase()))
if (!descMatches) return false
}
if (parsed.keywords?.length) {
const kwMatches = parsed.keywords.some(kw =>
pkgKeywords.some(pk => pk.includes(kw.toLowerCase())),
)
if (!kwMatches) return false
}
// If there's remaining text, it must match somewhere
if (parsed.text) {
const textLower = parsed.text.toLowerCase()
const textMatches =
pkgName.includes(textLower) ||
pkgDescription.includes(textLower) ||
pkgKeywords.some(k => k.includes(textLower))
if (!textMatches) return false
}
return true
}
// No operators - fall through to standard 'all' search
const lower = text.toLowerCase()
return (
pkgName.includes(lower) ||
pkgDescription.includes(lower) ||
pkgKeywords.some(k => k.includes(lower))
)
}
// Non-'all' scopes - simple matching
const lower = text.toLowerCase()
switch (scope) {
case 'name':
return pkgName.includes(lower)
case 'description':
return pkgDescription.includes(lower)
case 'keywords':
return pkgKeywords.some(k => k.includes(lower))
default:
return pkgName.includes(lower)
}
}
function matchesDownloadRange(pkg: NpmSearchResult, range: DownloadRange): boolean {
if (range === 'any') return true
const downloads = pkg.downloads?.weekly ?? 0
const config = DOWNLOAD_RANGES.find(r => r.value === range)
if (!config) return true
if (config.min !== undefined && downloads < config.min) return false
if (config.max !== undefined && downloads >= config.max) return false
return true
}
function matchesUpdatedWithin(pkg: NpmSearchResult, within: UpdatedWithin): boolean {
if (within === 'any') return true
const config = UPDATED_WITHIN_OPTIONS.find(o => o.value === within)
if (!config?.days) return true
const updatedDate = new Date(pkg.package.date)
const cutoff = new Date()
cutoff.setDate(cutoff.getDate() - config.days)
return updatedDate >= cutoff
}
// Apply all filters
const filteredPackages = computed(() => {
return packages.value.filter(pkg => {
if (!matchesTextFilter(pkg, filters.value.text, filters.value.searchScope)) return false
if (!matchesDownloadRange(pkg, filters.value.downloadRange)) return false
if (!matchesKeywords(pkg, filters.value.keywords)) return false
if (!matchesSecurity(pkg, filters.value.security)) return false
if (!matchesUpdatedWithin(pkg, filters.value.updatedWithin)) return false
return true
})
})
// Sort comparators
function comparePackages(a: NpmSearchResult, b: NpmSearchResult, option: SortOption): number {
const { key, direction } = parseSortOption(option)
const multiplier = direction === 'asc' ? 1 : -1
let diff: number
switch (key) {
case 'downloads-week':
diff = (a.downloads?.weekly ?? 0) - (b.downloads?.weekly ?? 0)
break
case 'downloads-day':
case 'downloads-month':
case 'downloads-year':
// Not yet implemented - fall back to weekly
diff = (a.downloads?.weekly ?? 0) - (b.downloads?.weekly ?? 0)
break
case 'updated':
diff = new Date(a.package.date).getTime() - new Date(b.package.date).getTime()
break
case 'name':
diff = a.package.name.localeCompare(b.package.name)
break
case 'quality':
diff = (a.score?.detail?.quality ?? 0) - (b.score?.detail?.quality ?? 0)
break
case 'popularity':
diff = (a.score?.detail?.popularity ?? 0) - (b.score?.detail?.popularity ?? 0)
break
case 'maintenance':
diff = (a.score?.detail?.maintenance ?? 0) - (b.score?.detail?.maintenance ?? 0)
break
case 'score':
diff = (a.score?.final ?? 0) - (b.score?.final ?? 0)
break
case 'relevance':
// Relevance preserves server order (already sorted by search relevance)
diff = 0
break
default:
diff = 0
}
return diff * multiplier
}
// Apply sorting to filtered results
const sortedPackages = computed(() => {
return [...filteredPackages.value].sort((a, b) => comparePackages(a, b, sortOption.value))
})
// i18n key mappings for filter chip values
const downloadRangeLabels = computed<Record<DownloadRange, string>>(() => ({
'any': t('filters.download_range.any'),
'lt100': t('filters.download_range.lt100'),
'100-1k': t('filters.download_range.100_1k'),
'1k-10k': t('filters.download_range.1k_10k'),
'10k-100k': t('filters.download_range.10k_100k'),
'gt100k': t('filters.download_range.gt100k'),
}))
const securityLabels = computed<Record<SecurityFilter, string>>(() => ({
all: t('filters.security_options.all'),
secure: t('filters.security_options.secure'),
warnings: t('filters.security_options.insecure'),
}))
const updatedWithinLabels = computed<Record<UpdatedWithin, string>>(() => ({
any: t('filters.updated.any'),
week: t('filters.updated.week'),
month: t('filters.updated.month'),
quarter: t('filters.updated.quarter'),
year: t('filters.updated.year'),
}))
// Active filter chips for display
const activeFilters = computed<FilterChip[]>(() => {
const chips: FilterChip[] = []
if (filters.value.text) {
chips.push({
id: 'text',
type: 'text',
label: t('filters.chips.search'),
value: filters.value.text,
})
}
if (filters.value.downloadRange !== 'any') {
chips.push({
id: 'downloadRange',
type: 'downloadRange',
label: t('filters.chips.downloads'),
value: downloadRangeLabels.value[filters.value.downloadRange],
})
}
for (const keyword of filters.value.keywords) {
chips.push({
id: `keyword-${keyword}`,
type: 'keywords',
label: t('filters.chips.keyword'),
value: keyword,
})
}
if (filters.value.security !== 'all') {
chips.push({
id: 'security',
type: 'security',
label: t('filters.chips.security'),
value: securityLabels.value[filters.value.security],
})
}
if (filters.value.updatedWithin !== 'any') {
chips.push({
id: 'updatedWithin',
type: 'updatedWithin',
label: t('filters.chips.updated'),
value: updatedWithinLabels.value[filters.value.updatedWithin],
})
}
return chips
})
// Check if any filters are active
const hasActiveFilters = computed(() => activeFilters.value.length > 0)
// Filter update helpers
function setTextFilter(text: string) {
filters.value.text = text
}
function setSearchScope(scope: SearchScope) {
filters.value.searchScope = scope
}
function setDownloadRange(range: DownloadRange) {
filters.value.downloadRange = range
}
function addKeyword(keyword: string) {
if (!filters.value.keywords.includes(keyword)) {
filters.value.keywords = [...filters.value.keywords, keyword]
const newQ = searchQuery.value
? `${searchQuery.value.trim()} keyword:${keyword}`
: `keyword:${keyword}`
router.replace({ query: { ...route.query, q: newQ } })
if (searchQueryModel) searchQueryModel.value = newQ
}
}
function removeKeyword(keyword: string) {
filters.value.keywords = filters.value.keywords.filter(k => k !== keyword)
const newQ = searchQuery.value
.replace(new RegExp(`keyword:${RegExp.escape(keyword)}($| )`, 'g'), '')
.trim()
router.replace({ query: { ...route.query, q: newQ || undefined } })
if (searchQueryModel) searchQueryModel.value = newQ
}
function toggleKeyword(keyword: string) {
if (filters.value.keywords.includes(keyword)) {
removeKeyword(keyword)
} else {
addKeyword(keyword)
}
}
function setSecurity(security: SecurityFilter) {
filters.value.security = security
}
function setUpdatedWithin(within: UpdatedWithin) {
filters.value.updatedWithin = within
}
function clearFilter(chip: FilterChip) {
switch (chip.type) {
case 'text':
filters.value.text = ''
break
case 'downloadRange':
filters.value.downloadRange = 'any'
break
case 'keywords':
removeKeyword(chip.value as string)
break
case 'security':
filters.value.security = 'all'
break
case 'updatedWithin':
filters.value.updatedWithin = 'any'
break
}
}
function clearAllFilters() {
filters.value = { ...DEFAULT_FILTERS }
}
function setSort(option: SortOption) {
sortOption.value = option
}
return {
// State
filters,
sortOption,
// Derived
filteredPackages,
sortedPackages,
availableKeywords,
activeFilters,
hasActiveFilters,
// Filter setters
setTextFilter,
setSearchScope,
setDownloadRange,
addKeyword,
removeKeyword,
toggleKeyword,
setSecurity,
setUpdatedWithin,
clearFilter,
clearAllFilters,
// Sort setter
setSort,
}
}