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

Introduce differentiated constructors #23

Open
wants to merge 1 commit into
base: attempt-removal-of-duplication
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
51 changes: 44 additions & 7 deletions src/ReactDream.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import withDebugger from '@hocs/with-debugger'
import withLog from '@hocs/with-log'
import doAp from './internals/doAp'
import doConcat from './internals/doConcat'
import doContramap from './internals/doContramap'
import doStatefulContramap from './internals/stateful/doContramap'
import doStatelessContramap from './internals/stateless/doContramap'
import doMap from './internals/doMap'
import doPromap from './internals/doPromap'
import doRotate from './internals/doRotate'
Expand All @@ -31,9 +32,13 @@ const map = Component => higherOrderComponent => ReactDream(doMap(higherOrderCom
const concat = Component => OtherComponent =>
ReactDream(doConcat(OtherComponent.Component)(Component))

// contramap : Component -> (a -> Props) -> ReactDream
const contramap = Component => propsPreprocessor =>
ReactDream(doContramap(propsPreprocessor)(Component))
// statelessContramap : Component -> (a -> Props) -> ReactDream
const statelessContramap = Component => propsPreprocessor =>
ReactDream(doStatelessContramap(propsPreprocessor)(Component))

// statefulContramap : Component -> (a -> Props) -> ReactDream
const statefulContramap = Component => propsPreprocessor =>
ReactDream(doStatefulContramap(propsPreprocessor)(Component))

// promap : Component -> (a -> Props) -> (Component -> Component) -> ReactDream
const promap = Component => (propsPreprocessor, higherOrderComponent) =>
Expand Down Expand Up @@ -96,15 +101,42 @@ const style = Component => getStyleFromProps =>
// TYPE
// ////////////////////////////////////////////////////////////////////////// //

// ReactDream : Component -> ReactDream
const ReactDream = Component => ({
// ReactDream.Stateless : Component -> ReactDream
export const Stateless = Component => ({
Component,

// Algebras
ap: ap(Component),
chain: chain(Component),
concat: concat(Component),
contramap: statelessContramap(Component),
map: map(Component),
promap: promap(Component),

// Custom helpers
addProps: addProps(Component),
debug: debug(Component),
defaultProps: defaultProps(Component),
fork: fork(Component),
name: name(Component),
log: log(Component),
propTypes: propTypes(Component),
removeProps: removeProps(Component),
rotate: rotate(Component),
scale: scale(Component),
style: style(Component),
translate: translate(Component),
})

// ReactDream.Stateful : Component -> ReactDream
export const Stateful = Component => ({
Component,

// Algebras
ap: ap(Component),
chain: chain(Component),
concat: concat(Component),
contramap: contramap(Component),
contramap: statefulContramap(Component),
map: map(Component),
promap: promap(Component),

Expand All @@ -123,6 +155,11 @@ const ReactDream = Component => ({
translate: translate(Component),
})

const ReactDream = Component =>
isReferentiallyTransparentFunctionComponent(Component)
? Stateless(Component)
: Stateful(Component)

ReactDream.of = ReactDream

export const of = ReactDream.of
Expand Down
2 changes: 1 addition & 1 deletion src/internals/doAp.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// doAp : (Component -> Component) -> ReactDream -> Component
export default higherOrderComponent => DreamComponent => DreamComponent.fork(higherOrderComponent)
export default higherOrderComponent => DreamComponent => higherOrderComponent(DreamComponent.Component)
119 changes: 0 additions & 119 deletions src/internals/doContramap.spec.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import React from 'react'
import compose from 'recompose/compose'
import getDisplayName from 'recompose/getDisplayName'
import isReferentiallyTransparentFunctionComponent from '../isReferentiallyTransparentFunctionComponent'

// doContramap : (a -> Props) -> Component -> Component
export default propsPreprocessor => Component => {
const Enhanced = isReferentiallyTransparentFunctionComponent(Component)
? compose(Component, propsPreprocessor)
: props => <Component {...propsPreprocessor(props)} />
const Enhanced = props => <Component {...propsPreprocessor(props)} />

Enhanced.displayName = getDisplayName(Component)

Expand Down
86 changes: 86 additions & 0 deletions src/internals/stateful/doContramap.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import React, { Component } from 'react'
import { create } from 'react-test-renderer'
import doContramap from './doContramap'
import { equal } from 'assert'

describe('Stateful / doContramap', () => {
it('returns a new component that wraps building the inner component with the propsPreprocesssor filtering the props', () => {
class NotReferentiallyTransparent extends Component {
constructor() {
super()

this.state = {}
}

render() {
return (
<div>
{this.props.name}
</div>
)
}
}

const propsPreprocesssor = () => ({ name: 'Regina Spektor' })

const Enhanced = doContramap(propsPreprocesssor)(NotReferentiallyTransparent)

const renderer = create(<Enhanced />)

equal(renderer.toJSON().children, 'Regina Spektor')
})

describe('it has name', () => {
it('preserves the name as displayName', () => {
class NotReferentiallyTransparent extends Component {
constructor() {
super()

this.state = {}
}

render() {
return (
<div>
{this.props.name}
</div>
)
}
}

const propsPreprocesssor = () => ({ name: 'Regina Spektor' })

const Enhanced = doContramap(propsPreprocesssor)(NotReferentiallyTransparent)

equal(Enhanced.displayName, 'NotReferentiallyTransparent')
})
})

describe('it has displayName', () => {
it('preserves it', () => {
class NotReferentiallyTransparent extends Component {
constructor() {
super()

this.state = {}
}

render() {
return (
<div>
{this.props.name}
</div>
)
}
}

NotReferentiallyTransparent.displayName = 'BeginToHope'

const propsPreprocesssor = () => ({ name: 'Regina Spektor' })

const Enhanced = doContramap(propsPreprocesssor)(NotReferentiallyTransparent)

equal(Enhanced.displayName, 'BeginToHope')
})
})
})
12 changes: 12 additions & 0 deletions src/internals/stateless/doContramap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react'
import compose from 'recompose/compose'
import getDisplayName from 'recompose/getDisplayName'

// doContramap : (a -> Props) -> Component -> Component
export default propsPreprocessor => Component => {
const Enhanced = compose(Component, propsPreprocessor)

Enhanced.displayName = getDisplayName(Component)

return Enhanced
}
35 changes: 35 additions & 0 deletions src/internals/stateless/doContramap.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { Component } from 'react'
import { create } from 'react-test-renderer'
import doContramap from './doContramap'
import { equal } from 'assert'

describe('Stateless / doContramap', () => {
it('pre composes the propsPreprocesssor', () => {
const ReferentiallyTransparentComponent = x => !x
const propsPreprocessor = () => true

equal(doContramap(propsPreprocessor)(ReferentiallyTransparentComponent)(), false)
})

describe('it has name', () => {
it('preserves the name as displayName', () => {
function ReferentiallyTransparentComponent(x) {
return x
}

equal(
doContramap(x => x)(ReferentiallyTransparentComponent).displayName,
'ReferentiallyTransparentComponent'
)
})
})

describe('it has displayName', () => {
it('preserves it', () => {
const ReferentiallyTransparentComponent = x => x
ReferentiallyTransparentComponent.displayName = 'Casablanca'

equal(doContramap(x => x)(ReferentiallyTransparentComponent).displayName, 'Casablanca')
})
})
})