UNPKG

@ayanaware/bento

Version:

Modular runtime framework designed to solve complex tasks

119 lines 4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VariableManager = void 0; const errors_1 = require("@ayanaware/errors"); const ValidatorRegistrationError_1 = require("../errors/ValidatorRegistrationError"); class VariableManager { constructor() { this.variables = new Map(); this.validators = new Map(); } /** * Check if a given variable exists * @param name name of variable * * @returns boolean */ hasVariable(name) { if (typeof name !== 'string') throw new errors_1.IllegalArgumentError('Variable name must be a string'); if (this.variables.has(name)) return true; return false; } /** * Fetch a value for a given variable name * @param name name of variable * @param def default value * * @returns variable value */ getVariable(name, def) { if (typeof name !== 'string') throw new errors_1.IllegalArgumentError('Variable name must be a string'); const value = this.variables.get(name); if (value === undefined && def !== undefined) return def; return value; } /** * Get Key/Value Object of all variables * * @returns Object of Key/Value pairs */ getVariables() { return Array.from(this.variables.entries()).reduce((a, [k, v]) => { a[k] = v; return a; }, {}); } /** * Update a given variables value * @param name name of variable * @param value new value */ setVariable(name, value) { if (typeof name !== 'string') throw new errors_1.IllegalArgumentError('Variable name must be a string'); if (value === undefined) return; this.variables.set(name, value); return value; } /** * Fully removes all traces of a variable from bento * @param name name of variable */ deleteVariable(name) { if (typeof name !== 'string') throw new errors_1.IllegalArgumentError('Variable name must be a string'); if (this.variables.has(name)) this.variables.delete(name); } /** * Add a new validator into Bento * @param name validator name * @param validator validator function */ addValidator(name, validator) { if (typeof name !== 'string') throw new errors_1.IllegalArgumentError('Validator name must be a string'); if (typeof validator !== 'function') throw new errors_1.IllegalArgumentError('Validator must be a function'); this.validators.set(name, validator); } /** * Remove validator from Bento * @param name validator name */ removeValidator(name) { if (typeof name !== 'string') throw new errors_1.IllegalArgumentError('Validator name must be a string'); if (!this.validators.has(name)) throw new errors_1.IllegalStateError(`Validator "${name}" does not exist`); this.validators.delete(name); } /** * Run a validator * @param name validator name * @param value validator value * @param args array of args to be passed * * @returns validator result */ runValidator(name, value, ...args) { if (typeof name !== 'string') throw new errors_1.IllegalArgumentError('Validator name must be a string'); if (!this.validators.has(name)) throw new errors_1.IllegalStateError(`Validator "${name}" does not exist`); const validator = this.validators.get(name); try { return validator.call(undefined, value, args); } catch (e) { throw new ValidatorRegistrationError_1.ValidatorRegistrationError(name, `Validator "${name}" failed to execute`).setCause(e); } } } exports.VariableManager = VariableManager; //# sourceMappingURL=VariableManager.js.map