PageRenderTime 31ms CodeModel.GetById 24ms app.highlight 5ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/express/node_modules/debug/debug.component.js

https://bitbucket.org/gagginaspinnata/todo-app-with-angularjs
JavaScript | 120 lines | 60 code | 21 blank | 39 comment | 16 complexity | b72793368887570e5a0334c9e5bcb736 MD5 | raw file
Possible License(s): Apache-2.0, MIT
  1;(function(){
  2
  3/**
  4 * Create a debugger with the given `name`.
  5 *
  6 * @param {String} name
  7 * @return {Type}
  8 * @api public
  9 */
 10
 11function debug(name) {
 12  if (!debug.enabled(name)) return function(){};
 13
 14  return function(fmt){
 15    var curr = new Date;
 16    var ms = curr - (debug[name] || curr);
 17    debug[name] = curr;
 18
 19    fmt = name
 20      + ' '
 21      + fmt
 22      + ' +' + debug.humanize(ms);
 23
 24    // This hackery is required for IE8
 25    // where `console.log` doesn't have 'apply'
 26    window.console
 27      && console.log
 28      && Function.prototype.apply.call(console.log, console, arguments);
 29  }
 30}
 31
 32/**
 33 * The currently active debug mode names.
 34 */
 35
 36debug.names = [];
 37debug.skips = [];
 38
 39/**
 40 * Enables a debug mode by name. This can include modes
 41 * separated by a colon and wildcards.
 42 *
 43 * @param {String} name
 44 * @api public
 45 */
 46
 47debug.enable = function(name) {
 48  localStorage.debug = name;
 49
 50  var split = (name || '').split(/[\s,]+/)
 51    , len = split.length;
 52
 53  for (var i = 0; i < len; i++) {
 54    name = split[i].replace('*', '.*?');
 55    if (name[0] === '-') {
 56      debug.skips.push(new RegExp('^' + name.substr(1) + '$'));
 57    }
 58    else {
 59      debug.names.push(new RegExp('^' + name + '$'));
 60    }
 61  }
 62};
 63
 64/**
 65 * Disable debug output.
 66 *
 67 * @api public
 68 */
 69
 70debug.disable = function(){
 71  debug.enable('');
 72};
 73
 74/**
 75 * Humanize the given `ms`.
 76 *
 77 * @param {Number} m
 78 * @return {String}
 79 * @api private
 80 */
 81
 82debug.humanize = function(ms) {
 83  var sec = 1000
 84    , min = 60 * 1000
 85    , hour = 60 * min;
 86
 87  if (ms >= hour) return (ms / hour).toFixed(1) + 'h';
 88  if (ms >= min) return (ms / min).toFixed(1) + 'm';
 89  if (ms >= sec) return (ms / sec | 0) + 's';
 90  return ms + 'ms';
 91};
 92
 93/**
 94 * Returns true if the given mode name is enabled, false otherwise.
 95 *
 96 * @param {String} name
 97 * @return {Boolean}
 98 * @api public
 99 */
100
101debug.enabled = function(name) {
102  for (var i = 0, len = debug.skips.length; i < len; i++) {
103    if (debug.skips[i].test(name)) {
104      return false;
105    }
106  }
107  for (var i = 0, len = debug.names.length; i < len; i++) {
108    if (debug.names[i].test(name)) {
109      return true;
110    }
111  }
112  return false;
113};
114
115// persist
116
117if (window.localStorage) debug.enable(localStorage.debug);
118  module.exports = debug;
119
120})();