diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 0000000..01723aa --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,2 @@ +export * from './pattern-match'; +export * from './not'; diff --git a/src/utils/not.ts b/src/utils/not.ts new file mode 100644 index 0000000..be2dfc5 --- /dev/null +++ b/src/utils/not.ts @@ -0,0 +1 @@ +export { not } from 'ts-pattern'; diff --git a/src/utils/pattern-match.ts b/src/utils/pattern-match.ts new file mode 100644 index 0000000..0762208 --- /dev/null +++ b/src/utils/pattern-match.ts @@ -0,0 +1,31 @@ +import { ComponentType } from 'react'; + +import { + Match, + MatchProps, + When, + WhenProps, + With, + WithProps, + Otherwise, +} from '../components'; + +export const getPatternMatch = ( + // @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>; + const TypedWhen = When as ComponentType>; + const TypedWith = With as ComponentType>; + + return { Match: TypedMatch, When: TypedWhen, With: TypedWith, Otherwise }; +}; + +export const usePatternMatch = ( + value?: Shape, + strict?: boolean +) => getPatternMatch(value, strict);