ArrayStoreclass

new ArrayStore<T>(value: PossibleArray<T> = [])
ParamType
valuePossibleArray<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.
PropertyType
.optionalFirstT
Get the first item in this store, or undefined if it has no items. readonly
.optionalLastT
Get the last item in this store, or undefined if it has no items. readonly
.firstT
Get the first item in this store, or throw RequiredError if it has no items. required readonly
.lastT
Get the last item in this store, or throw RequiredError if it has no items. required readonly
.existsboolean
Whether this store has at least one item. required readonly
.countnumber
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.

  • Accepts any PossibleArray<T> as input and normalises it to an ImmutableArray<T>.
  • Mutations (add(), delete(), toggle()) replace the stored array with an immutable updated copy.
  • Iterable, so you can 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.

Usage

ts
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 iterable

Use .optionalFirst / .optionalLast instead of .first / .last to get undefined rather than a thrown RequiredError when the array is empty.

Examples

const store = new ArrayStore([1, 2, 3]);
store.add(4); // [1, 2, 3, 4]
store.first; // 1

Methods

Go

ArrayStore.add()method

Add one or more items to this array.

add(...items: T[]): void
Go

ArrayStore.delete()method

Remove one or more items from this array.

delete(...items: T[]): void
Go

ArrayStore.toggle()method

Toggle one or more items in this array (add if absent, remove if present).

toggle(...items: T[]): void