Skip to content

Instantly share code, notes, and snippets.

@chadedrupt
Created August 20, 2019 06:19
Show Gist options
  • Save chadedrupt/6b55cb096ae882ae00fe7a51405dd8bb to your computer and use it in GitHub Desktop.
Save chadedrupt/6b55cb096ae882ae00fe7a51405dd8bb to your computer and use it in GitHub Desktop.
mapSelector for reselect
import { createSelector, Selector } from 'reselect'
import { isNil } from 'lodash'
export default function mapSelector<S, A, B>(
selector: Selector<S, A>,
mapping: (a: NonNullable<A>) => B
) {
return createSelector(
selector,
value => {
if (isNil(value)) return
return mapping(value as NonNullable<A>)
}
)
}
@chadedrupt
Copy link
Author

chadedrupt commented Aug 20, 2019

This:

const getSomeInnerValue = createSelector(
    getSomePossiblyNilValue,
    value => value && value.someInnerValye
)

can become this:

const getSomeInnerValue = mapSelector(
    getSomePossiblyNilValue,
    value => value.someInnerValye
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment