1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

rename functions to be more inline with typescript conceptual names

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-02-19 20:08:42 -05:00
parent 2fd41ad17d
commit 3cf0538905
3 changed files with 15 additions and 10 deletions

View File

@ -3,7 +3,7 @@ import { ResourceApplier } from "../../main/resource-applier";
import { clusterFrameMap } from "../cluster-frames";
import { ClusterId, clusterStore } from "../cluster-store";
import { appEventBus } from "../event-bus";
import { hasOptionalProperty, hasTypedProperty, isString, isBoolean, bindPredicate, isTypedArray } from "../utils/type-narrowing";
import { hasOptionalProperty, hasTypedProperty, isString, isBoolean, bindTypeGuard, isTypedArray } from "../utils/type-narrowing";
import { createTypedInvoker, createTypedSender } from "./type-enforced-ipc";
export type ClusterIdArgList = [clusterId: ClusterId];
@ -86,7 +86,7 @@ export const clusterKubectlApplyAll = createTypedInvoker({
},
verifier(args: unknown[]): args is [clusterId: ClusterId, resources: string[]] {
return hasTypedProperty(args, 0, isString)
&& hasTypedProperty(args, 1, bindPredicate(isTypedArray, isString))
&& hasTypedProperty(args, 1, bindTypeGuard(isTypedArray, isString))
&& args.length === 2;
},
});

View File

@ -1,6 +1,6 @@
import { UpdateInfo } from "electron-updater";
import { UpdateFileInfo, ReleaseNoteInfo } from "builder-util-runtime";
import { bindPredicate, bindPredicateOr, hasOptionalProperty, hasTypedProperty, isNull, isObject, isString, isTypedArray, isNumber, isBoolean } from "../utils/type-narrowing";
import { bindTypeGuard, createUnionGuard, hasOptionalProperty, hasTypedProperty, isNull, isObject, isString, isTypedArray, isNumber, isBoolean } from "../utils/type-narrowing";
import { createTypedSender } from "./type-enforced-ipc";
export const AutoUpdateLogPrefix = "[UPDATE-CHECKER]";
@ -23,16 +23,16 @@ function isUpdateFileInfo(src: unknown): src is UpdateFileInfo {
function isReleaseNoteInfo(src: unknown): src is ReleaseNoteInfo {
return isObject(src)
&& hasTypedProperty(src, "version", isString)
&& hasTypedProperty(src, "note", bindPredicateOr(isString, isNull));
&& hasTypedProperty(src, "note", createUnionGuard(isString, isNull));
}
function isUpdateInfo(src: unknown): src is UpdateInfo {
return isObject(src)
&& hasTypedProperty(src, "version", isString)
&& hasTypedProperty(src, "releaseDate", isString)
&& hasTypedProperty(src, "files", bindPredicate(isTypedArray, isUpdateFileInfo))
&& hasOptionalProperty(src, "releaseName", bindPredicateOr(isString, isNull))
&& hasOptionalProperty(src, "releaseNotes", bindPredicateOr(isString, isReleaseNoteInfo, isNull))
&& hasTypedProperty(src, "files", bindTypeGuard(isTypedArray, isUpdateFileInfo))
&& hasOptionalProperty(src, "releaseName", createUnionGuard(isString, isNull))
&& hasOptionalProperty(src, "releaseNotes", createUnionGuard(isString, isReleaseNoteInfo, isNull))
&& hasOptionalProperty(src, "stagingPercentage", isNumber);
}

View File

@ -101,14 +101,14 @@ export function isNull(val: unknown): val is null {
}
/**
* Creates a new predicate function (with the same predicate) from `fn`. Such
* Creates a new type-guard function (with the same predicate) from `fn`. Such
* that it can be called with just the value to be tested.
*
* This is useful for when using `hasOptionalProperty` and `hasTypedProperty`
* @param fn A typescript user predicate function to be bound
* @param boundArgs the set of arguments to be passed to `fn` in the new function
*/
export function bindPredicate<FnArgs extends any[], T>(fn: (arg1: unknown, ...args: FnArgs) => arg1 is T, ...boundArgs: FnArgs): (arg1: unknown) => arg1 is T {
export function bindTypeGuard<FnArgs extends any[], T>(fn: (arg1: unknown, ...args: FnArgs) => arg1 is T, ...boundArgs: FnArgs): (arg1: unknown) => arg1 is T {
return (arg1: unknown): arg1 is T => fn(arg1, ...boundArgs);
}
@ -118,7 +118,12 @@ 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<Predicates extends Predicate<any>[]>(...predicates: Predicates): Predicate<OrReturnPredicateType<Predicates>> {
/**
* Create a new type-guard for the union of the types that each of the
* predicates are type-guarding for
* @param predicates a list of predicates that should be executed in order
*/
export function createUnionGuard<Predicates extends Predicate<any>[]>(...predicates: Predicates): Predicate<OrReturnPredicateType<Predicates>> {
return (arg: unknown): arg is OrReturnPredicateType<Predicates> => {
return predicates.some(predicate => predicate(arg));
};