Skip to content

Commit

Permalink
Delete useless overload
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Jul 30, 2024
1 parent 8eb2922 commit 5b5084e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 72 deletions.
2 changes: 1 addition & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"size-limit": [
{
"path": "./dist/contracts.js",
"limit": "885 B"
"limit": "863 B"
}
]
}
36 changes: 1 addition & 35 deletions packages/contracts/src/contracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,41 +549,7 @@ describe('tuple', () => {
});
});

describe('contract, no base', () => {
const onlySome = contract((x: number) => ({
isData: (t): t is number => t === x,
getErrorMessages: (t) => ['expected ' + x + ', got ' + t],
}));

const onlyOne = onlySome(1);
const onlyTwo = onlySome(2);

it('valid', () => {
expect(onlyOne.isData(1)).toBeTruthy();
expect(onlyOne.getErrorMessages(1)).toEqual([]);

expect(onlyTwo.isData(2)).toBeTruthy();
expect(onlyTwo.getErrorMessages(2)).toEqual([]);
});

it('invalid', () => {
expect(onlyOne.isData(2)).toBeFalsy();
expect(onlyOne.getErrorMessages(2)).toMatchInlineSnapshot(`
[
"expected 1, got 2",
]
`);

expect(onlyTwo.isData(1)).toBeFalsy();
expect(onlyTwo.getErrorMessages(1)).toMatchInlineSnapshot(`
[
"expected 2, got 1",
]
`);
});
});

describe('contract, with base', () => {
describe('contract', () => {
const age = contract(num, ({ min, max }: { min: number; max: number }) => ({
isData: (t): t is number => t >= min && t <= max,
getErrorMessages: (t) => [
Expand Down
43 changes: 7 additions & 36 deletions packages/contracts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,29 +352,10 @@ export function tuple(...contracts: Array<Contract<unknown, any>>): any {
};
}

/**
* Creates a _Contract_ based on a passed function.
*
* @overload "contract(fn)"
*
* @example
*
* const age = contract((min, max) => ({
* isData: (data) => typeof data === 'number' && data >= min && data <= max,
* getErrorMessages: (data) =>
* `Expected a number between ${min} and ${max}, but got ${data}`,
* }));
*/
export function contract<P, O>(
fn: (config: P) => Contract<unknown, O>
): (config: P) => Contract<unknown, O>;

/**
* Creates a _Contract_ based on a passed function and a base _Contract_.
* Base _Contract_ is checked first, then the _Contract_ created by the function.
*
* @overload "contract(base, fn)"
*
* @example
*
* const age = contract(num, (min, max) => ({
Expand All @@ -386,30 +367,20 @@ export function contract<P, O>(
export function contract<P, I, O extends I>(
base: Contract<unknown, I>,
fn: (config: P) => Contract<I, O>
): (config: P) => Contract<unknown, O>;

export function contract<P, I, O extends I>(
base: Contract<unknown, I> | ((config: P) => Contract<I, O>),
fn?: (config: P) => Contract<I, O>
): (config: P) => Contract<I, O> {
const creator: any = fn ? fn : (base as (config: P) => Contract<I, O>);
const realBase = fn ? (base as Contract<unknown, I>) : null;
): (config: P) => Contract<unknown, O> {
return (params: P) => {
const check = (data: unknown): data is O => {
if (realBase) {
return realBase.isData(data) && creator(params).isData(data);
}
return creator(params).isData(data);
};
const next = fn(params);
const check = (data: unknown): data is O =>
base.isData(data) && next.isData(data);

return {
isData: check,
getErrorMessages: createGetErrorMessages(check, (data) => {
if (realBase && !realBase.isData(data)) {
return realBase.getErrorMessages(data);
if (!base.isData(data)) {
return base.getErrorMessages(data);
}

return creator(params).getErrorMessages(data);
return next.getErrorMessages(data);
}),
};
};
Expand Down

0 comments on commit 5b5084e

Please sign in to comment.