UNPKG

@interaktiv/mibuilder-core

Version:

Core libraries to interact with MiBuilder projects.

226 lines (185 loc) 5.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseConfigStore = void 0; var _dxl = require("@interaktiv/dxl"); var _types = require("@interaktiv/types"); /** * An abstract class that implements all the config management functions but * none of the storage functions. * * **Note:** To see the interface, look in typescripts autocomplete help or the npm package's ConfigStore.d.ts file. */ class BaseConfigStore extends _dxl.AsyncCreatable { /** * Constructor. * * @ignore * @param {Object} options The options for the class instance. * @param {Object} options.contents The contents of the store */ constructor(options) { super(options); this.options = options; this.setContents(this.options.contents || {}); } /** * Returns an array of {@link ConfigEntry} for each element in the config. * * @return {[String,Object][]} The content entries */ entries() { return (0, _types.definiteEntriesOf)(this.contents); } /** * Returns the value associated to the key, or undefined if there is none. * * @param {String} key The key * @return {*} The value for the key */ get(key) { return (0, _types.getAnyJson)(this.contents, key); } /** * Returns the list of keys that contain a value. * * @param {*} value The value to filter keys on * @return {String[]} The key(s) for the given value */ getKeysByValue(value) { const matchedEntries = this.entries().filter(entry => entry[1] === value); // Only return the keys return matchedEntries.map(entry => entry[0]); } /** * Returns a boolean asserting whether a value has been associated to the key * in the config object or not. * * @param {String} key The key * @return {Boolean} Returns true if the key has a value */ has(key) { return !!(0, _types.get)(this.contents, key); } /** * Returns an array that contains the keys for each element in the config * object. * * @return {String[]} The keys of the config */ keys() { return Object.keys(this.contents); } /** * Sets the value for the key in the config object. * * @param {String} key The Key * @param {*} value The value * @return {Object} The config object */ set(key, value) { this.setMethod(this.contents, key, value); return this.contents; } /** * Returns `true` if an element in the config object existed and has been * removed, or `false` if the element does not * exist. {@link BaseConfigStore.has} will return false afterwards. * * @param {String} key The key * @return {Boolean} Returns true if the element was removed otherwise false */ unset(key) { return delete this.contents[key]; } /** * Returns `true` if all elements in the config object existed and have been * removed, or `false` if all the elements * do not exist (some may have been removed). * {@link BaseConfigStore.has(key)} will return false afterwards. * * @param {String[]} keys The keys * @return {Boolean} Returns true if all elements exists and are removed */ unsetAll(keys) { return keys.reduce((val, key) => val && this.unset(key), true); } /** * Removes all key/value pairs from the config object. */ clear() { this.contents = {}; } /** * Returns an array that contains the values for each element in the config * object. * * @return {Array} The values of the config */ values() { return (0, _types.definiteValuesOf)(this.contents); } /** * Returns the entire config contents. * * @return {Object} The config contents */ getContents() { if ((0, _types.isNil)(this.contents)) this.setContents(); return this.contents; } /** * Sets the entire config contents. * * @param {Object} contents The contents */ setContents(contents) { this.contents = contents || {}; } /** * Invokes `actionFn` once for each key-value pair present in the config * object. * @param {function} actionFn The function `(key: string, value: ConfigValue) => void` to be called for each element. */ forEach(actionFn) { const entries = this.entries(); if (entries == null) return; entries.forEach(entry => actionFn(...entry)); } /** * Asynchronously invokes `actionFn` once for each key-value pair present in * the config object. * * @param {function} actionFn The function `(key: string, value: ConfigValue) => Promise<void>` to be called for each element. * @return {Promise<void>} A promise that resolves if all `actionFn` invokes are done */ awaitEach(actionFn) { const entries = this.entries(); if (entries == null || entries.length === 0) return Promise.resolve(); return Promise.all(entries.map(entry => actionFn(...entry))); } /** * Convert the config object to a JSON object. Returns the config contents. * Same as calling {@link ConfigStore.getContents} * * @return {Object} The content */ toObject() { return this.contents; } /** * Convert an object to a {@link ConfigContents} and set it as the config * contents. * * @param {Object} obj The object */ setContentsFromObject(obj) { this.contents = {}; Object.entries(obj).forEach(entry => this.setMethod(this.contents, ...entry)); } // Allows extended classes the ability to override the set method. i.e. maybe // they don't want nested object set from lib. setMethod(contents, key, value) { (0, _dxl.set)(contents, key, value); } } exports.BaseConfigStore = BaseConfigStore;