All files / core/domain/classes Configurable.ts

100% Statements 18/18
91.67% Branches 11/12
100% Functions 6/6
100% Lines 15/15

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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                                  148x 45x       469x   176x                                                                         644x       4x       61x   2x     61x 1265x   61x                   45x 3x       58x 45x    
import { Keyed } from "../../interfaces";
 
type ConfigMap = Keyed<any>;
 
export interface Settings {
  (a1: string | ConfigMap, a2?: any): any;
  embedObjects(...eoArgs: ConfigMap[]): Settings;
}
/**
 * This utils aims to facilitate the manipulation of each instance setting.
 * Using a function instead of an object brings two main advantages: First,
 * it will be easier in the future to catch settings updates through a
 * function than an object. Second, giving it a full object will "merge" it
 * to the settings object properly, keeping us to have to always add a loop.
 *
 * @return {configurable} The "settings" function.
 */
export default function configurable(...args: any[]): Settings {
  const data: ConfigMap = Object.assign({}, ...args);
 
  function getData(key: string) {
    if (data[key] !== undefined) {
      return data[key];
    }
    return undefined;
  }
  /**
   * The method to use to set or get any property of this instance.
   *
   * @param  {string|object}    arg1 If it is a string and if a2 is undefined,
   *                               then it will return the corresponding
   *                               property. If it is a string and if a2 is
   *                               set, then it will set a2 as the property
   *                               corresponding to a1, and return this. If
   *                               it is an object, then each pair string +
   *                               object(or any other type) will be set as a
   *                               property.
   * @param  {*?}               arg2 The new property corresponding to a1 if a1
   *                               is a string.
   * @return {*|configurable}      Returns itself or the corresponding
   *                               property.
   *
   * Polymorphism:
   * *************
   * Here are some basic use examples:
   *
   *  > settings = configurable();
   *  > settings('mySetting', 42);
   *  > settings('mySetting'); // Logs: 42
   *  > settings('mySetting', 123);
   *  > settings('mySetting'); // Logs: 123
   *  > settings({mySetting: 456});
   *  > settings('mySetting'); // Logs: 456
   *
   * Also, it is possible to use the function as a fallback:
   *  > settings({mySetting: 'abc'}, 'mySetting');  // Logs: 'abc'
   *  > settings({hisSetting: 'abc'}, 'mySetting'); // Logs: 456
   */
  function settings(arg1: string | ConfigMap, arg2?: any) {
    // single argument, (string) form: access the data item
    if (arguments.length === 1 && typeof arg1 === "string") {
      return getData(arg1);
    }
    // two arguments: (object, string) form: augmented lookup
    if (typeof arg1 === "object" && typeof arg2 === "string") {
      return (arg1 || {})[arg2] !== undefined ? arg1[arg2] : getData(arg2);
    }
 
    // Extend current sestings
    const o = typeof arg1 === "object" && arg2 === undefined ? arg1 : {};
    if (typeof arg1 === "string") {
      o[arg1] = arg2;
    }
 
    Object.keys(o).forEach(key => {
      data[key] = o[key];
    });
    return undefined;
  }
 
  /**
   * This method returns a configurable function, with new objects
   *
   * @param  {object*}  Any number of objects to search in.
   * @return {function} Returns the function. Check its documentation to know
   *                    more about how it works.
   */
  settings.embedObjects = function embedObjects(...extraData: ConfigMap[]) {
    return configurable(data, ...extraData);
  };
 
  // Initialize
  args.forEach(a => settings(a));
  return settings;
}