1
0
Fork 0

parseQueryStringFilter cleanup

This commit is contained in:
Chocobozzz 2020-05-06 10:31:52 +02:00
parent d056b01956
commit fc8aabd0bf
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 16 additions and 20 deletions

View File

@ -231,15 +231,7 @@ function parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes)
? [].concat.apply([], q.split('"').map((v, i) => i % 2 ? v : v.split(' '))).filter(Boolean) // split by space unless using double quotes
: []
// TODO: when Typescript supports Object.fromEntries, replace with the Object method
function fromEntries<T> (entries: [keyof T, T[keyof T]][]): T {
return entries.reduce(
(acc, [ key, value ]) => ({ ...acc, [key]: value }),
{} as T
)
}
const objectMap = (obj, fn) => fromEntries(
const objectMap = (obj, fn) => Object.fromEntries(
Object.entries(obj).map(
([ k, v ], i) => [ k, fn(v, k, i) ]
)
@ -248,7 +240,7 @@ function parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes)
return {
// search is the querystring minus defined filters
search: tokens.filter(e => !Object.values(prefixes).some(p => {
if (typeof p === "string") {
if (typeof p === 'string') {
return e.startsWith(p)
} else {
return e.startsWith(p.prefix)
@ -256,7 +248,7 @@ function parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes)
})).join(' '),
// filters defined in prefixes are added under their own name
...objectMap(prefixes, p => {
if (typeof p === "string") {
if (typeof p === 'string') {
return tokens.filter(e => e.startsWith(p)).map(e => e.slice(p.length)) // we keep the matched item, and remove its prefix
} else {
const _tokens = tokens.filter(e => e.startsWith(p.prefix)).map(e => e.slice(p.prefix.length)).map(p.handler)

View File

@ -32,10 +32,12 @@ export enum ScopeNames {
searchReportee?: string
searchVideo?: string
searchVideoChannel?: string
// filters
id?: number
state?: VideoAbuseState
is?: any
is?: 'deleted' | 'blacklisted'
// accountIds
serverAccountId: number
userAccountId: number
@ -91,11 +93,11 @@ export enum ScopeNames {
}
let onlyBlacklisted = false
if (options.is === "deleted") {
if (options.is === 'deleted') {
where = Object.assign(where, {
deletedVideo: { [Op.not]: null }
})
} else if (options.is === "blacklisted") {
} else if (options.is === 'blacklisted') {
onlyBlacklisted = true
}
@ -323,17 +325,17 @@ export class VideoAbuseModel extends Model<VideoAbuseModel> {
state: {
prefix: 'state:',
handler: v => {
if (v === "accepted") return VideoAbuseState.ACCEPTED
if (v === "pending") return VideoAbuseState.PENDING
if (v === "rejected") return VideoAbuseState.REJECTED
if (v === 'accepted') return VideoAbuseState.ACCEPTED
if (v === 'pending') return VideoAbuseState.PENDING
if (v === 'rejected') return VideoAbuseState.REJECTED
return undefined
}
},
is: {
prefix: 'is:',
handler: v => {
if (v === "deleted") return v
if (v === "blacklisted") return v
if (v === 'deleted') return v
if (v === 'blacklisted') return v
return undefined
}
},

View File

@ -12,7 +12,9 @@
"dom",
"es2015",
"es2016",
"es2017"
"es2017",
"es2018",
"es2019"
],
"typeRoots": [
"node_modules/sitemap/node_modules/@types",