tool/forecast/injector.js

import { requiredKeys } from '../../keys/required';

/**
 * Creates a basic injector
 * @class
 */
class Injector {
    /**
     * @param {struct} skv_config Struct passed in when initializing the injector
     */
    constructor(skv_config = {}) {
        this.skv_config = skv_config;
        this.arr_tax_years = [];
    }

    /**
     * Check the keys passed in to the injector are valid
     * @param {array} arr_valid_keys the allowable keys
     * @returns {boolean} valid or not?
     */
    validate(arr_valid_keys) {
        const arr_config_keys = Object.keys(this.skv_config);
        const cnt_keys = arr_config_keys.length;
        let is_valid_config = true;

        for (let i = 0; i < cnt_keys; i += 1) {
            const key = arr_config_keys[i];
            const index = arr_valid_keys.find((valid_key) => key === valid_key);

            if (!index) {
                is_valid_config = false;
                break;
            }
        }

        return is_valid_config;
    }

    /**
     * If a key isn't present in the struct then throw error
     * @param {struct} skv_to_check
     * @param {array} arr_required_keys Array of strings to check
     * @returns {struct} In json api format any missing keys
     */
    static requiredKeysCheck(skv_to_check, arr_required_keys) {
        return requiredKeys(skv_to_check, arr_required_keys);
    }

    /**
     * all injectors return manipulated array of tax years
     * @returns {array} Tax years
     */
    get taxYears() {
        return this.arr_tax_years;
    }
}

export {
    Injector,
};