-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |