ArrayStore.add()method
Add one or more items to this array.
add(...items: T[]): void
new ArrayStore<T>(value: PossibleArray<T> = [])
| Param | Type | |
|---|---|---|
value | PossibleArray<T> | The initial array value (defaults to an empty array). Defaults to [] |
| Return | |
|---|---|
ArrayStore<T> | Store an immutable array of items, with helpers to read and mutate its contents. |
| Property | Type | |
|---|---|---|
.optionalFirst | T | Get the first item in this store, or undefined if it has no items. readonly |
.optionalLast | T | Get the last item in this store, or undefined if it has no items. readonly |
.first | T | |
.last | T | |
.exists | boolean | Whether this store has at least one item. required readonly |
.count | number | Get the number of items in the current value of this store. required readonly |
Store an immutable array of items, with helpers to read and mutate its contents.
PossibleArray<T> as input and normalises it to an ImmutableArray<T>.add(), delete(), toggle()) replace the stored array with an immutable updated copy.for (const item of store).A Store for an array value. ArrayStore<T> defaults to an empty array, accepts any iterable as input, and adds immutable array helpers — every mutation produces a new array so consumers see a genuine change.
import { ArrayStore } from "shelving/store";
const tags = new ArrayStore<string>(["a", "b"]);
tags.add("c"); // ["a", "b", "c"]
tags.delete("a"); // ["b", "c"]
tags.toggle("b"); // ["c"] — removes if present, adds if not
console.log(tags.first); // "c" (throws if empty)
console.log(tags.count); // 1
console.log(tags.exists); // true
for (const tag of tags) console.log(tag); // ArrayStore is iterableUse .optionalFirst / .optionalLast instead of .first / .last to get undefined rather than a thrown RequiredError when the array is empty.
const store = new ArrayStore([1, 2, 3]); store.add(4); // [1, 2, 3, 4] store.first; // 1
Add one or more items to this array.
add(...items: T[]): void
Remove one or more items from this array.
delete(...items: T[]): void
Toggle one or more items in this array (add if absent, remove if present).
toggle(...items: T[]): void