Skip to content

Commit caf618e

Browse files
feat: allow filtering by starts with and ends with
1 parent a6851eb commit caf618e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/components/dash/filters/AddFilterGroup.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@
220220
<option value={FilterMode.EXCLUDES}>exclude</option>
221221
<option value={FilterMode.EXACT}>match</option>
222222
<option value={FilterMode.NOTEXACT}>don't match</option>
223+
<option value={FilterMode.STARTSWITH}>starts with</option>
224+
<option value={FilterMode.ENDSWITH}>ends with</option>
223225
<option value={FilterMode.EMPTY}>empty</option>
224226
<option value={FilterMode.NOTEMPTY}>not empty</option>
225227
<option value={FilterMode.HIGHERTHAN}>longer than</option>

src/lib/dash/filters.svelte.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ export const filterModeText = (newMode: FilterMode, type: string) => {
113113
verb: type === "date" ? "are before" : "are less than",
114114
afterVerb: type === "string" ? "characters long" : "",
115115
},
116+
{
117+
mode: FilterMode.STARTSWITH,
118+
verb: "start with",
119+
},
120+
{
121+
mode: FilterMode.ENDSWITH,
122+
verb: "end with",
123+
}
116124
]
117125

118126
return {
@@ -142,6 +150,10 @@ export enum FilterMode {
142150
EXACT = "match",
143151
// string: any no exact match, int: != value, groups: exclude members in all groups
144152
NOTEXACT = "no match",
153+
// string: starts with, int: N/A, groups: N/A
154+
STARTSWITH = "starts with",
155+
// string: ends with, int: N/A, groups N/A
156+
ENDSWITH = "ends with"
145157
}
146158

147159
export const groupArrayModes = [
@@ -405,6 +417,22 @@ function applyFilter<T>(list: T[], filter: Filter, groupList?: Group[]): T[] {
405417
} else return false
406418
})
407419
break
420+
case FilterMode.STARTSWITH:
421+
processedList = processedList.filter((i) => {
422+
if (!value) return true
423+
if (!i[field]) return false
424+
if (typeof i[field] !== "string") return true
425+
return (i[field] as string).startsWith(value as string)
426+
})
427+
break
428+
case FilterMode.ENDSWITH:
429+
processedList = processedList.filter((i) => {
430+
if (!value) return true
431+
if (!i[field]) return false
432+
if (typeof i[field] !== "string") return true
433+
return (i[field] as string).endsWith(value as string)
434+
})
435+
break
408436
}
409437
return processedList
410438
}

0 commit comments

Comments
 (0)