Add TreeModel and HasSubtree implementation

This commit is contained in:
2022-08-20 16:21:06 -05:00
parent 3d836afa59
commit f63891ef99
22 changed files with 380 additions and 108 deletions

View File

@@ -14,11 +14,11 @@ type MaybeCollectionIndex = CollectionIndex | undefined
type ComparisonFunction<T> = (item: CollectionItem<T>, otherItem: CollectionItem<T>) => number
import { WhereOperator, applyWhere, whereMatch } from './where'
import {Awaitable, Awaited, Either, isLeft, Maybe, MethodsOf, right, unright} from '../support/types'
import {Awaitable, Awaited, Either, isLeft, Maybe, MethodsOf, MethodType, right, unright} from '../support/types'
import {AsyncCollection} from './AsyncCollection'
import {ArrayIterable} from './ArrayIterable'
const collect = <T>(items: CollectionItem<T>[]): Collection<T> => Collection.collect(items)
const collect = <T>(items: CollectionItem<T>[] = []): Collection<T> => Collection.collect(items)
const toString = (item: unknown): string => String(item)
export {
@@ -381,8 +381,12 @@ class Collection<T> {
* @param method
* @param params
*/
mapCall<T2 extends MethodsOf<T>>(method: T2, ...params: Parameters<T[T2]>): Collection<ReturnType<T[T2]>> {
return this.map(x => x[method](...params))
mapCall<T2 extends MethodsOf<T>>(method: T2, ...params: Parameters<MethodType<T, T2>>): Collection<ReturnType<MethodType<T, T2>>> {
// This is dumb, but I'm not sure how else to resolve it. The types check out, but TypeScript loses track of the fact that
// typeof x[method] === MethodType<T, T2>, so it assumes we're indexing an object incorrectly.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return this.map((x: T) => x[method](...params))
}
/**
@@ -390,7 +394,7 @@ class Collection<T> {
* @param method
* @param params
*/
async awaitMapCall<T2 extends MethodsOf<T>>(method: T2, ...params: Parameters<T[T2]>): Promise<Collection<Awaited<ReturnType<T[T2]>>>> {
async awaitMapCall<T2 extends MethodsOf<T>>(method: T2, ...params: Parameters<MethodType<T, T2>>): Promise<Collection<Awaited<ReturnType<MethodType<T, T2>>>>> {
return this.mapCall(method, ...params).awaitAll()
}

View File

@@ -73,6 +73,10 @@ export type MethodsOf<T, TMethod = (...args: any[]) => any> = {
[K in keyof T]: T[K] extends TMethod ? K : never
}[keyof T]
export type MethodType<TClass, TKey extends keyof TClass, TMethod = (...args: any[]) => any> = {
[K in keyof TClass]: TClass[K] extends TMethod ? TClass[K] : never
}[TKey]
export type Awaited<T> = T extends PromiseLike<infer U> ? U : T
export type Integer = TypeTag<'@extollo/lib.Integer'> & number