All files / src/libs/Search SearchQuery.ts

100% Statements 30/30
100% Branches 20/20
100% Functions 6/6
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                2x 27x             65x               81x                 24x 1x     23x     23x 63x   43x 43x   43x       23x 23x   23x 40x   40x 20x 20x   20x 4x 2x 2x 2x     16x 13x 3x 3x           23x      
import SearchElement from './SearchElement';
import SearchOperator from './SearchOperator';
import SearchTerm from './SearchTerm';
 
/**
 * Class representing a search query.
 * This class is used to store and manage a collection of search elements.
 */
export default class SearchQuery {
    private readonly _elements: SearchElement[] = [];
 
    /**
     * Adds a search element to the query.
     * @param element The search element to add.
     */
    public addElement(element: SearchElement): void {
        this._elements.push(element);
    }
 
    /**
     * Gets the collection of search elements.
     * @returns An array of search elements.
     */
    public getElements(): SearchElement[] {
        return this._elements;
    }
 
    /**
     * Determines if the query matches the given text.
     * @param text The text to test.
     * @returns True if the query matches the text; otherwise, false.
     */
    public matches(text: string): boolean {
        if (this._elements.length === 0) {
            return false;
        }
 
        text = text.toLowerCase();
 
        // First: test the terms
        const termResults: boolean[] = this._elements
            .filter((element) => !element.isOperator)
            .map((element) => {
                const term = element as SearchTerm;
                const termMatch = text.includes(term.term.toLowerCase());
 
                return term.isNegated ? !termMatch : termMatch;
            });
 
        // Second: test the operators
        let result = termResults[0];
        let termIndex = 1;
 
        for (let i = 1; i < this._elements.length; i++) {
            const element = this._elements[i];
 
            if (element.isOperator) {
                const operator = element as SearchOperator;
                const nextTermResult = termResults[termIndex++];
 
                if (operator.isNegated) {
                    if (operator.operator === '&') {
                        result = result && !nextTermResult;
                    } else if (operator.operator === '|') {
                        result = result || !nextTermResult;
                    }
                } else {
                    if (operator.operator === '&') {
                        result = result && nextTermResult;
                    } else if (operator.operator === '|') {
                        result = result || nextTermResult;
                    }
                }
            }
        }
 
        return result;
    }
}
 
Zur TypeDoc-Dokumentation