/chainable-selectors

Wrapper for reselect that enables chaining.

Primary LanguageJavaScriptMIT LicenseMIT

Experimental library at this point

Getting started

THIS FUNCTIONALLITY UNDER DEVELOPMENT

In your initialization file

import { setSelectorCreator } from 'chainable-selectors';
import { createSelector } from 'reselect';

setSelectorCreator(createSelector);

This will take any library that uses reselect's basic api footprint. createSelector(sel1, sel2, ... , callback = (sel1Output, sel2Output) => ...)

Using Chainable Selectors

import { select } from 'chainable-selectors';
import { getIds, getHash } from './my-selectors';

const hiNames = select(ids)
  .map(hash, (id, h) => h[id])
  .reduce((prev = [], item) => {
    return [...prev, item.name];
  })
  .map((name) => `Hi ${name}`)
  .create();

Supported Methods

Map

Loop through previous output.

select(<selector>).
  .map(
    ...<dependent selectors>,
    <callback: ( <item at index>, ...<dependent selector output> )>
  )

Example:

.map(hash, (id, h) => h[id])

Reduce

Reduce previous output.

select(<selector>).
  .reduce(
    ...<dependent selectors>,
    <callback: ( <prev reduced value> = initial, <item at index>, ...<dependent selector output> )>
  )

Example:

.reduce((prev = [], item) => {
  return [...prev, item.id];
})

Use

Use custom function on previous output.

select(<selector>).
  .use(
    ...<dependent selectors>,
    <callback: ( <prev transform output>, ...<dependent selector output> )>
  )

Example:

.use(getSelectedId, (prevOutput, selectedId) =>
  R.equals(selectedId)(prevOutput.id)
)
.use(getSelectedId, hasSelectedId)