UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

49 lines (48 loc) 1.54 kB
import { getFirst, getLast, omitArrayItems, requireFirst, requireLast, toggleArrayItems, withArrayItems } from "../util/array.js"; import { Store } from "./Store.js"; /** Store an array. */ export class ArrayStore extends Store { constructor(value = [], time) { super(value, time); } /** Get the first item in this store or `null` if this query has no items. */ get optionalFirst() { return getFirst(this.value); } /** Get the last item in this store or `null` if this query has no items. */ get optionalLast() { return getLast(this.value); } /** Get the first item in this store. */ get first() { return requireFirst(this.value); } /** Get the last item in this store. */ get last() { return requireLast(this.value); } /** Does the document have at least one result. */ get exists() { return !!this.value.length; } /** Get the length of the current value of this store. */ get count() { return this.value.length; } /** Add items to this array. */ add(...items) { this.value = withArrayItems(this.value, ...items); } /** Remove items from this array. */ delete(...items) { this.value = omitArrayItems(this.value, ...items); } /** Toggle items in this array. */ toggle(...items) { this.value = toggleArrayItems(this.value, ...items); } /** Iterate over the items. */ [Symbol.iterator]() { return this.value.values(); } }