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

yourchoice-redux

v1.1.1

Published

Redux wrapper for the yourchoice library

Downloads

37

Readme

yourchoice-redux

npm code style: actano Build Status codecov Dependency Status devDependency Status

Redux wrapper for yourchoice.

Installing

With npm

npm install yourchoice-redux --save

Since yourchoice-redux is a frontend module, you will need a module bundler like webpack or browserify.

Caution: Yourchoice Redux uses Symbol.iterator which is not yet supported by all environments. You can polyfill it with core-js

if (!window.Symbol) {
  require('core-js/es6/symbol')
}

Usage

Reducer

YourChoice Redux provides a reducer with a standard signature (state, action) => nextState.

import {createStore, combineReducers} from 'redux'

import {reducer as yourchoiceReducer} from 'yourchoice-redux'

// connect to store
const store = createStore(combineReducers({
    // ... other reducers
    yourchoice: yourchoiceReducer
}))

Bind to selection

bindToSelection returns an object that contains the yourchoice actions and selectors bound to a specific selection.

import {bindToSelection} from 'yourchoice-redux'

// bind yourchoice to specific selection name
// if the selectionName is omitted it defaults to 'selection'
const mySelection = bindToSelection('my-selection');
/*
returns {
    actions: {
        setItems,
        rangeTo,
        remove,
        ...
    },
    selectors: {
        getSelection,
        getChangedSelection,
        ...
    }
}
*/

Selectors

Selectors need to be given the substate the reducer acts on.

export {
    getItems: (state) => mySelection.selectors.getItems(state.yourchoice)
}

Examples

Simple use case

import {createStore, combineReducers} from 'redux'
import {bindToSelection, reducer as yourchoiceReducer} from 'yourchoice-redux'
const selection = bindToSelection()

// connect to store
const store = createStore(combineReducers({
    // ... other reducers
    yourchoice: yourchoiceReducer
}))

// connect to ReactComponent
const mapStateToProps = (state) => {
  return {
    myList: selection.selectors.getItems(state.yourchoice),
    mySelection: selection.selectors.getSelection(state.yourchoice)
  }
}

// dipatch actions
store.dispatch(selection.actions.setItems([1, 2, 5, 8]))
store.dispatch(selection.actions.replace(2))

Bind more than one selection

// add global reducer to store at mountpoint yourchoice (see above)

// get action creators and selectors bound to selection name 'users'
const userSelection = bindToSelection('users');

...
store.dispatch(userSelection.actions.setItems(userList));
store.dispatch(userSelection.actions.replace('user-id-1'));
...
// get selector
const getSelectedUsers = (state) => userSelection.selectors.getSelection(state.yourchoice)

// get action creators and selectors bound to selection name 'teams'
const teamSelection = bindToSelection('teams');
...

API

  • reducer(state, action)
  • bindToSelection(selectionName)
  • action creator: rangeTo(toItem)
  • action creator: remove(itemsToRemove)
  • action creator: removeAll()
  • action creator: replace(item)
  • action creator: setItems(selectableItems)
  • action creator: setSelection(selectedItems)
  • action creator: toggle(item)
  • selector: getChangedSelection(state)
  • selector: getChangedDeselection(state)
  • selector: getItems(state)
  • selector: getSelection(state)

type BoundAPI = { actions: ActionCreators; selectors: Selectors; };

type ActionCreators = { rangeTo: (item: Item) => Action; remove: (items: Item[]) => Action; removeAll: () => Action; replace: (item: Item) => Action; setItems: (items: Item[]) => Action; setSelection: (items: Item[]) => Action; toggle: (item: Item) => Action; };

type Selectors = { getChangedSelection: (state: State) => Item[]; getChangedDeselection: (state: State) => Item[]; getItems: (state: State) => Item[]; getSelection: (state: State) => Item[]; };

`bindToSelection` returns an object containing all the following action creators and selectors, bound to the given selection name.

<h3 id="_action_rangeTo">action creator: rangeTo</h3>
```javascript
rangeTo(item: Item): Action

Selects a range of items usally starting from the previously toggled or replaced item and ending at the given item. This is equivalent to a shift+click by the user in a file manager.

The selectableItems can be any javascript iterable. This enables yourchoice to operate on any data structure. Native data types such as Array or Map implement the iterable protocol.

This action is usually dispatched initially before any selection is performed. This action should be called in order to update the state when selectable items have been added or removed. For example, if some of the currently selected items are not present in the given selectableItems anymore, then these items will be automatically removed from the current selection.

Consider using a store middleware or saga to do this.

Returns an array containing the currently selected items.

<h3 id="_selector_getChangedSelection">selector: getChangedSelection</h3>
```javascript
getChangedSelection(state: State): Item[]

Returns an array containing those items that have been added to the selection by the directly preceding operation. E.g. calling this after a call to rangeTo() will return all the items that have been added to the selection by this operation.