Skip to content

Commit

Permalink
chore: remove duplicate types
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 10, 2021
1 parent e4e2a80 commit eeac20e
Show file tree
Hide file tree
Showing 23 changed files with 125 additions and 143 deletions.
1 change: 1 addition & 0 deletions packages/n4s/src/compounds/compounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export default function compounds() {
}

export type TCompounds = ReturnType<typeof compounds>;
export type KCompounds = keyof TCompounds;
7 changes: 4 additions & 3 deletions packages/n4s/src/compounds/oneOf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { TLazy } from 'genEnforceLazy';
import { lengthEquals } from 'lengthEquals';
import type { TRuleDetailedResult } from 'ruleReturn';
import { longerThan } from 'longerThan';
import { TRuleDetailedResult, ruleReturn } from 'ruleReturn';
import runLazyRule from 'runLazyRule';

export default function oneOf(
Expand All @@ -10,7 +11,7 @@ export default function oneOf(
const passing: TRuleDetailedResult[] = [];

rules.every(rule => {
if (passing.length > 1) {
if (longerThan(passing, 1)) {
return false;
}

Expand All @@ -22,5 +23,5 @@ export default function oneOf(
return res.pass;
});

return { pass: lengthEquals(passing, 1) };
return ruleReturn(lengthEquals(passing, 1));
}
39 changes: 17 additions & 22 deletions packages/n4s/src/lib/ruleReturn.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
import defaultTo from 'defaultTo';

import { TEnforceContext } from 'enforceContext';
import type { TModifiers } from 'modifiers';
import { TRule } from 'runtimeRules';

export type TRuleReturn =
| boolean
| {
pass: boolean;
message?: string | (() => string);
};

export type TRuleDetailedResult = { pass: boolean; message?: string };

export type TLazyRuleMethods = TModifiers & {
test: (value: unknown) => boolean;
run: (value: unknown) => TRuleDetailedResult;
context: () => TEnforceContext;
extend: (customRules: TRule) => void;
};
import type { TStringable } from 'utilityTypes';

export function ruleReturn(
pass: boolean,
Expand All @@ -34,11 +15,11 @@ export function ruleReturn(
}

export function failing(): TRuleDetailedResult {
return { pass: false };
return ruleReturn(false);
}

export function passing(): TRuleDetailedResult {
return { pass: true };
return ruleReturn(true);
}

export function defaultToFailing(
Expand All @@ -52,3 +33,17 @@ export function defaultToPassing(
): TRuleDetailedResult {
return defaultTo(callback, passing());
}

export type TRuleReturn =
| boolean
| {
pass: boolean;
message?: TStringable;
};

export type TRuleDetailedResult = { pass: boolean; message?: string };

export type TLazyRuleMethods = {
test: (value: unknown) => boolean;
run: (value: unknown) => TRuleDetailedResult;
};
9 changes: 4 additions & 5 deletions packages/n4s/src/modifiers/partial.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import enforce from 'enforce';
import type { TLazy, TShapeObject } from 'genEnforceLazy';
import type { TShapeObject } from 'genEnforceLazy';

export default function partial(
shapeObject: TShapeObject
): Record<string, TLazy> {
const output: Record<string, TLazy> = {};
export default function partial(shapeObject: TShapeObject): TShapeObject {
const output: TShapeObject = {};
for (const key in shapeObject) {
// @ts-expect-error - This is pretty tricky to get right. Everything works, but ts still doesn't like it
output[key] = enforce.optional(shapeObject[key]);
}

Expand Down
20 changes: 11 additions & 9 deletions packages/n4s/src/runtime/enforce.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import assign from 'assign';

import eachEnforceRule from 'eachEnforceRule';
import { ctx } from 'enforceContext';
import { TEnforceContext, ctx } from 'enforceContext';
import enforceEager, { TEnforceEager } from 'enforceEager';
import genEnforceLazy, { TLazyRules } from 'genEnforceLazy';
import isProxySupported from 'isProxySupported';
import modifiers from 'modifiers';
import type { TLazyRuleMethods } from 'ruleReturn';
import { baseRules, getRule, TRule, TArgs, KBaseRules } from 'runtimeRules';
import modifiers, { TModifiers } from 'modifiers';
import { TRule, KBaseRules, baseRules, getRule } from 'runtimeRules';

/**
* Enforce is quite complicated, I want to explain it in detail.
* It is dynamic in nature, so a lot of proxy objects are involved.
Expand Down Expand Up @@ -73,10 +73,12 @@ const enforce = genEnforce();

export default enforce;

export type TEnforce = Record<
string,
(...args: TArgs) => TLazyRuleMethods & TLazyRules
> &
export type TEnforce = TModifiers &
TEnforceEager &
TLazyRules &
TLazyRuleMethods;
TEnforceMethods;

type TEnforceMethods = {
context: () => TEnforceContext;
extend: (customRules: TRule) => void;
};
24 changes: 5 additions & 19 deletions packages/n4s/src/runtime/enforceEager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import throwError from 'throwError';
import { DropFirst } from 'utilityTypes';

import type { TCompounds } from 'compounds';
import eachEnforceRule from 'eachEnforceRule';
import { ctx } from 'enforceContext';
import { isEmpty } from 'isEmpty';
Expand All @@ -12,12 +10,12 @@ import {
TArgs,
TRuleBase,
KBaseRules,
TBaseRules,
TRules,
} from 'runtimeRules';
import { transformResult } from 'transformResult';

export default function enforceEager(value: TRuleValue): TEagerRules {
const target = {} as TEagerRules;
export default function enforceEager(value: TRuleValue): TRules {
const target = {} as TRules;
if (!isProxySupported()) {
eachEnforceRule((ruleName: KBaseRules, ruleFn) => {
target[ruleName] = genRuleCall(target, ruleFn, ruleName);
Expand All @@ -33,11 +31,11 @@ export default function enforceEager(value: TRuleValue): TEagerRules {
return genRuleCall(proxy, rule, ruleName);
}
},
}) as TEagerRules;
}) as TRules;

return proxy;

function genRuleCall(target: TEagerRules, rule: TRuleBase, ruleName: string) {
function genRuleCall(target: TRules, rule: TRuleBase, ruleName: string) {
return function ruleCall(...args: TArgs) {
const transformedResult = transformResult(
ctx.run({ value }, () => rule(value, ...args)),
Expand All @@ -62,15 +60,3 @@ export default function enforceEager(value: TRuleValue): TEagerRules {
}

export type TEnforceEager = typeof enforceEager;

type TEagerRules = Record<string, (...args: TArgs) => TEagerRules> &
{
[P in keyof TCompounds]: (
...args: DropFirst<Parameters<TCompounds[P]>>
) => TEagerRules;
} &
{
[P in KBaseRules]: (
...args: DropFirst<Parameters<TBaseRules[P]>>
) => TEagerRules;
};
25 changes: 3 additions & 22 deletions packages/n4s/src/runtime/genEnforceLazy.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import mapFirst from 'mapFirst';
import { DropFirst } from 'utilityTypes';

import type { TCompounds } from 'compounds';
import eachEnforceRule from 'eachEnforceRule';
import type { TEnforce } from 'enforce';
import { ctx } from 'enforceContext';
import isProxySupported from 'isProxySupported';
import type { TRuleDetailedResult, TLazyRuleMethods } from 'ruleReturn';
import * as ruleReturn from 'ruleReturn';
import {
TRuleValue,
TArgs,
KBaseRules,
TBaseRules,
getRule,
} from 'runtimeRules';
import { TRuleValue, TArgs, KBaseRules, getRule, TRules } from 'runtimeRules';
import { transformResult } from 'transformResult';

type TRegisteredRules = Array<(value: TRuleValue) => TRuleDetailedResult>;
Expand All @@ -35,7 +26,7 @@ export default function genEnforceLazy(key: string) {
let proxy = {
run: genRun(),
test: genTest(),
} as TEnforce;
} as TLazy;

if (!isProxySupported()) {
eachEnforceRule((ruleName: KBaseRules) => {
Expand Down Expand Up @@ -79,17 +70,7 @@ export default function genEnforceLazy(key: string) {
}
}

export type TLazyRules = Record<string, (...args: TArgs) => TLazy> &
{
[P in keyof TCompounds]: (
...args: DropFirst<Parameters<TCompounds[P]>> | TArgs
) => TLazy;
} &
{
[P in KBaseRules]: (
...args: DropFirst<Parameters<TBaseRules[P]>> | TArgs
) => TLazy;
};
export type TLazyRules = TRules<TLazyRuleMethods>;

export type TLazy = TLazyRules & TLazyRuleMethods;

Expand Down
19 changes: 18 additions & 1 deletion packages/n4s/src/runtime/runtimeRules.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import compounds from 'compounds';
import type { DropFirst } from 'utilityTypes';

import compounds, { KCompounds, TCompounds } from 'compounds';
import type { TRuleReturn } from 'ruleReturn';
import rules from 'rules';
import schema from 'schema';
Expand All @@ -21,3 +23,18 @@ function getRule(ruleName: string): TRuleBase {
}

export { baseRules, getRule };

export type TRules<E = Record<string, unknown>> = Record<
string,
(...args: TArgs) => TRules & E
> &
{
[P in KCompounds]: (
...args: DropFirst<Parameters<TCompounds[P]>> | TArgs
) => TRules & E;
} &
{
[P in KBaseRules]: (
...args: DropFirst<Parameters<TBaseRules[P]>> | TArgs
) => TRules & E;
};
4 changes: 4 additions & 0 deletions packages/n4s/src/schema/__tests__/shape&loose.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import enforce from 'enforce';
import * as ruleReturn from 'ruleReturn';

/* eslint-disable sort-keys */

// The base behavior of 'loose' and 'shape' is practically the same
// so we cover them using the same tests.
describe.each(['loose', 'shape'])('enforce.%s', (methodName: string) => {
Expand Down Expand Up @@ -134,3 +136,5 @@ describe.each(['loose', 'shape'])('enforce.%s', (methodName: string) => {
});
});
});

/* eslint-enable sort-keys */
4 changes: 2 additions & 2 deletions packages/n4s/src/schema/shape.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import hasOwnProperty from 'hasOwnProperty';

import type { TLazy } from 'genEnforceLazy';
import type { TShapeObject } from 'genEnforceLazy';
import loose from 'loose';
import type { TRuleDetailedResult } from 'ruleReturn';
import * as ruleReturn from 'ruleReturn';

export default function shape(
inputObject: Record<string, any>,
shapeObject: Record<string, TLazy>
shapeObject: TShapeObject
): TRuleDetailedResult {
const baseRes = loose(inputObject, shapeObject);
if (!baseRes.pass) {
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/utilityTypes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type DropFirst<T extends unknown[]> = T extends [unknown, ...infer U]
? U
: never;

export type TStringable = string | ((...args: any[]) => string);
32 changes: 17 additions & 15 deletions packages/vast/src/vast.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import isFunction from 'isFunction';
import optionalFunctionValue from 'optionalFunctionValue';

type TStateInput<S> = S | (() => S);
type TSetStateInput<S> = S | ((prevState: S) => S);
export type TStateHandlerReturn<S> = [
S,
(nextState: TSetStateInput<S>) => void
];

type TCreateStateReturn = {
reset: () => void;
registerStateKey: <S>(
initialState?: TStateInput<S> | undefined,
onUpdate?: (() => void) | undefined
) => () => TStateHandlerReturn<S>;
};

export default function createState(
onStateChange?: (...args: unknown[]) => unknown
): TCreateStateReturn {
Expand Down Expand Up @@ -96,3 +81,20 @@ export default function createState(
}
}
}

type TStateInput<S> = S | (() => S);
type TSetStateInput<S> = S | ((prevState: S) => S);

export type TState = ReturnType<typeof createState>;
export type TStateHandlerReturn<S> = [
S,
(nextState: TSetStateInput<S>) => void
];

type TCreateStateReturn = {
reset: () => void;
registerStateKey: <S>(
initialState?: TStateInput<S> | undefined,
onUpdate?: (() => void) | undefined
) => () => TStateHandlerReturn<S>;
};
4 changes: 2 additions & 2 deletions packages/vest/src/core/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assign from 'assign';
import createContext from 'context';

import VestTest from 'VestTest';
import createStateRef from 'createStateRef';
import type { TStateRef } from 'createStateRef';

export default createContext<CTXType>((ctxRef, parentContext) =>
parentContext
Expand All @@ -20,7 +20,7 @@ export default createContext<CTXType>((ctxRef, parentContext) =>
);

type CTXType = {
stateRef?: ReturnType<typeof createStateRef>;
stateRef?: TStateRef;
exclusion: {
tests: Record<string, boolean>;
groups: Record<string, boolean>;
Expand Down
6 changes: 4 additions & 2 deletions packages/vest/src/core/state/createStateRef.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import createState from 'vast';
import type { TState } from 'vast';

import VestTest from 'VestTest';
import type { TDraftResult } from 'produceDraft';

export default function createStateRef(
state: ReturnType<typeof createState>,
state: TState,
{ suiteId }: { suiteId: string }
) {
return {
Expand All @@ -24,3 +24,5 @@ export default function createStateRef(
testObjects: state.registerStateKey<VestTest[]>(() => []),
};
}

export type TStateRef = ReturnType<typeof createStateRef>;
Loading

0 comments on commit eeac20e

Please sign in to comment.