YourChoice resembles the selection behavior of popular file managers. Pick items from a list using convenient functions like range selection. YourChoice covers the computation of the actual selection, no UI involved.
npm install yourchoice --save
Since yourchoice is a frontend module, you will need a module bundler like webpack or browserify.
Caution: Yourchoice uses es6/es7 features which are not supported in IE11 You can polyfill it with core-js
require('core-js/modules/es6.symbol')
require('core-js/modules/es6.array.from')
require('core-js/modules/es6.array.iterator')
require('core-js/modules/es6.object.assign')
require('core-js/modules/es7.array.includes')
If you are the retro kind of person, you can also download the JavaScript file.
<script type="text/javascript" src="https://github.com/actano/yourchoice/raw/master/yourchoice.js"></script>
Yourchoice provides a functional interface. It is well-suited to be used in conjunction with lodash/flow or Redux. Of course, this interface can also be used in a non-functional environment. The imperative interface of yourchoice is considered deprecated.
All functions of yourchoice take an object of type State
as their last argument. Some of the functions also return an updated State
object.
The state
object is an opaque object representing the current state of yourchoice. The properties of this object are private and one should not depended upon them.
In order to read properties form the state yourchoice provides accessor methods such as getSelection()
.
All functions exported by yourchoice are curried. That means that these functions can be partially applied with a subset of their arguments. This is particularly useful in conjunction with libraries like lodash/flow
.
const selectableItems = ['A', 'B', 'C', 'D', 'E']
const state = init()
newState = flow(
setItems(selectableItems),
replace('B'),
rangeTo('D')
)(state)
console.log(getSelection(newState)) // ['B', 'C', 'D']
Returns an empty state with no selection and no items that can be selected.
Changes the current set of items that can be selected/deselected.
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 function is usually called initially before any selection is performed. This function should be called in order to update the yourchoice 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.
Replaces the current selection with the given item
. Also defines this item as the starting point for a subsequent rangeTo()
selection. This is equivalent to a simple click by the user in a file manager.
Adds or removes the given item
to/from the selection. Other currently selected items are not affected. Also defines this item as the starting point for a subsequent rangeTo()
selection if it is added to the selection. This is equivalent to an alt+click (cmd+click) by the user in a file manager.
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.
Replaces the current selection with the given items
.
Removes the given items
from the current selection.
Removes all items from the current selection.
Returns an array containing all selectable items.
Returns an array containing the currently selected items.
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.
Returns an array containing those items that have been removed from the selection by the directly preceding operation. E.g. calling this after a call to rangeTo()
will return all the items that have been removed from the selection by this operation.