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

Add globalizeSelectors #2

Merged
merged 1 commit into from
Dec 1, 2016
Merged
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: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ npm install --save zeal-redux-utils
## Usage

```javascript
import { createActionTypes, createReducer } from 'zeal-redux-utils'
import {
createActionTypes,
createReducer,
globalizeSelectors
} from 'zeal-redux-utils'
```
32 changes: 32 additions & 0 deletions src/__tests__/globalizeSelectors-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { prop } from 'ramda'

import globalizeSelectors from '../globalizeSelectors'

describe('globalizeSelectors', () => {
const globalState = {
section: {
numbers: [2, 3, 4]
}
}

const localState = prop('section')

const selectors = {
numbers: prop('numbers'),
numberAt: (index, state) => state.numbers[index]
}

const globalized = globalizeSelectors(localState, selectors)

context('with a single-argument selector', () => {
test('allows the selector to work from the global state', () => {
expect(globalized.numbers(globalState)).toEqual([2, 3, 4])
})
})

context('with a multi-argument selector', () => {
test('allows the selector to work from the global state', () => {
expect(globalized.numberAt(1, globalState)).toEqual(3)
})
})
})
29 changes: 29 additions & 0 deletions src/globalizeSelectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { adjust, map } from 'ramda'

/*
Take a local state transform and an object containing selector functions.
* The local state transform is a function that takes the global state and
returns the appropriate sub-section of the state tree (aka local state).
* The selector functions are written to work with the local state.
Return a new object of selector functions. The output selector functions now
work with the global state instead.
To do this, we map a `globalize` function over the selectors.
We assume that the state argument is always the last one, and we want to
transform it by applying the local state. We use Ramda's `adjust` function
to do that.
The result of `globalize` is a function that takes the global state as its last
argument, transforms that state into local state, and then calls the original,
localized, selector.
*/

const globalize = transform => selector => (...args) =>
selector(...adjust(transform, -1, args))

export default (localStateTransform, selectors) =>
map(globalize(localStateTransform), selectors)
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export createActionTypes from './createActionTypes'
export createReducer from './createReducer'
export globalizeSelectors from './globalizeSelectors'