diff options
author | Chocobozzz <me@florianbigard.com> | 2020-05-06 10:31:52 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-05-06 10:31:52 +0200 |
commit | fc8aabd0bf38441c0591f21b9b435b52e99ffc23 (patch) | |
tree | 0fd22fd08d9952cd19961d9e01d1237a05eb5bfa /server/models/utils.ts | |
parent | d056b019565539c5d12a302d1968581a186e5514 (diff) | |
download | PeerTube-fc8aabd0bf38441c0591f21b9b435b52e99ffc23.tar.gz PeerTube-fc8aabd0bf38441c0591f21b9b435b52e99ffc23.tar.zst PeerTube-fc8aabd0bf38441c0591f21b9b435b52e99ffc23.zip |
parseQueryStringFilter cleanup
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) |