All files / core register.js

93.33% Statements 14/15
75% Branches 6/8
100% Functions 5/5
93.33% Lines 14/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                                          349x 349x 597x 117x   597x     349x 349x 349x         4x 344x 344x 344x 344x     4x    
/* eslint-disable no-param-reassign */
 
export default function configure(sigma) {
  /**
   * Takes a package name as parameter and checks at each lebel if it exists,
   * and if it does not, creates it.
   *
   * Example:
   * ********
   *  > pkg('a.b.c');
   *  > a.b.c;
   *  > // Object {};
   *  >
   *  > pkg('a.b.d');
   *  > a.b;
   *  > // Object { c: {}, d: {} };
   *
   * @param  {string} pkgName The name of the package to create/find.
   * @return {object}         The related package.
   */
  function getPackageObject(pkgName) {
    const getPackage = (levels, root) => {
      return levels.reduce((context, objName) => {
        if (!context[objName]) {
          context[objName] = {};
        }
        return context[objName];
      }, root);
    };
    const levels = (pkgName || "").split(".");
    Eif (levels[0] === "sigma") {
      return getPackage(levels.slice(1), sigma);
    }
    return getPackage(levels, sigma.packages);
  }
 
  sigma.register = (packageName, item) => {
    const parentPath = packageName.substring(0, packageName.lastIndexOf("."));
    const itemName = packageName.substring(packageName.lastIndexOf(".") + 1);
    const pkg = getPackageObject(parentPath);
    pkg[itemName] = pkg[itemName] || item;
  };
 
  sigma.register("sigma.utils.pkg", getPackageObject);
}