common.js
1 day ago
rest.js
2 weeks ago
search.js
3 weeks ago
site-import.js
2 years ago
svg.js
2 weeks ago
search.js
41 lines
| 1 | import Fuse from 'fuse.js/dist/fuse.min'; |
| 2 | |
| 3 | const SEARCH_KEYS = [ |
| 4 | { name: 'title', weight: 0.5 }, |
| 5 | { name: 'category', weight: 0.35 }, |
| 6 | { name: 'slug', weight: 0.3 }, |
| 7 | { name: 'site_description', weight: 0.25 }, |
| 8 | { name: 'description', weight: 0.25 }, |
| 9 | { name: 'keywords', weight: 0.2 }, |
| 10 | ]; |
| 11 | |
| 12 | const SEARCH_THRESHOLD = 0.3; |
| 13 | |
| 14 | export const searchCatalog = ( items, query ) => { |
| 15 | const q = ( query || '' ).trim(); |
| 16 | if ( ! q || ! Array.isArray( items ) || ! items.length ) { |
| 17 | return []; |
| 18 | } |
| 19 | |
| 20 | return new Fuse( items, { |
| 21 | includeScore: true, |
| 22 | threshold: SEARCH_THRESHOLD, |
| 23 | keys: SEARCH_KEYS, |
| 24 | } ) |
| 25 | .search( q ) |
| 26 | .map( ( result ) => result.item ) |
| 27 | .filter( Boolean ); |
| 28 | }; |
| 29 | |
| 30 | export const matchesCategory = ( site, cat ) => { |
| 31 | if ( 'free' === cat ) { |
| 32 | return ! site.upsell; |
| 33 | } |
| 34 | |
| 35 | if ( cat && 'all' !== cat ) { |
| 36 | return Array.isArray( site.keywords ) && site.keywords.includes( cat ); |
| 37 | } |
| 38 | |
| 39 | return true; |
| 40 | }; |
| 41 |