Skip to content

Commit

Permalink
types: use common types instead of repeated types
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Jun 13, 2023
1 parent 231edb8 commit 73da25c
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 65 deletions.
8 changes: 5 additions & 3 deletions packages/n4s/src/runtime/genEnforceLazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CB,
Stringable,
Maybe,
DynamicValue,
} from 'vest-utils';

import { ctx } from 'enforceContext';
Expand Down Expand Up @@ -91,6 +92,7 @@ export type LazyRuleRunners = {
export type ComposeResult = LazyRuleRunners & ((value: any) => void);

type RegisteredRules = Array<(value: RuleValue) => RuleDetailedResult>;
type LazyMessage =
| string
| ((value: unknown, originalMessage?: Stringable) => string);
type LazyMessage = DynamicValue<
string,
[value: unknown, originalMessage?: Stringable]
>;
11 changes: 8 additions & 3 deletions packages/vast/src/vast.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Maybe, isFunction, optionalFunctionValue } from 'vest-utils';
import {
DynamicValue,
Maybe,
isFunction,
optionalFunctionValue,
} from 'vest-utils';

// eslint-disable-next-line max-lines-per-function
export function createState(
Expand Down Expand Up @@ -87,8 +92,8 @@ export function createState(
}
}

type StateInput<S> = S | ((prevState?: S) => S);
type SetStateInput<S> = S | ((prevState: S) => S);
type StateInput<S> = DynamicValue<S, [prevState?: S]>;
type SetStateInput<S> = DynamicValue<S, [prevState: S]>;

export type State = CreateStateReturn;
export type StateHandlerReturn<S> = [S, (nextState: SetStateInput<S>) => void];
Expand Down
6 changes: 3 additions & 3 deletions packages/vest-utils/src/defaultTo.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import optionalFunctionValue from 'optionalFunctionValue';
import { Nullish } from 'utilityTypes';
import { DynamicValue, Nullish } from 'utilityTypes';

export default function defaultTo<T>(
value: Nullish<T> | ((...args: any[]) => Nullish<T>),
defaultValue: T | (() => T)
value: DynamicValue<Nullish<T>>,
defaultValue: DynamicValue<T>
): T {
return optionalFunctionValue(value) ?? optionalFunctionValue(defaultValue);
}
3 changes: 2 additions & 1 deletion packages/vest-utils/src/optionalFunctionValue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import isFunction from 'isFunction';
import { DynamicValue } from 'utilityTypes';

