export function structureQuery()

in kahuna/public/js/search/structured-query/syntax.js [17:55]


export function structureQuery(query) {

    const struct = [];
    let m;
    if (query === undefined) {
        return struct;
    }
    while ((m = parserRe.exec(query)) !== null) {
        const sign  = m[1];
        const field = m[2] || m[3] || m[4];
        const symbol  = m[5] || m[6];
        const value = m[7] || m[8] || m[9];
        const text  = m[10] || m[11] || m[12];
        const key = {
            '#': 'label',
            '~': 'collection'
        }[symbol] || field;
        if (key) {
            // We don't want editable collection filters
            const type = key === 'collection' ? 'static-filter' : 'filter';
            const filterType = sign === '-' ? 'exclusion' : 'inclusion';
            struct.push({
                type: type,
                filterType: filterType,
                key: key,
                value: value
            });
        } else {
            // Maintain negatable "any" search as text
            const prepend = (sign === '-' ? '-' : '');
            struct.push({
                type: 'text',
                value: prepend + falsyValuesToEmptyString(text)
            });
        }
    }

    return orderChips(struct);
}