From d8af3f70c2e4bf05f2e6b8211167f7871638ba7c Mon Sep 17 00:00:00 2001 From: Brian Fernandez Date: Fri, 29 Dec 2023 20:03:20 -0500 Subject: [PATCH] refactor!: replace existing methods with improved alternatives This commit replaces the following methods: - `inCollection()` is replaced with `getByCollection()` - `name()` is replaced with `getByFileName()` - `fullText()` is replaced with `getByContent()` - `mimeType()` is replaced with `getByFileType()` - `createdTime()` is replaced with `getByCreatedAt()` - `modifiedTime()` is replaced with `getByUpdatedAt()` --- src/index.ts | 125 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 73 insertions(+), 52 deletions(-) diff --git a/src/index.ts b/src/index.ts index 48e9b01..38ba4fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,29 +1,35 @@ import { Collection, File, Operator, QueryType } from './constants.js' -type QueryTerm = Collection | File +type QueryTemplateOptions = { + field: File | Collection + op: Operator + entry: { + key?: string + value: unknown + } +} const QueryTemplate = { - [QueryType.COLLECTION]: ( - value: string, - operator: Operator, - collection: QueryTerm - ) => `'${value}' ${operator} ${collection}`, - [QueryType.STRING]: (value: string, operator: Operator, term: QueryTerm) => - `${term} ${operator} '${value}'`, - [QueryType.BOOLEAN]: (value: string, operator: Operator, term: QueryTerm) => - `${term} ${operator} ${value}` + [QueryType.COLLECTION]: ({ field, op, entry }: QueryTemplateOptions) => + `'${entry.value}' ${op} ${field}`, + [QueryType.STRING]: ({ field, op, entry }: QueryTemplateOptions) => + `${field} ${op} '${entry.value}'`, + [QueryType.BOOLEAN]: ({ field, op, entry }: QueryTemplateOptions) => + `${field} ${op} ${entry.value}` +} + +type AddQueryOpts = { + field: File | Collection + op: Operator + entry: Record | string[] } class QueryBuilder { private readonly queries: string[] = [] private negateNextTerm = false - private lastTerm: QueryTerm = Collection.PARENTS - private addQuery( - type: QueryType, - operator: Operator, - values: string | string[] - ): void { + private addQuery(type: QueryType, options: AddQueryOpts): void { + const { field, op, entry } = options let query = '' if (this.negateNextTerm) { @@ -31,70 +37,85 @@ class QueryBuilder { query += `${Operator.NOT} ` } - if (typeof values === 'string') { - query += QueryTemplate[type](values, operator, this.lastTerm) - } - - if (Array.isArray(values)) { - query += `(${values - .map(v => QueryTemplate[type](v, operator, this.lastTerm)) - .join(` ${Operator.OR} `)})` + const _queries = [] + for (const key in entry) { + const value = entry[key as keyof typeof entry] + _queries.push( + QueryTemplate[type]({ field, op, entry: { key: `${key}`, value } }) + ) } + query += `(${_queries.join( + ` ${Array.isArray(entry) ? Operator.OR : Operator.AND} ` + )})` this.queries.push(query) } - // Comparison methods (query operators) - not(): this { this.negateNextTerm = true - return this } - // Term methods (query terms) - - inCollection(collection: Collection, values: string | string[]): this { - this.lastTerm = collection - this.addQuery(QueryType.COLLECTION, Operator.IN, values) - + getByCollection(collection: Collection, value: string | string[]): this { + this.addQuery(QueryType.COLLECTION, { + field: collection, + op: Operator.IN, + entry: Array.isArray(value) ? value : [value] + }) return this } - isTrashed(value: boolean): this { - this.lastTerm = File.TRASHED - this.addQuery(QueryType.BOOLEAN, Operator.EQUAL, `${value}`) - + getByFileName(filename: string | string[]): this { + this.addQuery(QueryType.STRING, { + field: File.NAME, + op: Operator.EQUAL, + entry: Array.isArray(filename) ? filename : [filename] + }) return this } - name(): this { - this.lastTerm = File.NAME - + getByContent(value: string | string[]): this { + this.addQuery(QueryType.STRING, { + field: File.FULL_TEXT, + op: Operator.EQUAL, + entry: Array.isArray(value) ? value : [value] + }) return this } - fullText(): this { - this.lastTerm = File.FULL_TEXT - + getByFileType(filetype: string | string[]): this { + this.addQuery(QueryType.STRING, { + field: File.MIME_TYPE, + op: Operator.EQUAL, + entry: Array.isArray(filetype) ? filetype : [filetype] + }) return this } - mimeType(): this { - this.lastTerm = File.MIME_TYPE - + getByCreatedAt(timestamp: string | string[]): this { + this.addQuery(QueryType.STRING, { + field: File.CREATED_TIME, + op: Operator.EQUAL, + entry: Array.isArray(timestamp) ? timestamp : [timestamp] + }) return this } - modifiedTime(): this { - this.lastTerm = File.MODIFIED_TIME - + getByUpdatedAt(timestamp: string | string[]): this { + this.addQuery(QueryType.STRING, { + field: File.MODIFIED_TIME, + op: Operator.EQUAL, + entry: Array.isArray(timestamp) ? timestamp : [timestamp] + }) return this } - createdTime(): this { - this.lastTerm = File.CREATED_TIME - + isTrashed(value: boolean): this { + this.addQuery(QueryType.BOOLEAN, { + field: File.TRASHED, + op: Operator.EQUAL, + entry: [`${value}`] + }) return this }