mirror of
https://github.com/hcengineering/platform.git
synced 2024-12-24 12:06:57 +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 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
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
*/
|
*/
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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">
|
||||||
|
Loading…
Reference in New Issue
Block a user