mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
rewrite bindPredicateOr to have arbitrary arity
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
04c36b4b5b
commit
2fd41ad17d
@ -32,8 +32,8 @@ function isUpdateInfo(src: unknown): src is UpdateInfo {
|
||||
&& hasTypedProperty(src, "releaseDate", isString)
|
||||
&& hasTypedProperty(src, "files", bindPredicate(isTypedArray, isUpdateFileInfo))
|
||||
&& hasOptionalProperty(src, "releaseName", bindPredicateOr(isString, isNull))
|
||||
&& hasOptionalProperty(src, "stagingPercentage", isNumber)
|
||||
&& hasOptionalProperty(src, "releaseNotes", bindPredicateOr(isString, isReleaseNoteInfo, isNull));
|
||||
&& hasOptionalProperty(src, "releaseNotes", bindPredicateOr(isString, isReleaseNoteInfo, isNull))
|
||||
&& hasOptionalProperty(src, "stagingPercentage", isNumber);
|
||||
}
|
||||
|
||||
export function isUpdateAvailableArgs(args: unknown[]): args is UpdateAvailableArgs {
|
||||
|
||||
@ -113,20 +113,13 @@ export function bindPredicate<FnArgs extends any[], T>(fn: (arg1: unknown, ...ar
|
||||
}
|
||||
|
||||
type Predicate<T> = (arg: unknown) => arg is T;
|
||||
type Rest<T extends any[]> = T extends [any, ...infer R] ? R : any;
|
||||
type First<T extends any[]> = T extends [infer R, ...any[]] ? R : any;
|
||||
type ReturnPredicateType<T extends (src: unknown) => src is any> = T extends (src: unknown) => src is infer R ? R : any;
|
||||
type OrReturnPredicateType<T extends Predicate<any>[]> = ReturnPredicateType<First<T>> | (T extends [any] ? never : OrReturnPredicateType<Rest<T>>);
|
||||
|
||||
export function bindPredicateOr<T1>(p1: Predicate<T1>): Predicate<T1>;
|
||||
export function bindPredicateOr<T1, T2>(p1: Predicate<T1>, p2: Predicate<T2>): Predicate<T1 | T2>;
|
||||
export function bindPredicateOr<T1, T2, T3>(p1: Predicate<T1>, p2: Predicate<T2>, p3: Predicate<T3>): Predicate<T1 | T2 | T3>;
|
||||
export function bindPredicateOr<T1, T2, T3, T4>(p1: Predicate<T1>, p2: Predicate<T2>, p3: Predicate<T3>, p4: Predicate<T4>): Predicate<T1 | T2 | T3 | T4>;
|
||||
|
||||
export function bindPredicateOr<T extends any[]>(...predicates: T): Predicate<T> {
|
||||
return (arg: unknown): arg is any => {
|
||||
for (const predicate of predicates) {
|
||||
if (predicate) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
export function bindPredicateOr<Predicates extends Predicate<any>[]>(...predicates: Predicates): Predicate<OrReturnPredicateType<Predicates>> {
|
||||
return (arg: unknown): arg is OrReturnPredicateType<Predicates> => {
|
||||
return predicates.some(predicate => predicate(arg));
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user