Comparing version 0.8.1 to 1.0.0
202
debug.js
/** | ||
* This is the common logic for both the Node.js and web browser | ||
* implementations of `debug()`. | ||
* | ||
* Expose `debug()` as the module. | ||
*/ | ||
module.exports = debug; | ||
exports = module.exports = debug; | ||
exports.coerce = coerce; | ||
exports.disable = disable; | ||
exports.enable = enable; | ||
exports.enabled = enabled; | ||
exports.humanize = require('ms'); | ||
/** | ||
* Create a debugger with the given `name`. | ||
* The currently active debug mode names, and names to skip. | ||
*/ | ||
exports.names = []; | ||
exports.skips = []; | ||
/** | ||
* Map of special "%n" handling functions, for the debug "format" argument. | ||
* | ||
* @param {String} name | ||
* @return {Type} | ||
* @api public | ||
* Valid key names are a single, lowercased letter, i.e. "n". | ||
*/ | ||
function debug(name) { | ||
if (!debug.enabled(name)) return function(){}; | ||
exports.formatters = {}; | ||
return function(fmt){ | ||
fmt = coerce(fmt); | ||
/** | ||
* Previously assigned color. | ||
*/ | ||
var curr = new Date; | ||
var ms = curr - (debug[name] || curr); | ||
debug[name] = curr; | ||
var prevColor = 0; | ||
fmt = name | ||
+ ' ' | ||
+ fmt | ||
+ ' +' + debug.humanize(ms); | ||
/** | ||
* Previous log timestamp. | ||
*/ | ||
// This hackery is required for IE8 | ||
// where `console.log` doesn't have 'apply' | ||
window.console | ||
&& console.log | ||
&& Function.prototype.apply.call(console.log, console, arguments); | ||
} | ||
} | ||
var prevTime; | ||
/** | ||
* The currently active debug mode names. | ||
* Select a color. | ||
* | ||
* @return {Number} | ||
* @api private | ||
*/ | ||
debug.names = []; | ||
debug.skips = []; | ||
function selectColor() { | ||
return exports.colors[prevColor++ % exports.colors.length]; | ||
} | ||
/** | ||
* Enables a debug mode by name. This can include modes | ||
* separated by a colon and wildcards. | ||
* Create a debugger with the given `namespace`. | ||
* | ||
* @param {String} name | ||
* @param {String} namespace | ||
* @return {Function} | ||
* @api public | ||
*/ | ||
debug.enable = function(name) { | ||
try { | ||
localStorage.debug = name; | ||
} catch(e){} | ||
function debug(namespace) { | ||
var split = (name || '').split(/[\s,]+/) | ||
, len = split.length; | ||
// define the `disabled` version | ||
function disabled() { | ||
} | ||
disabled.enabled = false; | ||
for (var i = 0; i < len; i++) { | ||
name = split[i].replace('*', '.*?'); | ||
if (name[0] === '-') { | ||
debug.skips.push(new RegExp('^' + name.substr(1) + '$')); | ||
// define the `enabled` version | ||
function enabled() { | ||
var self = enabled; | ||
// set `diff` timestamp | ||
var curr = +new Date(); | ||
var ms = curr - (prevTime || curr); | ||
self.diff = ms; | ||
self.prev = prevTime; | ||
self.curr = curr; | ||
prevTime = curr; | ||
// add the `color` if not set | ||
if (null == self.useColors) self.useColors = exports.useColors(); | ||
if (null == self.color && self.useColors) self.color = selectColor(); | ||
var args = Array.prototype.slice.call(arguments); | ||
args[0] = exports.coerce(args[0]); | ||
if ('string' !== typeof args[0]) { | ||
// anything else let's inspect with %o | ||
args = ['%o'].concat(args); | ||
} | ||
else { | ||
debug.names.push(new RegExp('^' + name + '$')); | ||
} | ||
// apply any `formatters` transformations | ||
var index = 0; | ||
args[0] = args[0].replace(/%([a-z%])/g, function(match, format) { | ||
// if we encounter an escaped % then don't increase the array index | ||
if (match === '%%') return match; | ||
index++; | ||
var formatter = exports.formatters[format]; | ||
if ('function' === typeof formatter) { | ||
var val = args[index]; | ||
match = formatter.call(self, val); | ||
// now we need to remove `args[index]` since it's inlined in the `format` | ||
args.splice(index, 1); | ||
index--; | ||
} | ||
return match; | ||
}); | ||
exports.log.apply(self, args); | ||
} | ||
}; | ||
enabled.enabled = true; | ||
var fn = exports.enabled(namespace) ? enabled : disabled; | ||
fn.namespace = namespace; | ||
return fn; | ||
} | ||
/** | ||
* Disable debug output. | ||
* Enables a debug mode by namespaces. This can include modes | ||
* separated by a colon and wildcards. | ||
* | ||
* @param {String} namespaces | ||
* @api public | ||
*/ | ||
debug.disable = function(){ | ||
debug.enable(''); | ||
}; | ||
function enable(namespaces) { | ||
exports.save(namespaces); | ||
var split = (namespaces || '').split(/[\s,]+/); | ||
var len = split.length; | ||
for (var i = 0; i < len; i++) { | ||
if (!split[i]) continue; // ignore empty strings | ||
namespaces = split[i].replace('*', '.*?'); | ||
if (namespaces[0] === '-') { | ||
exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$')); | ||
} else { | ||
exports.names.push(new RegExp('^' + namespaces + '$')); | ||
} | ||
} | ||
} | ||
/** | ||
* Humanize the given `ms`. | ||
* Disable debug output. | ||
* | ||
* @param {Number} m | ||
* @return {String} | ||
* @api private | ||
* @api public | ||
*/ | ||
debug.humanize = function(ms) { | ||
var sec = 1000 | ||
, min = 60 * 1000 | ||
, hour = 60 * min; | ||
function disable() { | ||
exports.enable(''); | ||
} | ||
if (ms >= hour) return (ms / hour).toFixed(1) + 'h'; | ||
if (ms >= min) return (ms / min).toFixed(1) + 'm'; | ||
if (ms >= sec) return (ms / sec | 0) + 's'; | ||
return ms + 'ms'; | ||
}; | ||
/** | ||
@@ -110,10 +167,11 @@ * Returns true if the given mode name is enabled, false otherwise. | ||
debug.enabled = function(name) { | ||
for (var i = 0, len = debug.skips.length; i < len; i++) { | ||
if (debug.skips[i].test(name)) { | ||
function enabled(name) { | ||
var i, len; | ||
for (i = 0, len = exports.skips.length; i < len; i++) { | ||
if (exports.skips[i].test(name)) { | ||
return false; | ||
} | ||
} | ||
for (var i = 0, len = debug.names.length; i < len; i++) { | ||
if (debug.names[i].test(name)) { | ||
for (i = 0, len = exports.names.length; i < len; i++) { | ||
if (exports.names[i].test(name)) { | ||
return true; | ||
@@ -123,6 +181,10 @@ } | ||
return false; | ||
}; | ||
} | ||
/** | ||
* Coerce `val`. | ||
* | ||
* @param {Mixed} val | ||
* @return {Mixed} | ||
* @api private | ||
*/ | ||
@@ -134,7 +196,1 @@ | ||
} | ||
// persist | ||
try { | ||
if (window.localStorage) debug.enable(localStorage.debug); | ||
} catch(e){} |
{ | ||
"name": "debug" | ||
, "version": "0.8.1" | ||
, "repository": { "type": "git", "url": "git://github.com/visionmedia/debug.git" } | ||
, "description": "small debugging utility" | ||
, "keywords": ["debug", "log", "debugger"] | ||
, "author": "TJ Holowaychuk <tj@vision-media.ca>" | ||
, "dependencies": {} | ||
, "devDependencies": { "mocha": "*" } | ||
, "main": "lib/debug.js" | ||
, "browser": "./debug.js" | ||
, "engines": { "node": "*" } | ||
, "files": [ | ||
"lib/debug.js", | ||
"debug.js" | ||
] | ||
, "component": { | ||
"scripts": { | ||
"debug/index.js": "debug.js" | ||
} | ||
"name": "debug", | ||
"version": "1.0.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/visionmedia/debug.git" | ||
}, | ||
"description": "small debugging utility", | ||
"keywords": [ | ||
"debug", | ||
"log", | ||
"debugger" | ||
], | ||
"author": "TJ Holowaychuk <tj@vision-media.ca>", | ||
"dependencies": { | ||
"ms": "0.6.2" | ||
}, | ||
"devDependencies": { | ||
"browserify": "4.1.6", | ||
"mocha": "*" | ||
}, | ||
"main": "./node.js", | ||
"browser": "./browser.js", | ||
"component": { | ||
"scripts": { | ||
"debug/index.js": "browser.js", | ||
"debug/debug.js": "debug.js" | ||
} | ||
} | ||
} |
@@ -7,3 +7,3 @@ # debug | ||
``` | ||
```bash | ||
$ npm install debug | ||
@@ -73,7 +73,7 @@ ``` | ||
You can also exclude specific debuggers by prefixing them with a "-" character. For example, `DEBUG=* -connect:*` would include all debuggers except those starting with "connect:". | ||
You can also exclude specific debuggers by prefixing them with a "-" character. For example, `DEBUG=*,-connect:*` would include all debuggers except those starting with "connect:". | ||
## Browser support | ||
Debug works in the browser as well, currently persisted by `localStorage`. For example if you have `worker:a` and `worker:b` as shown below, and wish to debug both type `debug.enable('worker:*')` in the console and refresh the page, this will remain until you disable with `debug.disable()`. | ||
Debug works in the browser as well, currently persisted by `localStorage`. For example if you have `worker:a` and `worker:b` as shown below, and wish to debug both type `debug.enable('worker:*')` in the console and refresh the page, this will remain until you disable with `debug.disable()`. | ||
@@ -89,6 +89,14 @@ ```js | ||
setInterval(function(){ | ||
a('doing some work'); | ||
b('doing some work'); | ||
}, 1200); | ||
``` | ||
#### Web Inspector Colors | ||
Colors are also enabled on "Web Inspectors" that understand the `%c` formatting | ||
option. These are WebKit web inspectors, and the Firebug plugin for Firefox. | ||
Colored output looks something like: | ||
![](https://cloud.githubusercontent.com/assets/71256/3139768/b98c5fd8-e8ef-11e3-862a-f7253b6f47c6.png) | ||
## License | ||
@@ -95,0 +103,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
17921
10
366
1
123
1
2
3
2