Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

add flattenProps #366

Closed
wants to merge 2 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion docs/API.md
Expand Up @@ -42,6 +42,7 @@ const PureComponent = pure(BaseComponent)
+ [`renameProp()`](#renameprop)
+ [`renameProps()`](#renameprops)
+ [`flattenProp()`](#flattenprop)
+ [`flattenProps()`](#flattenprops)
+ [`withState()`](#withstate)
+ [`withReducer()`](#withreducer)
+ [`branch()`](#branch)
Expand Down Expand Up @@ -212,6 +213,16 @@ flattenProp(

Flattens a prop so that its fields are spread out into the props object.

### `flattenProps()`

```js
flattenProps(
propNames: Array<string>
): HigherOrderComponent
```

Same as `flattenProp` but takes an array of prop names. Higher index props overwrite lower index props.

```js
const enhance = compose(
withProps({
Expand Down Expand Up @@ -343,7 +354,7 @@ const Post = enhance(({ title, author, content }) =>
renderNothing: HigherOrderComponent
```

A higher-order component that always renders `null`.
A higher-order component that always renders `null`.

This is useful in combination with another helper that expects a higher-order component, like `branch()`:

Expand Down
2 changes: 1 addition & 1 deletion src/packages/recompose/__tests__/flattenProp-test.js
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import { shallow } from 'enzyme'
import { flattenProp } from '../'

test('flattenProps flattens an object prop and spreads it into the top-level props object', () => {
test('flattenProp flattens an object prop and spreads it into the top-level props object', () => {
const Counter = flattenProp('data-state')('div')
expect(Counter.displayName).toBe('flattenProp(div)')

Expand Down
28 changes: 28 additions & 0 deletions src/packages/recompose/__tests__/flattenProps-test.js
@@ -0,0 +1,28 @@
import React from 'react'
import { shallow } from 'enzyme'
import { flattenProps } from '../'

test('flattenProps flattens object props and spreads them into the top-level props object', () => {
const Counter = flattenProps(['data-first', 'data-second'])('div')
expect(Counter.displayName).toBe('flattenProps(div)')

const wrapper = shallow(
<Counter
data-pass="through"
data-first={{ 'data-foo': 1 }}
data-second={{ 'data-bar': 2 }}
/>
)

expect(
wrapper.equals(
<div
data-pass="through"
data-first={{ 'data-foo': 1 }}
data-second={{ 'data-bar': 2 }}
data-foo={1}
data-bar={2}
/>
)
).toBe(true)
})
20 changes: 20 additions & 0 deletions src/packages/recompose/flattenProps.js
@@ -0,0 +1,20 @@
import createHelper from './createHelper'
import createEagerFactory from './createEagerFactory'

const flattenProps = propNames => BaseComponent => {
const factory = createEagerFactory(BaseComponent)

return props =>
factory({
...props,
...propNames.reduce(
(flattenedProps, propName) => ({
...flattenedProps,
...props[propName],
}),
{}
),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a "library-like" utility function, maybe it would be worth it to mutate in the reducer. It's ugly, sure, but it performs better:

      ...propNames.reduce(
        (flattenedProps, propName) =>
          Object.assign(flattenedProps, props[propName]),
        {}
      )

If a tree falls in the woods, does it make a sound?

If a pure function mutates some local data in order to produce an immutable return value, is that ok?

— Rich Hickey, Clojure

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

every pure function is impure at assembler level - istarkov :-)

})
}

export default createHelper(flattenProps, 'flattenProps')
1 change: 1 addition & 0 deletions src/packages/recompose/index.js
Expand Up @@ -7,6 +7,7 @@ export { default as defaultProps } from './defaultProps'
export { default as renameProp } from './renameProp'
export { default as renameProps } from './renameProps'
export { default as flattenProp } from './flattenProp'
export { default as flattenProps } from './flattenProps'
export { default as withState } from './withState'
export { default as withReducer } from './withReducer'
export { default as branch } from './branch'
Expand Down