From 37c029dd152c1d5f2041c2d721df0470a0c21181 Mon Sep 17 00:00:00 2001 From: Glazomer Date: Fri, 1 May 2020 16:45:52 +1000 Subject: [PATCH] refactor(core): made compose into independent file and re-exported it --- packages/core/.gitignore | 1 + packages/core/compose.ts | 153 ++++++++++++++++++++++++++++++++++++++ packages/core/core.ts | 154 +-------------------------------------- 3 files changed, 156 insertions(+), 152 deletions(-) create mode 100644 packages/core/compose.ts diff --git a/packages/core/.gitignore b/packages/core/.gitignore index d0318021..64095251 100644 --- a/packages/core/.gitignore +++ b/packages/core/.gitignore @@ -1 +1,2 @@ core.d.ts +compose.d.ts diff --git a/packages/core/compose.ts b/packages/core/compose.ts new file mode 100644 index 00000000..2e458bc4 --- /dev/null +++ b/packages/core/compose.ts @@ -0,0 +1,153 @@ +import { ComponentType, StatelessComponent } from 'react' + +export type ExtractProps = T extends ComponentType ? { [P in keyof K]: K[P] } : never +export type HOC = (WrappedComponent: ComponentType) => ComponentType +export type Wrapper = HOC +export type Composition = >( + fn: U, +) => StatelessComponent> & T> + +export function compose(fn1: HOC): Composition + +export function compose(fn1: HOC, fn2: HOC): Composition + +export function compose( + fn1: HOC, + fn2: HOC, + fn3: HOC, +): Composition + +export function compose( + fn1: HOC, + fn2: HOC, + fn3: HOC, + fn4: HOC, +): Composition + +export function compose( + fn1: HOC, + fn2: HOC, + fn3: HOC, + fn4: HOC, + fn5: HOC, +): Composition + +export function compose( + fn1: HOC, + fn2: HOC, + fn3: HOC, + fn4: HOC, + fn5: HOC, + fn6: HOC, +): Composition + +export function compose( + fn1: HOC, + fn2: HOC, + fn3: HOC, + fn4: HOC, + fn5: HOC, + fn6: HOC, + fn7: HOC, +): Composition + +export function compose( + fn1: HOC, + fn2: HOC, + fn3: HOC, + fn4: HOC, + fn5: HOC, + fn6: HOC, + fn7: HOC, + fn8: HOC, +): Composition + +export function compose(...fns: Array>): Composition + +/** + * @param funcs higher order components + * + * @example + * ```ts + * import { compose } from '@bem-react/core'; + * + * const Enhanced = compose( + * withBemMod('Component', { size: 's' }), + * withBemMod('Component', { theme: 'normal' }), + * )(Component); + * ``` + */ +export function compose() { + // Use arguments instead of rest-arguments to get faster and more compact code. + const fns: any[] = [].slice.call(arguments) + + return fns.reduce( + (a, b) => { + return function() { + return a(b.apply(0, arguments)) + } + }, + (arg: any) => arg, + ) +} + +export function composeU(fn1: HOC): Composition + +export function composeU(fn1: HOC, fn2: HOC): Composition + +export function composeU( + fn1: HOC, + fn2: HOC, + fn3: HOC, +): Composition + +export function composeU( + fn1: HOC, + fn2: HOC, + fn3: HOC, + fn4: HOC, +): Composition + +export function composeU( + fn1: HOC, + fn2: HOC, + fn3: HOC, + fn4: HOC, + fn5: HOC, +): Composition + +export function composeU( + fn1: HOC, + fn2: HOC, + fn3: HOC, + fn4: HOC, + fn5: HOC, + fn6: HOC, +): Composition + +export function composeU( + fn1: HOC, + fn2: HOC, + fn3: HOC, + fn4: HOC, + fn5: HOC, + fn6: HOC, + fn7: HOC, +): Composition + +export function composeU( + fn1: HOC, + fn2: HOC, + fn3: HOC, + fn4: HOC, + fn5: HOC, + fn6: HOC, + fn7: HOC, + fn8: HOC, +): Composition + +export function composeU(...fns: Array>): Composition + +export function composeU(...args: any[]) { + return compose(...args) +} diff --git a/packages/core/core.ts b/packages/core/core.ts index 12a7ab39..6d1cc8c1 100644 --- a/packages/core/core.ts +++ b/packages/core/core.ts @@ -1,4 +1,4 @@ -import { ComponentType, StatelessComponent, createElement } from 'react' +import { ComponentType, createElement } from 'react' import { cn, NoStrictEntityMods, ClassNameFormatter } from '@bem-react/classname' import { classnames } from '@bem-react/classnames' @@ -127,154 +127,4 @@ export function withBemMod( } } -export type ExtractProps = T extends ComponentType ? { [P in keyof K]: K[P] } : never -export type HOC = (WrappedComponent: ComponentType) => ComponentType -export type Wrapper = HOC -export type Composition = >( - fn: U, -) => StatelessComponent> & T> - -export function compose(fn1: HOC): Composition - -export function compose(fn1: HOC, fn2: HOC): Composition - -export function compose( - fn1: HOC, - fn2: HOC, - fn3: HOC, -): Composition - -export function compose( - fn1: HOC, - fn2: HOC, - fn3: HOC, - fn4: HOC, -): Composition - -export function compose( - fn1: HOC, - fn2: HOC, - fn3: HOC, - fn4: HOC, - fn5: HOC, -): Composition - -export function compose( - fn1: HOC, - fn2: HOC, - fn3: HOC, - fn4: HOC, - fn5: HOC, - fn6: HOC, -): Composition - -export function compose( - fn1: HOC, - fn2: HOC, - fn3: HOC, - fn4: HOC, - fn5: HOC, - fn6: HOC, - fn7: HOC, -): Composition - -export function compose( - fn1: HOC, - fn2: HOC, - fn3: HOC, - fn4: HOC, - fn5: HOC, - fn6: HOC, - fn7: HOC, - fn8: HOC, -): Composition - -export function compose(...fns: Array>): Composition - -/** - * @param funcs higher order components - * - * @example - * ```ts - * import { compose } from '@bem-react/core'; - * - * const Enhanced = compose( - * withBemMod('Component', { size: 's' }), - * withBemMod('Component', { theme: 'normal' }), - * )(Component); - * ``` - */ -export function compose() { - // Use arguments instead of rest-arguments to get faster and more compact code. - const fns: any[] = [].slice.call(arguments) - - return fns.reduce( - (a, b) => { - return function() { - return a(b.apply(0, arguments)) - } - }, - (arg: any) => arg, - ) -} - -export function composeU(fn1: HOC): Composition - -export function composeU(fn1: HOC, fn2: HOC): Composition - -export function composeU( - fn1: HOC, - fn2: HOC, - fn3: HOC, -): Composition - -export function composeU( - fn1: HOC, - fn2: HOC, - fn3: HOC, - fn4: HOC, -): Composition - -export function composeU( - fn1: HOC, - fn2: HOC, - fn3: HOC, - fn4: HOC, - fn5: HOC, -): Composition - -export function composeU( - fn1: HOC, - fn2: HOC, - fn3: HOC, - fn4: HOC, - fn5: HOC, - fn6: HOC, -): Composition - -export function composeU( - fn1: HOC, - fn2: HOC, - fn3: HOC, - fn4: HOC, - fn5: HOC, - fn6: HOC, - fn7: HOC, -): Composition - -export function composeU( - fn1: HOC, - fn2: HOC, - fn3: HOC, - fn4: HOC, - fn5: HOC, - fn6: HOC, - fn7: HOC, - fn8: HOC, -): Composition - -export function composeU(...fns: Array>): Composition - -export function composeU(...args: any[]) { - return compose(...args) -} +export * from './compose'