platform/packages/core/src/query.ts

38 lines
995 B
TypeScript
Raw Normal View History

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