mirror of
https://github.com/hcengineering/platform.git
synced 2024-12-23 19:44:59 +03:00
UserPopup to use $like
Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
parent
2cd37973a8
commit
7e33fd8906
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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">
|
||||
|
Loading…
Reference in New Issue
Block a user