diff --git a/src/modifyQuery.ts b/src/modifyQuery.ts index 818f5db..3ca56a3 100644 --- a/src/modifyQuery.ts +++ b/src/modifyQuery.ts @@ -3,9 +3,10 @@ export function queryHasFilter(query: string, key: string, value: string): boole } export const removeLabelFromQuery = (query: string, key: string, value: string): string => { + const operators = ['AND', 'NOT'] const parts = query.split(' ') const index = parts.findIndex((part) => part.includes(`${key}:${value}`)) - const newParts = removeAtIndexAndBefore(parts, index) + const newParts = removeAtIndexAndBefore(parts, index, operators.includes(parts[index - 1])) return newParts.join(' ') } @@ -13,15 +14,16 @@ export const addLabelToQuery = (query: string, key: string, value: string, opera return `${query} ${operator} ${key}:${value}` } -const removeAtIndexAndBefore = (arr: string[], index: number): string[] => { +const removeAtIndexAndBefore = (arr: string[], index: number, removeBefore: boolean): string[] => { if (index < 0 || index >= arr.length) { return arr; } - if (index === 0) { - arr.splice(index, 1); + if (removeBefore) { + const isStart = index === 0; + arr.splice(isStart ? index : index - 1, isStart ? 1 : 2); } else { - arr.splice(index - 1, 2); + arr.splice(index, 1); } return arr;