Code coverage report for nodash/lib/register.js

Statements: 100% (42 / 42)      Branches: 86.36% (19 / 22)      Functions: 100% (10 / 10)      Lines: 100% (41 / 41)      Ignored: none     

All files » nodash/lib/ » register.js
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      1   5   5   21   1 145 1105       5                 9                         1 75 75 75 305   75     1 1385   1385 265   220 75   145   220   45 45   265   1120 1120 1120 1120   1120 1120 1255 1120 1120     1120       1120 1075   1120 1395       5    
/* vim: set et sw=2 ts=2: */
'use strict';
 
module.exports = function (Nodash, options) {
 
  var curried = require('./curried');
 
  Nodash.__metadata = {};
 
  function id(x) { return x; }
 
  function registerLib(object) {
    Object.keys(object).forEach(function (key) {
      register(key, object[key]);
    });
  }
 
  var specialInjections = {
    Set:
      options.Set || (typeof Set !== 'undefined' && Set || require('./Set')),
 
    Math:
      options.Math || Math,
 
    error:
      function (Err, message) {
        throw new Err(message);
      },
 
    freeze:
      Object.freeze || id,
 
    create:
      Object.create || id,
 
    id:
      id
  };
 
  function registerInjected(array) {
    var func = array.pop();
    var args = [];
    array.forEach(function (arg) {
      args.push(arg in specialInjections ? specialInjections[arg] : Nodash[arg]);
    });
    register(func.apply(Nodash, args));
  }
 
  function register() {
    var args = [].slice.call(arguments);
    
    if (arguments.length === 1) {
      switch (typeof args[0]) {
      case 'object':
        if (Array.isArray(args[0])) {
          registerInjected(args[0].slice());
        } else {
          registerLib(args[0]);
        }
        break;
      case 'function':
        register(args[0].call(Nodash));
        break;
      }
      return;
    }
    var func = args.pop();
    var aliases = [];
    args.forEach(function (arg) {
      [].push.apply(aliases, arg.split(/ +/));
    });
    var name = null;
    for (var i = 0; i < aliases.length; i += 1) {
      if (/^[a-z0-9]+$/i.test(aliases[i])) {
        name = aliases[i];
        break;
      }
    }
    Nodash.__metadata[name] = {
      aliases: aliases,
      definition: func 
    };
    if (!/^[A-Z]/.test(name)) {
      func = curried(func);
    }
    aliases.forEach(function (alias) {
      Nodash[alias] = func;
    });
  }
 
  return register;
};