export default function optionalFunctionValue<T>(
value: T | ((...args: any[]) => T),
value: DynamicValue<T>,
...args: unknown[]
): T {
return isFunction(value) ? value(...args) : value;
Expand Down
11 changes: 8 additions & 3 deletions packages/vest-utils/src/tinyState.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import optionalFunctionValue from 'optionalFunctionValue';
import { DynamicValue } from 'utilityTypes';

export function createTinyState<S>(initialValue: S | (() => S)): TinyState<S> {
export function createTinyState<S>(
initialValue: SetValueInput<S>
): TinyState<S> {
let value: S;

resetValue();

return () => [value, setValue, resetValue];

function setValue(nextValue: S | ((currentValue: S) => S)) {
function setValue(nextValue: SetValueInput<S>) {
value = optionalFunctionValue(nextValue, value);
}

Expand All @@ -18,6 +21,8 @@ export function createTinyState<S>(initialValue: S | (() => S)): TinyState<S> {

export type TinyState<S> = () => [
value: S,
setValue: (next: S | ((prev: S) => S)) => void,
setValue: (next: SetValueInput<S>) => void,
resetValue: () => void
];

type SetValueInput<S> = DynamicValue<S, [prev: S]>;
10 changes: 8 additions & 2 deletions packages/vest-utils/src/utilityTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ export type DropFirst<T extends unknown[]> = T extends [unknown, ...infer U]
? U
: never;

export type Stringable = string | ((...args: any[]) => string);
export type Stringable = string | CB<string>;

export type CB = (...args: any[]) => any;
export type CB<T = any, Args extends TArgs = TArgs> = (...args: Args) => T;

export type ValueOf<T> = T[keyof T];

Expand All @@ -13,3 +13,9 @@ export type Nullish<T = void> = Nullable<T> | Maybe<T>;
export type Nullable<T> = T | null;

export type Maybe<T> = T | undefined;

export type OneOrMoreOf<T> = T | T[];

export type DynamicValue<T, Args extends TArgs = TArgs> = T | CB<T, Args>;

type TArgs = any[];
2 changes: 2 additions & 0 deletions packages/vest-utils/src/vest-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ export type {
Nullish,
Nullable,
Maybe,
OneOrMoreOf,
DynamicValue,
} from 'utilityTypes';
3 changes: 2 additions & 1 deletion packages/vest/src/core/context/SuiteContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createCascade } from 'context';
import { assign, TinyState, tinyState, cache, CacheApi } from 'vest-utils';
import { DynamicValue } from 'vest-utils/src/utilityTypes';

import { IsolateTest } from 'IsolateTest';
import { Modes } from 'Modes';
Expand All @@ -25,7 +26,7 @@ export const SuiteContext = createCascade<CTXType>((ctxRef, parentContext) => {

type CTXType = {
exclusion: TExclusion;
inclusion: Record<string, boolean | (() => boolean)>;
inclusion: Record<string, DynamicValue<boolean>>;
currentTest?: IsolateTest;
groupName?: string;
skipped?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/vest/src/exports/parser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { suiteSelectors } from 'vest';
import { hasOwnProperty, invariant, isNullish, isPositive } from 'vest-utils';

import { ErrorStrings } from 'ErrorStrings';
import { SuiteSummary, TFieldName, TGroupName } from 'SuiteResultTypes';
import { suiteSelectors } from 'vest';

export function parse<F extends TFieldName, G extends TGroupName>(
summary: SuiteSummary<F, G>
Expand Down
7 changes: 4 additions & 3 deletions packages/vest/src/hooks/exclusive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
hasOwnProperty,
optionalFunctionValue,
Maybe,
OneOrMoreOf,
} from 'vest-utils';

import { ErrorStrings } from 'ErrorStrings';
Expand All @@ -12,9 +13,9 @@ import { TExclusion, useExclusion, useInclusion } from 'SuiteContext';
import { TFieldName, TGroupName } from 'SuiteResultTypes';
import { useIsExcludedIndividually } from 'skipWhen';

export type ExclusionItem = Maybe<string | string[]>;
export type FieldExclusion<F extends TFieldName> = Maybe<F | F[]>;
export type GroupExclusion<G extends TGroupName> = Maybe<G | G[]>;
export type ExclusionItem = Maybe<OneOrMoreOf<string>>;
export type FieldExclusion<F extends TFieldName> = Maybe<OneOrMoreOf<F>>;
export type GroupExclusion<G extends TGroupName> = Maybe<OneOrMoreOf<G>>;

/**
* Adds a field or a list of fields into the inclusion list
Expand Down
19 changes: 4 additions & 15 deletions packages/vest/src/hooks/include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
} from 'vest-utils';

import { useExclusion, useInclusion } from 'SuiteContext';
import { SuiteResult, TFieldName, TGroupName } from 'SuiteResultTypes';
import { TFieldName, TGroupName } from 'SuiteResultTypes';
import { TDraftCondition } from 'getTypedMethods';
import { useCreateSuiteResult } from 'suiteResult';

/**
Expand All @@ -32,13 +33,7 @@ import { useCreateSuiteResult } from 'suiteResult';
export function include<F extends TFieldName, G extends TGroupName>(
fieldName: F
): {
when: (
condition:
| F
| TFieldName
| boolean
| ((draft: SuiteResult<F, G>) => boolean)
) => void;
when: (condition: F | TFieldName | TDraftCondition<F, G>) => void;
} {
const inclusion = useInclusion();
const exclusion = useExclusion();
Expand All @@ -52,13 +47,7 @@ export function include<F extends TFieldName, G extends TGroupName>(
/**
* Specifies the inclusion criteria for the field in `include` function.
*/
function when(
condition:
| F
| TFieldName
| ((draft: SuiteResult<F, G>) => boolean)
| boolean
): void {
function when(condition: F | TFieldName | TDraftCondition<F, G>): void {
const inclusion = useInclusion();
const exclusion = useExclusion();

Expand Down
15 changes: 9 additions & 6 deletions packages/vest/src/hooks/optional/OptionalTypes.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { DynamicValue, OneOrMoreOf } from 'vest-utils';

import { TFieldName } from 'SuiteResultTypes';

export type OptionalFields = Record<string, OptionalFieldDeclaration>;

export type OptionalsInput<F extends TFieldName> = F | F[] | OptionalsObject<F>;
export type OptionalsInput<F extends TFieldName> =
| OneOrMoreOf<F>
| OptionalsObject<F>;

type OptionalsObject<F extends TFieldName> = Record<
F,
(() => boolean) | boolean
>;
type OptionalsObject<F extends TFieldName> = Record<F, TOptionalRule>;

type ImmediateOptionalFieldDeclaration = {
type: OptionalFieldTypes.CUSTOM_LOGIC;
rule: boolean | (() => boolean);
rule: TOptionalRule;
applied: boolean;
};

Expand All @@ -21,6 +22,8 @@ type DelayedOptionalFieldDeclaration = {
rule: null;
};

type TOptionalRule = DynamicValue<boolean>;

export type OptionalFieldDeclaration =
| ImmediateOptionalFieldDeclaration
| DelayedOptionalFieldDeclaration;
Expand Down
5 changes: 3 additions & 2 deletions packages/vest/src/isolates/omitWhen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { optionalFunctionValue } from 'vest-utils';
import { Isolate } from 'vestjs-runtime';

import { SuiteContext, useOmitted } from 'SuiteContext';
import { SuiteResult, TFieldName, TGroupName } from 'SuiteResultTypes';
import { TFieldName, TGroupName } from 'SuiteResultTypes';
import { TDraftCondition } from 'getTypedMethods';
import { useCreateSuiteResult } from 'suiteResult';

/**
Expand All @@ -17,7 +18,7 @@ import { useCreateSuiteResult } from 'suiteResult';
*/
// @vx-allow use-use
export function omitWhen<F extends TFieldName, G extends TGroupName>(
conditional: boolean | ((draft: SuiteResult<F, G>) => boolean),
conditional: TDraftCondition<F, G>,
callback: CB
): void {
Isolate.create(() => {
Expand Down
5 changes: 3 additions & 2 deletions packages/vest/src/isolates/skipWhen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { CB, optionalFunctionValue } from 'vest-utils';
import { Isolate } from 'vestjs-runtime';

import { SuiteContext, useSkipped } from 'SuiteContext';
import { SuiteResult, TFieldName, TGroupName } from 'SuiteResultTypes';
import { TFieldName, TGroupName } from 'SuiteResultTypes';
import { TDraftCondition } from 'getTypedMethods';
import { useCreateSuiteResult } from 'suiteResult';

/**
Expand All @@ -16,7 +17,7 @@ import { useCreateSuiteResult } from 'suiteResult';
*/
// @vx-allow use-use
export function skipWhen<F extends TFieldName, G extends TGroupName>(
condition: boolean | ((draft: SuiteResult<F, G>) => boolean),
condition: TDraftCondition<F, G>,
callback: CB
): void {
Isolate.create(() => {
Expand Down
21 changes: 9 additions & 12 deletions packages/vest/src/suite/getTypedMethods.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import { optional, skipWhen, omitWhen, IsolateTest, group } from 'vest';
import { optional } from 'optional';
import { CB } from 'vest-utils';
import { CB, DynamicValue } from 'vest-utils';
import { Isolate, IsolateKey } from 'vestjs-runtime';

import { IsolateTest } from 'IsolateTest';
Expand Down Expand Up @@ -33,14 +33,9 @@ export function getTypedMethods<

export type TTypedMethods<F extends TFieldName, G extends TGroupName> = {
include: (fieldName: F) => {
when: (
condition: boolean | F | ((draft: SuiteResult<F, G>) => boolean)
) => void;
when: (condition: F | TDraftCondition<F, G>) => void;
};
omitWhen: (
conditional: boolean | ((draft: SuiteResult<F, G>) => boolean),
callback: CB
) => void;
omitWhen: (conditional: TDraftCondition<F, G>, callback: CB) => void;
only: {
(item: FieldExclusion<F>): void;
group(item: GroupExclusion<G>): void;
Expand All @@ -50,10 +45,7 @@ export type TTypedMethods<F extends TFieldName, G extends TGroupName> = {
(item: FieldExclusion<F>): void;
group(item: GroupExclusion<G>): void;
};
skipWhen: (
condition: boolean | ((draft: SuiteResult<F, G>) => boolean),
callback: CB
) => void;
skipWhen: (condition: TDraftCondition<F, G>, callback: CB) => void;
test: {
(fieldName: F, message: string, cb: TestFn): IsolateTest;
(fieldName: F, cb: TestFn): IsolateTest;
Expand All @@ -64,3 +56,8 @@ export type TTypedMethods<F extends TFieldName, G extends TGroupName> = {
};
group: (groupName: G, callback: () => void) => Isolate;
};

export type TDraftCondition<
F extends TFieldName,
G extends TGroupName
> = DynamicValue<boolean, [draft: SuiteResult<F, G>]>;
12 changes: 8 additions & 4 deletions packages/vest/src/suiteResult/selectors/suiteSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export function suiteSelectors<F extends TFieldName, G extends TGroupName>(
return getFailures(summary, Severity.WARNINGS, fieldName);
}

function getWarning(fieldName?: F): Maybe<string | SummaryFailure<F, G>> {
function getWarning(fieldName?: F): GetSingularResponse<F, G> {
return getFailure<F, G>(Severity.WARNINGS, summary, fieldName as F);
}

Expand All @@ -146,7 +146,7 @@ export function suiteSelectors<F extends TFieldName, G extends TGroupName>(
return getFailures(summary, Severity.ERRORS, fieldName);
}

function getError(fieldName?: F): Maybe<string | SummaryFailure<F, G>> {
function getError(fieldName?: F): GetSingularResponse<F, G> {
return getFailure<F, G>(Severity.ERRORS, summary, fieldName as F);
}

Expand All @@ -167,8 +167,8 @@ export function suiteSelectors<F extends TFieldName, G extends TGroupName>(
}

export interface SuiteSelectors<F extends TFieldName, G extends TGroupName> {
getWarning(fieldName?: F): Maybe<string | SummaryFailure<F, G>>;
getError(fieldName?: F): Maybe<string | SummaryFailure<F, G>>;
getWarning(fieldName?: F): GetSingularResponse<F, G>;
getError(fieldName?: F): GetSingularResponse<F, G>;
getErrors(fieldName: F): string[];
getErrors(): FailureMessages;
getWarnings(): FailureMessages;
Expand Down Expand Up @@ -288,3 +288,7 @@ function getFailure<F extends TFieldName, G extends TGroupName>(
matchingFieldName(summaryFailure, fieldName)
)?.message;
}

type GetSingularResponse<F extends TFieldName, G extends TGroupName> = Maybe<
string | SummaryFailure<F, G>
>;
Loading

2 comments on commit 73da25c

@vercel
Copy link

@vercel vercel bot commented on 73da25c Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest-next – ./website

vest-website.vercel.app
vest-next-git-latest-ealush.vercel.app
vest-next.vercel.app
vest-next-ealush.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 73da25c Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest – ./website

vest.vercel.app
vest-ealush.vercel.app
vestjs.dev
vest-git-latest-ealush.vercel.app
www.vestjs.dev

Please sign in to comment.