Skip to content

Commit

Permalink
feat(src/utils): add utils to wrap components and reduce generic typi…
Browse files Browse the repository at this point in the history
…ng boilerplate
  • Loading branch information
ythecombinator committed Jan 14, 2022
1 parent a17f2ae commit 8885efb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './pattern-match';
export * from './not';
1 change: 1 addition & 0 deletions src/utils/not.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { not } from 'ts-pattern';
31 changes: 31 additions & 0 deletions src/utils/pattern-match.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ComponentType } from 'react';

import {
Match,
MatchProps,
When,
WhenProps,
With,
WithProps,
Otherwise,
} from '../components';

export const getPatternMatch = <Shape, Strict extends boolean>(
// @ts-ignore
// Reason: we want to enable developers to pass a parameter instead of a generic type.
value?: Shape,
// @ts-ignore
// Reason: we want to enable developers to pass a parameter instead of a generic type.
strict?: boolean
) => {
const TypedMatch = Match as ComponentType<MatchProps<Shape>>;
const TypedWhen = When as ComponentType<WhenProps<Shape>>;
const TypedWith = With as ComponentType<WithProps<Shape, Strict>>;

return { Match: TypedMatch, When: TypedWhen, With: TypedWith, Otherwise };
};

export const usePatternMatch = <Shape, Strict extends boolean>(
value?: Shape,
strict?: boolean
) => getPatternMatch<Shape, Strict>(value, strict);

0 comments on commit 8885efb

Please sign in to comment.