Pickable
Updated on August 22, 2024Source codeTests
Pickable
is a class that enriches an array, allowing it to:
- Store the index-based location of items that have been picked
- Retrieve the items that have been picked
- Store a status (
ready
,picked
, oromitted
) - Replace existing picks using different strategies, like FIFO or LIFO
- Omit specific picks, or all picks
Construct a Pickable
instance
The Pickable
constructor accepts two parameters:
array
options
Pickable
instance. See the Pickable constructor options section for more guidance.Pickable
constructor options
initialPicks
[]
The Pickable
instance's initial index-based picks.
initialPicks
should be a number or an array of numbers.
State and methods
array
A shallow copy (Array) of the array passed to the Pickable
constructor.
If you assign a value directly to array
, a setter will pass the new value to setArray
.
picks
An array of index-based locations (Number) stored by the Pickable
instance. Defaults to the initialPicks
option passed to the constructor.
If you assign a value directly to picks
, a setter will pass the new value to setPicks
.
picks
are stored in the order that they were picked, not in order of lowest-to-highest index.
first
picks
.last
picks
oldest
picks
newest
picks
multiple
picks
contains more than one indexitems
picks
indices in array
status
Pickable
instance. status
is ready
after the instance is constructed, and changes to picked
a successful pick, or omitted
after a successful omission.setArray(array)
Pickable
instance's array
array
(Array)Pickable
instancesetPicks(picks)
pick
method, but it doesn't support the optional options
argument.picks
(Number or Array)Pickable
instancepick(indexOrIndices[, options])
Picks a specific item or more than one item, identified by their index-based positions in the array.
See the How Pickable
picks section for more information on how options
affect picking.
options
argument. Pickable
instanceomit([indexOrIndices])
Omits (removes from picks
) a specific item or more than one item, identified by their index-based positions in the array.
When called with no arguments, omit
omits every pick from picks
, leaving only an empty array.
Pickable
instance How Pickable
picks
In general, whenever the setPicks
or pick
methods are called, the Pickable
instance computes the new picks and stores their index-based locations in its picks
property.
Picks are stored in the order that they were picked, not in order of lowest-to-highest index. Duplicate picks are ignored.
import { Pickable } from '@baleada/logic'
const pickable = new Pickable(myArray)
pickable.pick(4)
pickable.pick(2)
pickable.pick(420)
pickable.pick(42)
pickable.picks // -> [4, 2, 420, 42]
pickable.first // -> 2
pickable.last // -> 420
pickable.oldest // -> 4
pickable.newest // -> 42
The only other thing you need to know about how your Pickable
instance picks is what options are available for the pick
method, and how those options affect the way Pickable
computes the final picks.
pick
accepts an optional options
object as its second argument. Here's a breakdown of the options
object:
replace
none
When replace
is set to none
, Pickable
will append the new index or indices to its existing picks.
When replace
is set to all
, Pickable
will clear out all the existing picks, and replace them with the new index or indices passed to the pick
method.
When replace
is set to fifo
, Pickable
will add the new picks, then remove the oldest picks until the picks
array reaches its previous length.
When replace
is set to lifo
, Pickable
removes the most recent picks to make room for the new picks, so that the array maintains its previous length.
allowsDuplicates
false
allowsDuplicates
is set to true
, Pickable
will allow duplicate picks to be stored in picks
.Using with TypeScript
The Pickable
constructor accepts one generic type that you can use to enforce a type for the items in array
. By default, TypeScript will infer the item type from the initial array you pass to the constructor, but you can specify the type manually if needed.
const withInferredTypes = new Pickable([1, 2, 3])
withInferredTypes.array = ['a', 'b', 'c'] // Type error
const withManualTypes = new Pickable<string | number>([1, 2, 3])
withManualTypes.array = ['a', 'b', 'c'] // No type error
API design compliance
options
object.array
setArray
set<Property>
methodspicks
, setPicks
status
, first
, last
, oldest
, newest
, items
, multiple
pick
, omit
stop
methodpick(indexOrIndices[, options])
able