# Code coverage report for lib/util.js

## Statements: 70.21% (33 / 47) Branches: 58.82% (20 / 34) Functions: 63.64% (7 / 11) Lines: 78.05% (32 / 41) Ignored: none

All files » lib/ » util.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
    811
    1
    1
    1
    1
    1

    1

    1

    1

    4

    5
    5
    5
    54
    22
    22
    22
    22

    32

    1
    259
    168

    168
    168

    240

    3
    3
    2

    3

    2
    2

    1
     var _ = require('lazy.js');
    var extend = require('xtend');
    var traverse = require('traverse');
    var path = require('path');
    var Promise = require('bluebird');
    var mkdirp = Promise.promisify(require('mkdirp'));

    var package = require('../package');

    var boolStrings = {true: true, false: false};

    var util = _(require('util')).merge({
      clone: extend,
      extend: extend,

      toArray: function (obj) { return [].slice.call(obj); },

      mod: function (n, m) { return ((n % m) + m) % m; },

      typeOf: function (val) {
        return (val.__proto__.constructor.toString().match(/\w+\s+(\w+)/) || [])[1];
      },

      getterSetter: function (name, getter, setter) {
        getter = getter || _.identity;
        setter = setter || _.identity;
        return function () {
          if (arguments.length) {
            var newVal = setter.apply(this, arguments);
            this.data[name] = newVal;
            this.emit && this.emit(name, getter.call(this, newVal));
            return this;
          } else {
            return getter.call(this, this.data[name]);
          }
        };
      },

      parseOpts: function (opts) {
        return traverse(opts).map(function (opt) {
          if (opt && typeof opt === 'string') {
            Iif (opt in boolStrings) return boolStrings[opt];

            var number = Number(opt);
            if (number === number) return number; // if (!isNaN(number))
          }
          return opt;
        });
      },

      resolvePath: function (givenPath) {
        Iif (!givenPath) givenPath = '';
        if (givenPath[0] === '~') {
          givenPath = path.join(process.platform !== 'win32'
            ? process.env.HOME
            : process.env.USERPROFILE
          , givenPath.slice(1));
        }
        return path.resolve.apply(null, [].slice.call(arguments, 1).concat([givenPath]));
      },

      getUserDir: function () {
        var userDir = util.resolvePath('~/.' + package.name);
        return mkdirp(userDir).return(userDir);
      },

      getBinding: function (bindings, key) {
        for (var name in bindings) {
          if (bindings.hasOwnProperty(name)) {
            var keyBindings = bindings[name];
            if (!keyBindings) continue;
            if (typeof keyBindings === 'string') keyBindings = [keyBindings];
            if (keyBindings.some(function (binding) { return binding === key.full || binding === key.sequence; }))
              return name;
          }
        }
      }
    }).toObject();

    module.exports = util;