shelving
Version:
Toolkit for using data in JavaScript.
44 lines (43 loc) • 1.61 kB
JavaScript
import { EMPTY_DICTIONARY, getDictionaryItems, omitDictionaryItems, requireDictionary } from "../util/dictionary.js";
import { omitProps, withProp } from "../util/object.js";
import { updateData } from "../util/update.js";
import { BusyStore } from "./BusyStore.js";
/** Store a dictionary object. */
export class DictionaryStore extends BusyStore {
// Override to set default value to empty dictionary.
constructor(value = EMPTY_DICTIONARY) {
super(requireDictionary(value));
}
// Override to convert a possible dictionary to a dictionary on set.
_convert(possible) {
return requireDictionary(possible);
}
/** 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]();
}
}