UNPKG

@barchart/common-js

Version:
159 lines (153 loc) 4.59 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var CompoundMap_exports = {}; __export(CompoundMap_exports, { default: () => CompoundMap }); module.exports = __toCommonJS(CompoundMap_exports); var assert = __toESM(require("./../../lang/assert.js")); var is = __toESM(require("./../../lang/is.js")); class CompoundMap { #depth; #map; /** * @param {number} depth - The number of keys. */ constructor(depth) { assert.argumentIsRequired(depth, "depth", Number); this.#depth = depth; this.#map = {}; } /** * Returns true if the map has a value (or a grouping of values) at the * given key. * * @public * @param {...string} keys * @returns {boolean} */ has(...keys) { validateKeys(keys, this.#depth, false); let target = this.#map; return keys.every((k) => { const returnVal = Object.prototype.hasOwnProperty.call(target, k); if (returnVal) { target = target[k]; } return returnVal; }); } /** * Puts a value into the map, overwriting any preexisting value. * * @public * @param {*} value * @param {...string} keys */ put(value, ...keys) { validateKeys(keys, this.#depth, true); let target = this.#map; let final = keys.length - 1; keys.forEach((k, i) => { if (i === final) { target[k] = value; } else { if (!Object.prototype.hasOwnProperty.call(target, k)) { target[k] = {}; } target = target[k]; } }); } /** * Gets a value from the map, returning null if the value does not exist. * * @public * @param {...string} keys * @returns {*} */ get(...keys) { validateKeys(keys, this.#depth, true); return keys.reduce((target, k) => { let next; if (is.object(target) && Object.prototype.hasOwnProperty.call(target, k)) { next = target[k]; } else { next = null; } return next; }, this.#map); } /** * Deletes a value (or a group of values) from the tree. * * @public * @param {...string} keys * @returns {boolean} */ remove(...keys) { validateKeys(keys, this.#depth, false); let returnVal = this.has(...keys); if (returnVal) { keys.reduce((target, k, i) => { let next; if (keys.length === i + 1) { delete target[k]; } else { next = target[k]; } return next; }, this.#map); } return returnVal; } /** * Returns a string representation. * * @public * @returns {string} */ toString() { return "[CompoundMap]"; } } function validateKeys(keys, depth, exact) { assert.argumentIsValid(keys, "keys", (k) => exact && k.length === depth || !exact && !(k.length > depth), "incorrect number of keys"); } { const cjsExports = module.exports; const cjsDefaultExport = cjsExports && cjsExports.__esModule ? cjsExports.default : cjsExports; if (cjsDefaultExport && (typeof cjsDefaultExport === 'function' || typeof cjsDefaultExport === 'object')) { Object.keys(cjsExports).forEach((key) => { if (key !== 'default' && key !== '__esModule') { cjsDefaultExport[key] = cjsExports[key]; } }); } module.exports = cjsDefaultExport; }