Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): made compose into independent file and re-exported it #558

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
core.d.ts
compose.d.ts
153 changes: 153 additions & 0 deletions packages/core/compose.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { ComponentType, StatelessComponent } from 'react'

export type ExtractProps<T> = T extends ComponentType<infer K> ? { [P in keyof K]: K[P] } : never
export type HOC<T> = (WrappedComponent: ComponentType) => ComponentType<T>
export type Wrapper<T> = HOC<T>
export type Composition<T> = <U extends ComponentType<any>>(
fn: U,
) => StatelessComponent<JSX.LibraryManagedAttributes<U, ExtractProps<U>> & T>

export function compose<T1>(fn1: HOC<T1>): Composition<T1>

export function compose<T1, T2>(fn1: HOC<T1>, fn2: HOC<T2>): Composition<T1 & T2>

export function compose<T1, T2, T3>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
): Composition<T1 & T2 & T3>

export function compose<T1, T2, T3, T4>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
): Composition<T1 & T2 & T3 & T4>

export function compose<T1, T2, T3, T4, T5>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
fn5: HOC<T5>,
): Composition<T1 & T2 & T3 & T4 & T5>

export function compose<T1, T2, T3, T4, T5, T6>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
fn5: HOC<T5>,
fn6: HOC<T6>,
): Composition<T1 & T2 & T3 & T4 & T5 & T6>

export function compose<T1, T2, T3, T4, T5, T6, T7>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
fn5: HOC<T5>,
fn6: HOC<T6>,
fn7: HOC<T7>,
): Composition<T1 & T2 & T3 & T4 & T5 & T6 & T7>

export function compose<T1, T2, T3, T4, T5, T6, T7, T8>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
fn5: HOC<T5>,
fn6: HOC<T6>,
fn7: HOC<T7>,
fn8: HOC<T8>,
): Composition<T1 & T2 & T3 & T4 & T5 & T6 & T7 & T8>

export function compose(...fns: Array<HOC<any>>): Composition<any>

/**
* @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<T1>(fn1: HOC<T1>): Composition<T1>

export function composeU<T1, T2>(fn1: HOC<T1>, fn2: HOC<T2>): Composition<T1 | T2>

export function composeU<T1, T2, T3>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
): Composition<T1 | T2 | T3>

export function composeU<T1, T2, T3, T4>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
): Composition<T1 | T2 | T3 | T4>

export function composeU<T1, T2, T3, T4, T5>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
fn5: HOC<T5>,
): Composition<T1 | T2 | T3 | T4 | T5>

export function composeU<T1, T2, T3, T4, T5, T6>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
fn5: HOC<T5>,
fn6: HOC<T6>,
): Composition<T1 | T2 | T3 | T4 | T5 | T6>

export function composeU<T1, T2, T3, T4, T5, T6, T7>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
fn5: HOC<T5>,
fn6: HOC<T6>,
fn7: HOC<T7>,
): Composition<T1 | T2 | T3 | T4 | T5 | T6 | T7>

export function composeU<T1, T2, T3, T4, T5, T6, T7, T8>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
fn5: HOC<T5>,
fn6: HOC<T6>,
fn7: HOC<T7>,
fn8: HOC<T8>,
): Composition<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8>

export function composeU(...fns: Array<HOC<any>>): Composition<any>

export function composeU(...args: any[]) {
return compose(...args)
}
154 changes: 2 additions & 152 deletions packages/core/core.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -127,154 +127,4 @@ export function withBemMod<T, U extends IClassNameProps = {}>(
}
}

export type ExtractProps<T> = T extends ComponentType<infer K> ? { [P in keyof K]: K[P] } : never
export type HOC<T> = (WrappedComponent: ComponentType) => ComponentType<T>
export type Wrapper<T> = HOC<T>
export type Composition<T> = <U extends ComponentType<any>>(
fn: U,
) => StatelessComponent<JSX.LibraryManagedAttributes<U, ExtractProps<U>> & T>

export function compose<T1>(fn1: HOC<T1>): Composition<T1>

export function compose<T1, T2>(fn1: HOC<T1>, fn2: HOC<T2>): Composition<T1 & T2>

export function compose<T1, T2, T3>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
): Composition<T1 & T2 & T3>

export function compose<T1, T2, T3, T4>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
): Composition<T1 & T2 & T3 & T4>

export function compose<T1, T2, T3, T4, T5>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
fn5: HOC<T5>,
): Composition<T1 & T2 & T3 & T4 & T5>

export function compose<T1, T2, T3, T4, T5, T6>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
fn5: HOC<T5>,
fn6: HOC<T6>,
): Composition<T1 & T2 & T3 & T4 & T5 & T6>

export function compose<T1, T2, T3, T4, T5, T6, T7>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
fn5: HOC<T5>,
fn6: HOC<T6>,
fn7: HOC<T7>,
): Composition<T1 & T2 & T3 & T4 & T5 & T6 & T7>

export function compose<T1, T2, T3, T4, T5, T6, T7, T8>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
fn5: HOC<T5>,
fn6: HOC<T6>,
fn7: HOC<T7>,
fn8: HOC<T8>,
): Composition<T1 & T2 & T3 & T4 & T5 & T6 & T7 & T8>

export function compose(...fns: Array<HOC<any>>): Composition<any>

/**
* @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<T1>(fn1: HOC<T1>): Composition<T1>

export function composeU<T1, T2>(fn1: HOC<T1>, fn2: HOC<T2>): Composition<T1 | T2>

export function composeU<T1, T2, T3>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
): Composition<T1 | T2 | T3>

export function composeU<T1, T2, T3, T4>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
): Composition<T1 | T2 | T3 | T4>

export function composeU<T1, T2, T3, T4, T5>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
fn5: HOC<T5>,
): Composition<T1 | T2 | T3 | T4 | T5>

export function composeU<T1, T2, T3, T4, T5, T6>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
fn5: HOC<T5>,
fn6: HOC<T6>,
): Composition<T1 | T2 | T3 | T4 | T5 | T6>

export function composeU<T1, T2, T3, T4, T5, T6, T7>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
fn5: HOC<T5>,
fn6: HOC<T6>,
fn7: HOC<T7>,
): Composition<T1 | T2 | T3 | T4 | T5 | T6 | T7>

export function composeU<T1, T2, T3, T4, T5, T6, T7, T8>(
fn1: HOC<T1>,
fn2: HOC<T2>,
fn3: HOC<T3>,
fn4: HOC<T4>,
fn5: HOC<T5>,
fn6: HOC<T6>,
fn7: HOC<T7>,
fn8: HOC<T8>,
): Composition<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8>

export function composeU(...fns: Array<HOC<any>>): Composition<any>

export function composeU(...args: any[]) {
return compose(...args)
}
export * from './compose'