All files / if-run/lib parameterize.ts

100% Statements 26/26
100% Branches 3/3
100% Functions 4/4
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 807x 7x 7x   7x                   7x         7x 7x         7x 1143x 1143x   1143x 1141x     2x   2x               7x       3x   3x 2x 2x 1x   1x     1x       1x               3x     7x           7x  
import {debugLogger} from '../../common/util/debug-logger';
import {logger} from '../../common/util/logger';
import {memoizedLog} from '../util/log-memoize';
 
import {STRINGS, PARAMETERS} from '../config';
 
import {Parameters} from '../types/parameters';
import {ManifestParameter} from '../../common/types/manifest';
 
const {
  REJECTING_OVERRIDE,
  UNKNOWN_PARAM,
  SYNCING_PARAMETERS,
  CHECKING_AGGREGATION_METHOD,
} = STRINGS;
 
/**
 * Parameters manager. Provides get aggregation method and combine functionality.
 */
const Parameterize = () => {
  let parametersStorage = PARAMETERS;
 
  /**
   * Returns aggregation method for given `unitName`. If doesn't exist then returns value `sum`.
   */
  const getAggregationMethod = (unitName: string) => {
    debugLogger.setExecutingPluginName();
    memoizedLog(console.debug, CHECKING_AGGREGATION_METHOD(unitName));
 
    if (`${unitName}` in parametersStorage) {
      return parametersStorage[unitName as keyof typeof PARAMETERS].aggregation;
    }
 
    memoizedLog(logger.warn, UNKNOWN_PARAM(unitName));
 
    return 'sum';
  };
 
  /**
   * Checks if additional parameters are provided in context.
   * If so, then checks if they are coincident with default ones and exits with warning message.
   * Otherwise appends context based parameters to defaults.
   */
  const combine = (
    contextParameters: ManifestParameter[] | null | undefined,
    parameters: Parameters
  ) => {
    console.debug(SYNCING_PARAMETERS);
 
    if (contextParameters) {
      contextParameters.forEach(param => {
        if (`${param.name}` in parameters) {
          logger.warn(REJECTING_OVERRIDE(param));
 
          return;
        }
 
        const {description, unit, aggregation, name} = param;
 
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        parameters[name] = {
          description,
          unit,
          aggregation,
        };
      });
    }
 
    parametersStorage = parameters;
  };
 
  return {
    combine,
    getAggregationMethod,
  };
};
 
export const parameterize = Parameterize();