2021-12-20 10:18:29 +00:00
|
|
|
import { DocumentQuery } from '.'
|
2021-08-03 16:55:52 +00:00
|
|
|
import { Doc } from './classes'
|
2022-01-13 09:07:41 +00:00
|
|
|
import { getObjectValue } from './objvalue'
|
2021-08-03 16:55:52 +00:00
|
|
|
import { createPredicates, isPredicate } from './predicate'
|
2022-02-07 09:09:57 +00:00
|
|
|
import { SortingOrder, SortingQuery } from './storage'
|
2021-08-03 16:55:52 +00:00
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-03 16:55:52 +00:00
|
|
|
export function findProperty (objects: Doc[], propertyKey: string, value: any): Doc[] {
|
|
|
|
if (isPredicate(value)) {
|
|
|
|
const preds = createPredicates(value, propertyKey)
|
|
|
|
for (const pred of preds) {
|
|
|
|
objects = pred(objects)
|
|
|
|
}
|
|
|
|
return objects
|
|
|
|
}
|
|
|
|
const result: Doc[] = []
|
|
|
|
for (const object of objects) {
|
2022-01-13 09:07:41 +00:00
|
|
|
const val = getObjectValue(propertyKey, object)
|
2021-11-18 12:48:05 +00:00
|
|
|
if ((val === value) || isArrayValueCheck(val, value)) {
|
2021-08-03 16:55:52 +00:00
|
|
|
result.push(object)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-11-18 12:48:05 +00:00
|
|
|
function isArrayValueCheck<T, P> (val: T, value: P): boolean {
|
|
|
|
return Array.isArray(val) && !Array.isArray(value) && val.includes(value)
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-03 16:55:52 +00:00
|
|
|
export function resultSort<T extends Doc> (result: T[], sortOptions: SortingQuery<T>): void {
|
|
|
|
const sortFunc = (a: any, b: any): number => {
|
|
|
|
for (const key in sortOptions) {
|
2021-12-20 09:06:31 +00:00
|
|
|
const aValue = getValue(key, a)
|
|
|
|
const bValue = getValue(key, b)
|
2022-02-07 09:09:57 +00:00
|
|
|
const result = getSortingResult(aValue, bValue, (sortOptions[key] as SortingOrder))
|
|
|
|
if (result !== 0) return result
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
result.sort(sortFunc)
|
|
|
|
}
|
2021-12-20 09:06:31 +00:00
|
|
|
|
2022-02-07 09:09:57 +00:00
|
|
|
function getSortingResult (aValue: any, bValue: any, order: SortingOrder): number {
|
|
|
|
let res = 0
|
2021-12-20 09:06:31 +00:00
|
|
|
if (typeof aValue === 'undefined') {
|
|
|
|
return typeof bValue === 'undefined' ? 0 : -1
|
|
|
|
}
|
|
|
|
if (typeof bValue === 'undefined') {
|
|
|
|
return 1
|
|
|
|
}
|
2022-02-07 09:09:57 +00:00
|
|
|
if (Array.isArray(aValue) && Array.isArray(bValue)) {
|
|
|
|
res = (aValue.sort((a, b) => (a - b) * order)[0] ?? 0) - (bValue.sort((a, b) => (a - b) * order)[0] ?? 0)
|
|
|
|
} else {
|
|
|
|
res = typeof aValue === 'string' ? aValue.localeCompare(bValue) : (aValue - bValue)
|
|
|
|
}
|
|
|
|
return res * order
|
2021-12-20 09:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getValue (key: string, obj: any): any {
|
2022-01-13 09:07:41 +00:00
|
|
|
let value = getObjectValue(key, obj)
|
2022-02-07 09:09:57 +00:00
|
|
|
if (typeof value === 'object' && !Array.isArray(value)) {
|
2021-12-20 09:06:31 +00:00
|
|
|
value = JSON.stringify(value)
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
2021-12-20 10:18:29 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function matchQuery<T extends Doc> (docs: Doc[], query: DocumentQuery<T>): Doc[] {
|
|
|
|
let result = [...docs]
|
|
|
|
for (const key in query) {
|
|
|
|
if (key === '_id' && ((query._id as any)?.$like === undefined || query._id === undefined)) continue
|
|
|
|
const value = (query as any)[key]
|
|
|
|
result = findProperty(result, key, value)
|
|
|
|
if (result.length === 0) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|