Skip to content

Commit

Permalink
add check operators for removeLabelFromQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
Loori-R committed Feb 13, 2024
1 parent 1011e16 commit 18bfb16
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/modifyQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@ 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(' ')
}

export const addLabelToQuery = (query: string, key: string, value: string, operator: string): string => {
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;
Expand Down

0 comments on commit 18bfb16

Please sign in to comment.