From 5b5084e89f4ad3c0e042aadf6d8377299060a6ea Mon Sep 17 00:00:00 2001 From: Igor Kamyshev Date: Tue, 30 Jul 2024 16:21:01 +0700 Subject: [PATCH] Delete useless overload --- packages/contracts/package.json | 2 +- packages/contracts/src/contracts.test.ts | 36 +------------------- packages/contracts/src/index.ts | 43 ++++-------------------- 3 files changed, 9 insertions(+), 72 deletions(-) diff --git a/packages/contracts/package.json b/packages/contracts/package.json index 254333e..8530c24 100644 --- a/packages/contracts/package.json +++ b/packages/contracts/package.json @@ -32,7 +32,7 @@ "size-limit": [ { "path": "./dist/contracts.js", - "limit": "885 B" + "limit": "863 B" } ] } diff --git a/packages/contracts/src/contracts.test.ts b/packages/contracts/src/contracts.test.ts index 4560cc6..8d2c0ec 100644 --- a/packages/contracts/src/contracts.test.ts +++ b/packages/contracts/src/contracts.test.ts @@ -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) => [ diff --git a/packages/contracts/src/index.ts b/packages/contracts/src/index.ts index 7487f72..9f08a76 100644 --- a/packages/contracts/src/index.ts +++ b/packages/contracts/src/index.ts @@ -352,29 +352,10 @@ export function tuple(...contracts: Array>): 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( - fn: (config: P) => Contract -): (config: P) => Contract; - /** * 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) => ({ @@ -386,30 +367,20 @@ export function contract( export function contract( base: Contract, fn: (config: P) => Contract -): (config: P) => Contract; - -export function contract( - base: Contract | ((config: P) => Contract), - fn?: (config: P) => Contract -): (config: P) => Contract { - const creator: any = fn ? fn : (base as (config: P) => Contract); - const realBase = fn ? (base as Contract) : null; +): (config: P) => Contract { 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); }), }; };