selectorator
selectorator
is an abstraction API for creating selectors via reselect with less boilerplate code.
Table of contents
Installation
$ npm i selectorator --save
Versions
Versions of selectorator
on or above 3.x.x
will use the corresponding major version of reselect
as a dependency. If you wish to still use the 2.x.x
branch of reselect
for your application, then you should continue using the 1.x.x
branch of selectorator
.
If you would like to learn more about the breaking changes related to the major version change for reselect
, please visit the reselect
CHANGELOG.
Usage
; // selector created with single method callconst getBarBaz = ; const state = foo: bar: "bar" baz: "baz"; console; // "bar baz"
Not a whole lot of magic here, just simplifying the creation of the "identity selectors" that reselect
requires, instead replacing them with a standardized dot- or bracket-notation string for retrieval of a nested property in the state object.
That said, you can still use your own custom identity selectors, or compose selectors, if you so choose. Here is the example from the reselect
README modified to use selectorator
:
// subtotal built using simple methodconst getSubtotal = ; // tax builtrued with simple method combined with other selectorconst getTax = ; // total build entirely with other selectorsconst getTotal = ; const state = shop: taxPercent: 8 items: name: "apple" value: 12 name: "orange" value: 095 ; console; // 2.15console; // 0.172console; // {total: 2.322}
Shorthand types
The following types of shorthand are available for parameter selector creation:
- Pulls from state:
string
=> `'foo[0].bar'number
=>0
Array
=>['foo', 0, 'bar']
- Pulls from specific argument:
Object
=>{path: 'foo[0].bar', argIndex: 1}
Please note that the Object
usage is the only approach that will allow for selection of parameters. All other shorthands will pull from the first parameter.
TypeScript
Selectorator now supports two optional type parameters, it accepts an Input type param (usually the redux state) and the expected output type.
When creating a selector that accepts multiple params, the state should be array of the input types example
i.e createSelector<[State, number[], boolean], string>
; interface State foo: bar: string; ; baz: string; ; // State is input type, string is output type const getBarBaz = createSelector<State string> "foo.bar" "baz" { return ` `; }; // getBarBaz() has type signature: (state: State) => string; const getBarBaz2 = createSelector<any string> "foo.bar" "baz" { return ` `; }; // getBarBaz2() has type signature: (state: any) => string; const getBarBaz3 = ; // getBarBaz3() has type signature: (state: any) => any; const getBarBaz4 = ; // getBarBaz4() has type signature: (...state: any[]) => any; const getBarBazQux5 = createSelector<State string string> "foo.bar" "baz" path: 0 argIndex: 2 { return ` `; }; // getBarBaz5() has type signature: (state_0: State, state_1: string[]) => string; const getStucturedBarBaz = ; // getStructuredBarBaz() has type signature: (state: any) => ({ barBaz: string });
Options
All the capabilities that exist with reselect
are still available using selectorator
, they are just passed as an object of options to createSelector
.
deepEqual
defaults to false
A common usage of custom selectors is to perform a deep equality check instead of the standard strict equality check when comparing values. To apply this, simply set deepEqual
to true
.
; const selectoratorOptions = deepEqual: true; const getBaz = ;
isEqual
defaults to isSameValueZero
If you want to use a custom equality comparator, pass the method as this option.
; const selectoratorOptions = // silly example checking current or next values related to "foo" { return currentFoo === "foo" || nextFoo !== "foo"; }; const getFoo = ;
Please note that if this parameter is provided and deepEqual
is also set to true
, deepEqual
will take priority and the isEqual
method will not be used.
memoizer
defaults to reselect
defaultMemoize
If you want to use a custom memoizer, pass the method as this option. This will use createSelectorCreator
from reselect
internally, so consult their documentation on proper usage.
;; const selectoratorOptions = memoizer: moize; const getFoo = ;
memoizerParams
defaults to []
reselect
allows you to pass parameters to the memoizer
function, and this array will translate directly into parameters 3
-n
. This is useful if your memoizer
uses something other than direct comparison for its equality test.
; const selectoratorOptions = memoizer: memoizerThatChecksEqualToEachOtherOrToSpecificValuePassed memoizerParams: "specificValue"; const getFoo = ;
Development
Standard stuff, clone the repo and npm install
dependencies. The npm scripts available:
build
=> run webpack to build developmentdist
file with NODE_ENV=developmentbuild:minifed
=> run webpack to build productiondist
file with NODE_ENV=productiondev
=> run webpack dev server to run example app (playground!)docs
=> builds the docs viajsdoc
lint
=> run ESLint against all files in thesrc
folderprepublish
=> runsprepublish:compile
prepublish:compile
=> runlint
,test:coverage
,transpile
,build
,build:minified
, anddocs
test
=> run AVA test functions withNODE_ENV=test
test:coverage
=> runtest
but withnyc
for coverage checkertest:watch
=> runtest
, but with persistent watchertranspile
=> run babel against all files insrc
to create files inlib