diff options
Diffstat (limited to 'server/models/utils.ts')
-rw-r--r-- | server/models/utils.ts | 14 |
1 files changed, 3 insertions, 11 deletions
diff --git a/server/models/utils.ts b/server/models/utils.ts index 956562e70..fe4596d31 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts | |||
@@ -231,15 +231,7 @@ function parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes) | |||
231 | ? [].concat.apply([], q.split('"').map((v, i) => i % 2 ? v : v.split(' '))).filter(Boolean) // split by space unless using double quotes | 231 | ? [].concat.apply([], q.split('"').map((v, i) => i % 2 ? v : v.split(' '))).filter(Boolean) // split by space unless using double quotes |
232 | : [] | 232 | : [] |
233 | 233 | ||
234 | // TODO: when Typescript supports Object.fromEntries, replace with the Object method | 234 | const objectMap = (obj, fn) => Object.fromEntries( |
235 | function fromEntries<T> (entries: [keyof T, T[keyof T]][]): T { | ||
236 | return entries.reduce( | ||
237 | (acc, [ key, value ]) => ({ ...acc, [key]: value }), | ||
238 | {} as T | ||
239 | ) | ||
240 | } | ||
241 | |||
242 | const objectMap = (obj, fn) => fromEntries( | ||
243 | Object.entries(obj).map( | 235 | Object.entries(obj).map( |
244 | ([ k, v ], i) => [ k, fn(v, k, i) ] | 236 | ([ k, v ], i) => [ k, fn(v, k, i) ] |
245 | ) | 237 | ) |
@@ -248,7 +240,7 @@ function parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes) | |||
248 | return { | 240 | return { |
249 | // search is the querystring minus defined filters | 241 | // search is the querystring minus defined filters |
250 | search: tokens.filter(e => !Object.values(prefixes).some(p => { | 242 | search: tokens.filter(e => !Object.values(prefixes).some(p => { |
251 | if (typeof p === "string") { | 243 | if (typeof p === 'string') { |
252 | return e.startsWith(p) | 244 | return e.startsWith(p) |
253 | } else { | 245 | } else { |
254 | return e.startsWith(p.prefix) | 246 | return e.startsWith(p.prefix) |
@@ -256,7 +248,7 @@ function parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes) | |||
256 | })).join(' '), | 248 | })).join(' '), |
257 | // filters defined in prefixes are added under their own name | 249 | // filters defined in prefixes are added under their own name |
258 | ...objectMap(prefixes, p => { | 250 | ...objectMap(prefixes, p => { |
259 | if (typeof p === "string") { | 251 | if (typeof p === 'string') { |
260 | return tokens.filter(e => e.startsWith(p)).map(e => e.slice(p.length)) // we keep the matched item, and remove its prefix | 252 | return tokens.filter(e => e.startsWith(p)).map(e => e.slice(p.length)) // we keep the matched item, and remove its prefix |
261 | } else { | 253 | } else { |
262 | const _tokens = tokens.filter(e => e.startsWith(p.prefix)).map(e => e.slice(p.prefix.length)).map(p.handler) | 254 | const _tokens = tokens.filter(e => e.startsWith(p.prefix)).map(e => e.slice(p.prefix.length)).map(p.handler) |