import * as Fuse from 'fuse.js' ; import * as Mobx from 'mobx' ; import { Pattern } from './pattern' ; import * as Types from '@meetalva/types' ; export interface PatternSearchInit { patterns : Pattern [ ] ; } export class PatternSearch { @ Mobx . observable private fuse : Fuse < Types . SerializedPattern > ; public constructor ( init ) { this . fuse = new Fuse ( init . patterns . map ( item => item . toJSON ( ) ) , { keys : [ 'name' , 'description' ] } ) ; } public query ( term ) { if ( term . trim ( ) . length === 0 ) { return ( this . fuse as any ) . list . map ( ( item ) => item . id ) ; } return this . fuse . search ( term ) . map ( match => match . id ) ; } }