2021-08-03 16:55:52 +00:00
|
|
|
import { Doc } from './classes'
|
|
|
|
import { createPredicates, isPredicate } from './predicate'
|
|
|
|
import { SortingQuery } from './storage'
|
|
|
|
|
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) {
|
|
|
|
if ((object as any)[propertyKey] === value) {
|
|
|
|
result.push(object)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
const result = typeof a[key] === 'string' ? a[key].localeCompare(b[key]) : a[key] - b[key]
|
|
|
|
if (result !== 0) return result * (sortOptions[key] as number)
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
result.sort(sortFunc)
|
|
|
|
}
|