Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed May 6, 2024
1 parent b38b404 commit dba8e10
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
4 changes: 2 additions & 2 deletions packages/universal/modifier/src/modifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ export function ElementPlaceholder<E extends anydom.Element>(

initialize(value: anydom.Element): void {
const element = verified(REFS.get(ref), isPresent);
verify(
verify?.(
value,
(anyElement): anyElement is E => anyElement instanceof type,
expected(`A ref (${describe(description)})`)
expected?.(`A ref (${describe(description)})`)
.toBe(`initialized with an instance of ${type.name}`)
.butGot(() => `an instance of ${value.constructor.name}`),
);
Expand Down
19 changes: 12 additions & 7 deletions packages/universal/verify/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { isOneOf as isOneOfDev } from "./src/assertions/multi.js";
import { hasType as hasTypeDev } from "./src/assertions/types.js";
import type { VerifyFn } from "./src/verify.js";
import {
expected as expectedDev
} from './src/verify.js';
expected as expectedDev,
verify as verifyDev
} from "./src/verify.js";

export {
verified,
verify
verified
} from "./src/verify.js";

const noop = () => { };
const noop: unknown = () => { };

export {
exhaustive,
Expand All @@ -25,6 +26,10 @@ export {
export { type TypeOf } from "./src/assertions/types.js";
export { type Expectation, VerificationError } from "./src/verify.js";

export const expected: typeof expectedDev | null = import.meta.env.DEV ? expectedDev : null
export const hasType: typeof hasTypeDev = import.meta.env.DEV ? hasTypeDev : (noop as unknown as typeof hasTypeDev)
export const expected: typeof expectedDev = import.meta.env.DEV ? expectedDev : (noop as typeof expectedDev)
export const hasType: typeof hasTypeDev = import.meta.env.DEV ? hasTypeDev : (noop as typeof hasTypeDev)
export const isOneOf: typeof isOneOfDev = import.meta.env.DEV ? isOneOfDev : (noop as typeof isOneOfDev)

export const verify: VerifyFn | null = import.meta.env.DEV
? verifyDev
: null;
24 changes: 8 additions & 16 deletions packages/universal/verify/src/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export function verify<Value, Narrow extends Value>(
check: (input: Value) => input is Narrow,
error?: Expectation<Value>,
): asserts value is Narrow;
export function verify() {
export function verify(): void {
// eslint-disable-next-line prefer-rest-params
const params = [...arguments] as Parameters<typeof verifyFunc>;
if (import.meta.env.DEV) {
verifyFunc(...params)
Expand All @@ -46,29 +47,20 @@ function verifyFunc(
}
}

function noop(): void {
return;
}

export function verified<T, U extends T>(
value: T,
check: (input: T) => input is U,
error?: Expectation<T>,
): U {
): U;

export function verified(): unknown {
// eslint-disable-next-line prefer-rest-params
const [value, check, error] = [...arguments] as Parameters<typeof verified>;
if (import.meta.env.DEV) {
verify(value, check, error);
}
return value as unknown as U;
return value;
}

verified.noop = <const T, const U extends T>(
value: T,
_check: (input: T) => input is U,
_error?: Expectation<T>,
): U => {
return value as U;
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class Expectation<In = any> {
static create<In>(description?: string): Expectation<In> {
Expand Down

0 comments on commit dba8e10

Please sign in to comment.