shelving
Version:
Toolkit for using data in JavaScript.
39 lines (38 loc) • 1.34 kB
JavaScript
import { getDictionaryItems, omitDictionaryItems } from "../util/dictionary.js";
import { omitProps, 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. */
deleteItems(...keys) {
this.value = omitDictionaryItems(this.value, ...keys);
}
/** Get an item in this dictionary. */
get(name) {
return this.value[name];
}
/** Set an item in this dictionary. */
set(name, value) {
this.value = withProp(this.value, name, value);
}
/** Delete an item (or several items) in this dictionary. */
delete(name, ...names) {
this.value = omitProps(this.value, name, ...names);
}
/** Iterate over the entries of the object. */
[Symbol.iterator]() {
return getDictionaryItems(this.value)[Symbol.iterator]();
}
}