aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/models/utils.ts27
1 files changed, 16 insertions, 11 deletions
diff --git a/server/models/utils.ts b/server/models/utils.ts
index 3e3825b32..956562e70 100644
--- a/server/models/utils.ts
+++ b/server/models/utils.ts
@@ -223,9 +223,12 @@ interface QueryStringFilterPrefixes {
223 [key: string]: string | { prefix: string, handler: Function, multiple?: boolean } 223 [key: string]: string | { prefix: string, handler: Function, multiple?: boolean }
224} 224}
225 225
226function parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes) { 226function parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes): {
227 search: string
228 [key: string]: string | number | string[] | number[]
229} {
227 const tokens = q // tokenize only if we have a querystring 230 const tokens = q // tokenize only if we have a querystring
228 ? [].concat.apply([], q.split('"').map((v, i) => i % 2 ? v : v.split(' '))).filter(Boolean) 231 ? [].concat.apply([], q.split('"').map((v, i) => i % 2 ? v : v.split(' '))).filter(Boolean) // split by space unless using double quotes
229 : [] 232 : []
230 233
231 // TODO: when Typescript supports Object.fromEntries, replace with the Object method 234 // TODO: when Typescript supports Object.fromEntries, replace with the Object method
@@ -252,16 +255,18 @@ function parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes)
252 } 255 }
253 })).join(' '), 256 })).join(' '),
254 // filters defined in prefixes are added under their own name 257 // filters defined in prefixes are added under their own name
255 ...objectMap(prefixes, v => { 258 ...objectMap(prefixes, p => {
256 if (typeof v === "string") { 259 if (typeof p === "string") {
257 return tokens.filter(e => e.startsWith(v)).map(e => e.slice(v.length)) 260 return tokens.filter(e => e.startsWith(p)).map(e => e.slice(p.length)) // we keep the matched item, and remove its prefix
258 } else { 261 } else {
259 const _tokens = tokens.filter(e => e.startsWith(v.prefix)).map(e => e.slice(v.prefix.length)).map(v.handler) 262 const _tokens = tokens.filter(e => e.startsWith(p.prefix)).map(e => e.slice(p.prefix.length)).map(p.handler)
260 return !v.multiple 263 // multiple is false by default, meaning we usually just keep the first occurence of a given prefix
261 ? _tokens.length > 0 264 if (!p.multiple && _tokens.length > 0) {
262 ? _tokens[0] 265 return _tokens[0]
263 : '' 266 } else if (!p.multiple) {
264 : _tokens 267 return ''
268 }
269 return _tokens
265 } 270 }
266 }) 271 })
267 } 272 }