Make new routing system the default
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-01-19 13:24:59 -06:00
parent 8cf19792a6
commit dc16dfdb81
17 changed files with 298 additions and 535 deletions

View File

@@ -14,6 +14,7 @@ type MaybeCollectionIndex = CollectionIndex | undefined
type ComparisonFunction<T> = (item: CollectionItem<T>, otherItem: CollectionItem<T>) => number
import { WhereOperator, applyWhere, whereMatch } from './where'
import {Awaitable, Either, isLeft, right, unright} from '../support/types'
const collect = <T>(items: CollectionItem<T>[]): Collection<T> => Collection.collect(items)
const toString = (item: unknown): string => String(item)
@@ -316,6 +317,44 @@ class Collection<T> {
return new Collection<T2>(newItems)
}
/**
* Create a new collection by mapping the items in this collection using the given function
* where the function returns an Either. The collection is all Right instances. If a Left
* is encountered, that value is returned.
* @param func
*/
mapRight<TLeft, TRight>(func: KeyFunction<T, Either<TLeft, TRight>>): Either<TLeft, Collection<TRight>> {
const newItems: CollectionItem<TRight>[] = []
for ( let i = 0; i < this.length; i += 1 ) {
const result = func(this.storedItems[i], i)
if ( isLeft(result) ) {
return result
}
newItems.push(unright(result))
}
return right(new Collection<TRight>(newItems))
}
/**
* Create a new collection by mapping the items in this collection using the given function
* where the function returns an Either. The collection is all Right instances. If a Left
* is encountered, that value is returned.
* @param func
*/
async asyncMapRight<TLeft, TRight>(func: KeyFunction<T, Awaitable<Either<TLeft, TRight>>>): Promise<Either<TLeft, Collection<TRight>>> {
const newItems: CollectionItem<TRight>[] = []
for ( let i = 0; i < this.length; i += 1 ) {
const result = await func(this.storedItems[i], i)
if ( isLeft(result) ) {
return result
}
newItems.push(unright(result))
}
return right(new Collection<TRight>(newItems))
}
/**
* Create a new collection by mapping the items in this collection using the given function,
* excluding any for which the function returns undefined.

View File

@@ -4,6 +4,10 @@ export type Awaitable<T> = T | Promise<T>
/** Type alias for something that may be undefined. */
export type Maybe<T> = T | undefined
export type MaybeArr<T extends [...any[]]> = {
[Index in keyof T]: Maybe<T[Index]>
} & {length: T['length']}
export type Either<T1, T2> = Left<T1> | Right<T2>
export type Left<T> = [T, undefined]
@@ -26,6 +30,14 @@ export function right<T>(what: T): Right<T> {
return [undefined, what]
}
export function unleft<T>(what: Left<T>): T {
return what[0]
}
export function unright<T>(what: Right<T>): T {
return what[1]
}
/** Type alias for a callback that accepts a typed argument. */
export type ParameterizedCallback<T> = ((arg: T) => any)