shelving
Version:
Toolkit for using data in JavaScript.
35 lines (34 loc) • 1.17 kB
JavaScript
import { getDictionaryItems, omitDictionaryItems } from "../util/dictionary.js";
import { withProp } from "../util/object.js";
import { updateData } from "../util/update.js";
import { Store } from "./Store.js";
/** Store a dictionary object. */
export class DictionaryStore extends Store {
constructor(value = {}, time) {
super(value, time);
}
/** Get the length of the current value of this store. */
get count() {
return Object.keys(this.value).length;
}
/** Set a named entry in this object with a different value. */
update(updates) {
this.value = updateData(this.value, updates);
}
/** Remove a named entry from this object. */
delete(...keys) {
this.value = omitDictionaryItems(this.value, ...keys);
}
/** Get an item in this dictionary. */
getItem(name) {
return this.value[name];
}
/** Set an item in this dictionary. */
setItem(name, value) {
this.value = withProp(this.value, name, value);
}
/** Iterate over the entries of the object. */
[Symbol.iterator]() {
return getDictionaryItems(this.value)[Symbol.iterator]();
}
}