@devs-toolbox/store
v1.0.0
Published
A simple store
Downloads
1
Readme
Dev's Toolbox // Store
A simple store with support for publishing updates and managing data cloning to prevent mutations.
Store API
new Store(initialValue [, options])
Create a new Store.
initialValue
- The starting value of the storeoptions
- optional Configuration for the store
Options
| Option | Default Value | Description |
| ---------- | --------------- | ----------------------------------------------------------------------------------------- |
| onChange
| (value) => {}
| A function which takes the current value. It is invoked whenever the store value changes. |
.set(newValue)
Set the value of the store.
newValue
- The value to set
.modify(modifierFn)
Modify the value of the story by providing a function which takes the current store value and returns a modified value.
modifierFn
- A function to modify the store value
.value
(Property) a deep copy of the current value of the store.
CollectionStore API
new CollectionStore(initialValue[, options])
Create a new CollectionStore.
initialValue
- The starting value of the storeoptions
- optional Configuration for the store
Options
| Option | Default Value | Description |
| ---------- | --------------- | ----------------------------------------------------------------------------------------- |
| onChange
| (value) => {}
| A function which takes the current value. It is invoked whenever the store value changes. |
.append(value)
Append an element to the end of the collection
value
- The value to append
.shift()
Pop the first element in the stored collection from the collection and return it.
.find(predicateFn)
Returns an array of all items in the collection which are truthy for the given predicate
predicateFn
- A function to use to find items in the collection
.findOne(predicateFn)
Returns the first found item in the collection which is truthy for the predicate
predicateFn
- A function to use to find an item in the collection
.removeWhere(predicateFn)
Removes elements from the collection where the predicate is truthy.
predicateFn
- A function to use to identify items to remove
.modifyWhere(predicateFn, modifier)
Modify items in the collection where a predicate is truthy.
predicateFn
- A function to use to identify items to modifymodifier
- A function to modify individual items of the collection
.includes(value)
Check if a value is in the stored collection. Returns true
or false
depending on the presence or absence of the value in the collection.
value
- The value to check for.
.map(mapFn)
Map over each element in the collection, returning an array of the results.
mapFn
- A function to map over each value in the collection
.size
(Property) The size of the collection as a number.