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

View File

@ -2,20 +2,6 @@ import { Doc } from './classes'
import { createPredicates, isPredicate } from './predicate' import { createPredicates, isPredicate } from './predicate'
import { SortingQuery } from './storage' 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 * @public
*/ */

View File

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

View File

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