UserPopup to use $like

Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
Andrey Platov 2021-10-01 15:07:09 +02:00
parent 2cd37973a8
commit 7e33fd8906
No known key found for this signature in database
GPG Key ID: C8787EFEB4B64AF0
4 changed files with 9 additions and 18 deletions

View File

@ -15,11 +15,12 @@
//
import type { Doc } from './classes'
import { checkLikeQuery } from './query'
type Predicate = (docs: Doc[]) => Doc[]
type PredicateFactory = (pred: any, propertyKey: string) => Predicate
const likeSymbol = '%'
const predicates: Record<string, PredicateFactory> = {
$in: (o: any, propertyKey: string): Predicate => {
if (!Array.isArray(o)) {
@ -35,11 +36,13 @@ const predicates: Record<string, PredicateFactory> = {
},
$like: (query: string, propertyKey: string): Predicate => {
const searchString = query.split(likeSymbol).join('.*')
const regex = RegExp(`^${searchString}$`, 'i')
return (docs: Doc[]): Doc[] => {
const result: Doc[] = []
for (const doc of docs) {
const value = (doc as any)[propertyKey] as string
if (checkLikeQuery(value, query)) result.push(doc)
if (regex.test(value)) result.push(doc)
}
return result
}

View File

@ -2,20 +2,6 @@ import { Doc } from './classes'
import { createPredicates, isPredicate } from './predicate'
import { SortingQuery } from './storage'
/**
* @public
*/
export const likeSymbol = '%'
/**
* @public
*/
export function checkLikeQuery (value: string, query: string): boolean {
const searchString = query.split(likeSymbol).join('.*')
const regex = RegExp(`^${searchString}$`)
return regex.test(value)
}
/**
* @public
*/

View File

@ -21,9 +21,11 @@ import type { Tx } from './tx'
* @public
*/
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type QuerySelector<T> = {
export type QuerySelector<T> = { // TODO: refactor this shit
$in?: T[]
$like?: string
$regex?: string
$options?: string
}
/**

View File

@ -32,7 +32,7 @@
const dispatch = createEventDispatcher()
const query = createQuery()
$: query.query(_class, {}, result => { objects = result })
$: query.query(_class, { name: { $like: '%'+search+'%' } }, result => { objects = result })
</script>
<div class="popup">