npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-selectors

v1.0.3

Published

Simplified Selector Functions

Downloads

218

Readme

Simple Selectors

Travis npm package

npm i simple-selectors --save

Simple "selectors" library, forked and heavily based on the original reselect.

In brief:

  • Selectors can compute derived data, allowing Redux to store the minimal possible state.
  • Selectors are efficient. A selector is not recomputed unless one of its arguments change.
  • Selectors are composable. They can be used as input to other selectors.

For more detailed information about the rationale behind selectors, please visit the reselect documentation.

Differences from Reselect:

  • A selector takes a maximum of two arguments, (state) or (state, props) - variadic args are not permitted.
  • createSelector also covers createStructuredSelector when passed an object.
  • createSimpleSelector creates a non-memoized selector for situations where memoization is not helpful
  • Simplified memoization, only the equality operator can be configured via memoizedSelectorCreator
  • Simpler internals for easier debugging

API

createSelector(...inputSelectors | [inputSelectors], resultFunc)

createSelector(structuredSelector)

Takes one or more selectors, or an array of selectors, computes their values and passes them as arguments to resultFn.

createSelector determines if the value returned by an input-selector has changed between calls using reference equality (===). Inputs to selectors created with createSelector should be immutable.

Selectors created with createSelector have a cache size of 1. This means they always recalculate when the value of an input-selector changes, as a selector only stores the preceding value of each input-selector.

const mySelector = createSelector(
  state => state.values.value1,
  state => state.values.value2,
  (value1, value2) => value1 + value2
)

// You can also pass an array of selectors
const totalSelector = createSelector(
  [
    state => state.values.value1,
    state => state.values.value2
  ],
  (value1, value2) => value1 + value2
)

It can be useful to access the props of a component from within a selector. When a selector is connected to a component with connect, the component props are passed as the second argument to the selector:

const abSelector = (state, props) => state.a * props.b

// props only (ignoring state argument)
const cSelector =  (_, props) => props.c

// state only (props argument omitted as not required)
const dSelector = state => state.d

const totalSelector = createSelector(
  abSelector,
  cSelector,
  dSelector,
  (ab, c, d) => ({
    total: ab + c + d
  })
)

Structured Selector:

createSelector may also take an object whose properties are input-selectors and returns a structured selector. This "structured selector" returns an object with the same keys as the inputSelectors argument, but with the selectors replaced with their values.

const mySelectorA = state => state.a
const mySelectorB = state => state.b

const structuredSelector = createSelector({
  x: mySelectorA,
  y: mySelectorB
})

const result = structuredSelector({ a: 1, b: 2 }) // will produce { x: 1, y: 2 }

Structured selectors can be nested:

const nestedSelector = createSelector({
  subA: createSelector({
    selectorA,
    selectorB
  }),
  subB: createSelector({
    selectorC,
    selectorD
  })
})

createSimpleSelector(...inputSelectors | [inputSelectors], resultFunc)

createSimpleSelector(structuredSelector)

Same API as createSelector, however it does not perform any memoization on the selector.

memoizedSelectorCreator(equalityCheck)

memoizedSelectorCreator can be used to make a customized version of createSelector.

The equalityCheck determines how equality is used when checking against previous values.

import shallowEquals from 'shallow-equals'

function shallowish(maxDepth = 1) {
  return function eq(a, b, currentDepth = 0) {
    if (a === b) return true
    if (currentDepth >= maxDepth) return false
    if ((Array.isArray(a) && Array.isArray(b)) || (isPlainObject(a) && isPlainObject(b))) {
      return shallowEquals(a, b, (a, b) => eq(a, b, currentDepth + 1));
    }
    return false
  }
}

const customSelector = memoizedSelectorCreator(shallowish(2))

customSelector(selectorA, selectorB, (a, b) => a.concat(b))

License

MIT