• Jump To … +
    Either.js List.js Maybe.js Set.js Stream.js Thunk.js Tuple.js boolean.js char.js base.js extra.js fold.js map.js zipWith.js collection.js control.js curried.js eq.js floating.js fractional.js function.js integral.js match.js num.js numeric.js object.js ord.js realfrac.js register.js string.js type.js nodash.js
  • register.js

  • ¶
    /* 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;
    };