MCPcopy Create free account
hub / github.com/ThatGuySam/doesitarm / SearchFilters

Class SearchFilters

helpers/search/filters.js:4–119  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2import { isString } from '~/helpers/check-types.js'
3
4export class SearchFilters {
5 constructor({
6 initialFilters = {}
7 } = {}) {
8 this.initialFilters = initialFilters
9
10 this.filters = {
11 ...initialFilters
12 }
13 }
14
15 get list () {
16 return Object.entries( this.filters ).map( ([ filterKey, filterValue ]) => {
17 return `${ filterKey }${ filterSeparator }${ filterValue }`
18 } )
19 }
20
21 get asQuery () {
22 return this.list.join(' ')
23 }
24
25 get asPagefindFilters () {
26 return Object.fromEntries( Object.entries( this.filters ).map( ([ filterKey, filterValue ]) => {
27 return [ filterKey, [ filterValue ] ]
28 }) )
29 }
30
31 getByKey ( key ) {
32 return `${ key }${ filterSeparator }${ this.filters[ key ] }`
33 }
34
35 isQueryValue ( filterNameOrQueryValue ) {
36 return filterNameOrQueryValue.includes( filterSeparator )
37 }
38
39 getKeyAndValue ( filterQueryValue ) {
40 const key = filterQueryValue.substring(0, filterQueryValue.indexOf( filterSeparator ))
41 const value = filterQueryValue.substring(filterQueryValue.indexOf( filterSeparator )+1)
42
43 return { key, value }
44 }
45
46 getFilterNameAndValueFromString ( filterNameOrQueryValue ) {
47 if ( this.isQueryValue( filterNameOrQueryValue ) ) {
48 return this.getKeyAndValue( filterNameOrQueryValue )
49 }
50
51 return {
52 key: filterNameOrQueryValue,
53 value: null
54 }
55 }
56
57 remove ( filterName ) {
58 if ( this.isQueryValue( filterName ) ) throw new Error(`${ filterName } is not a valid filter name`)
59
60 delete this.filters[ filterName ]
61 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected