Skip to content

Commit

Permalink
Add globalizeSelectors
Browse files Browse the repository at this point in the history
  • Loading branch information
lexun committed Dec 1, 2016
1 parent b5cbefb commit b3c5b23
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
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'

0 comments on commit b3c5b23

Please sign in to comment.