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

Compose: implement compose natively instead of reexporting from Lodash #32734

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 0 additions & 6 deletions packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ For more details, you can refer to each Higher Order Component's README file. [A
Composes multiple higher-order components into a single higher-order component. Performs right-to-left function
composition, where each successive invocation is supplied the return value of the previous.

This is just a re-export of `lodash`'s `flowRight` function.

_Related_

- <https://docs-lodash.com/v4/flow-right/>

<a name="createHigherOrderComponent" href="#createHigherOrderComponent">#</a> **createHigherOrderComponent**

Given a function mapping a component to an enhanced component and modifier
Expand Down
14 changes: 0 additions & 14 deletions packages/compose/src/higher-order/compose.js

This file was deleted.

16 changes: 16 additions & 0 deletions packages/compose/src/higher-order/compose/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Composes multiple higher-order components into a single higher-order component. Performs right-to-left function
* composition, where each successive invocation is supplied the return value of the previous.
*/
export default function compose( ...funcs ) {
return function ( ...args ) {
const length = funcs.length;
let index = length - 1;
let result =
length > 0 ? funcs[ index ].apply( this, args ) : args[ 0 ];
while ( --index >= 0 ) {
result = funcs[ index ].call( this, result );
}
return result;
};
}
38 changes: 38 additions & 0 deletions packages/compose/src/higher-order/compose/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Internal dependencies
*/
import compose from '../';

describe( 'compose', () => {
it( 'composes empty array to identity that returns the first arg', () => {
const id = compose();
expect( id( 1 ) ).toBe( 1 );
expect( id( 'a', 'b' ) ).toBe( 'a' );
} );

it( 'composes functions together', () => {
const sumSquareInc = compose(
( a ) => a + 1,
( a ) => a ** 2,
( a, b ) => a + b
);
expect( sumSquareInc( 2, 3 ) ).toBe( 26 );
} );

it( 'composes while maintaining the this binding', () => {
const decorate = compose(
function ( s ) {
return s + this.suffix;
},
function ( s ) {
return this.prefix + s;
}
);
const obj = {
prefix: 'pre-',
suffix: '-ation',
decorate,
};
expect( obj.decorate( 'serv' ) ).toBe( 'pre-serv-ation' );
} );
} );
2 changes: 1 addition & 1 deletion packages/compose/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Utils
export { default as createHigherOrderComponent } from './utils/create-higher-order-component';

// Compose helper (aliased flowRight from Lodash)
// Compose helper
export { default as compose } from './higher-order/compose';

// Higher-order components
Expand Down