Comparing version 0.2.1 to 0.3.0
92
index.js
@@ -134,14 +134,7 @@ /*! | ||
Time.prototype.diff = function(name, options) { | ||
var magenta = log.colors.magenta; | ||
var gray = log.colors.gray; | ||
Time.prototype.diff = function(namespace, options) { | ||
var opts = {}; | ||
extend(opts, this.options, options); | ||
if (typeof opts.times === 'undefined') { | ||
return function() {}; | ||
} | ||
this.start(name); | ||
this.start(namespace); | ||
var time = this; | ||
@@ -151,4 +144,20 @@ var prev; | ||
function diff(msg) { | ||
var val; | ||
if (typeof opts.logDiff === 'string') { | ||
if (!toRegex(opts.logDiff).test(namespace)) { | ||
// garbage collect | ||
time.times[namespace] = null; | ||
return; | ||
} | ||
} | ||
if (opts.logDiff === false) { | ||
// garbage collect | ||
time.times[namespace] = null; | ||
return; | ||
} | ||
var timestamp = log.timestamp; | ||
var magenta = log.colors.magenta; | ||
var gray = log.colors.gray; | ||
var name = opts.prefix === false ? '' : namespace; | ||
if (typeof opts.diffColor === 'function') { | ||
@@ -158,3 +167,3 @@ gray = opts.diffColor; | ||
if (opts.color === false) { | ||
if (opts.nocolor === true) { | ||
magenta = identity; | ||
@@ -164,26 +173,30 @@ gray = identity; | ||
if (opts.times === true || opts.times === name) { | ||
var timeDiff = magenta(time.end(name)); | ||
if (typeof prev !== 'undefined') { | ||
val = time.end(prev); | ||
} | ||
if (opts.timestamp === false) { | ||
timestamp = ''; | ||
} | ||
// start the next cycle | ||
time.start(msg); | ||
prev = msg; | ||
var elapsed = magenta(time.end(namespace)); | ||
var val; | ||
if (typeof val === 'string') { | ||
timeDiff += gray(' (+' + val + ')'); | ||
} | ||
if (typeof prev !== 'undefined') { | ||
val = time.end(prev); | ||
} | ||
// create the arguments to log out | ||
var args = [name, msg, timeDiff]; | ||
// start the next cycle | ||
time.start(msg); | ||
prev = msg; | ||
// support custom `.format` function | ||
if (typeof opts.format === 'function') { | ||
opts.format.apply(null, args); | ||
} else { | ||
format.apply(null, args); | ||
} | ||
if (typeof val === 'string') { | ||
elapsed += gray(' (+' + val + ')'); | ||
} | ||
// create the arguments to log out | ||
var args = [timestamp, name, msg, elapsed].filter(Boolean); | ||
// support custom `.format` function | ||
if (typeof opts.formatArgs === 'function') { | ||
args = [].concat(opts.formatArgs.apply(null, args) || []); | ||
} | ||
console.error.apply(console, args); | ||
}; | ||
@@ -194,2 +207,10 @@ | ||
function toRegex(str) { | ||
if (~str.indexOf(',')) { | ||
str = '(' + str.split(',').join('|') + ')'; | ||
} | ||
str = str.replace(/\*/g, '[^.]*?'); | ||
return new RegExp('^' + str + '$'); | ||
} | ||
function identity(val) { | ||
@@ -199,10 +220,2 @@ return val; | ||
function format(name, msg, timeDiff) { | ||
var args = [log.timestamp, name + ':', msg]; | ||
if (arguments.length === 3) { | ||
args.push(timeDiff); | ||
} | ||
console.error.apply(console, args); | ||
} | ||
/** | ||
@@ -213,4 +226,1 @@ * Expose `time` | ||
module.exports = Time; | ||
module.exports.format = format; | ||
{ | ||
"name": "time-diff", | ||
"description": "Returns the formatted, high-resolution time difference between `start` and `end` times.", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"homepage": "https://github.com/jonschlinkert/time-diff", | ||
@@ -53,10 +53,11 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"pretty-time", | ||
"verb" | ||
"verb", | ||
"minimist" | ||
], | ||
"related": { | ||
"list": [ | ||
"ansi-colors", | ||
"log-utils", | ||
"time-diff", | ||
"pretty-time", | ||
"ansi-colors" | ||
"time-diff" | ||
] | ||
@@ -63,0 +64,0 @@ }, |
# time-diff [![NPM version](https://img.shields.io/npm/v/time-diff.svg?style=flat)](https://www.npmjs.com/package/time-diff) [![NPM downloads](https://img.shields.io/npm/dm/time-diff.svg?style=flat)](https://npmjs.org/package/time-diff) [![Build Status](https://img.shields.io/travis/jonschlinkert/time-diff.svg?style=flat)](https://travis-ci.org/jonschlinkert/time-diff) | ||
> Returns the formatted, high-resolution time difference between `start` and `end` times. | ||
Returns the formatted, high-resolution time difference between `start` and `end` times. | ||
@@ -107,2 +107,67 @@ ## Install | ||
## Options | ||
### options.logDiff | ||
Disable time diffs, or filter time diffs to the specified name(s). | ||
**type**: `Boolean|String` | ||
**default**: `undefined` | ||
### options.nocolor | ||
Set to `true` to disable color in the output. | ||
**type**: `Boolean` | ||
**default**: `undefined` | ||
**Example** | ||
```js | ||
var diff = time.diff('foo', {nocolor: true}); | ||
``` | ||
### options.formatArgs | ||
Format arguments passed to `process.stderr`. | ||
**type**: `Function` | ||
**default**: `undefined` | ||
**Examples** | ||
Show `message` and `elapsed` time only: | ||
```js | ||
var time = new Time(); | ||
var diff = time.diff('foo', { | ||
formatArgs: function(timestamp, name, msg, elapsed) { | ||
return [msg, elapsed]; | ||
} | ||
}); | ||
diff('first diff'); | ||
//=> 'first diff 36μs' | ||
diff('second diff'); | ||
//=> 'second diff 71μs' | ||
``` | ||
Show `name` and `elapsed` time only: | ||
```js | ||
var diff = time.diff('foo', { | ||
formatArgs: function(timestamp, name, msg, elapsed) { | ||
return [name, elapsed]; | ||
} | ||
}); | ||
diff('first diff'); | ||
//=> 'foo 36μs' | ||
diff('second diff'); | ||
//=> 'foo 71μs' | ||
``` | ||
## Examples | ||
@@ -186,2 +251,14 @@ | ||
## CLI usage | ||
If you're using `time-diff` with a command line application, try using [minimist](https://github.com/substack/minimist) for setting options. | ||
```js | ||
var opts = {alias: {nocolor: 'n', logTime: 't', logDiff: 'd'}}; | ||
var argv = require('minimist')(process.argv.slice(2), opts); | ||
var Time = require('time-diff'); | ||
var time = new Time(argv); | ||
``` | ||
## Related projects | ||
@@ -192,3 +269,3 @@ | ||
* [ansi-colors](https://www.npmjs.com/package/ansi-colors): Collection of ansi colors and styles. | [homepage](https://github.com/doowb/ansi-colors) | ||
* [log-utils](https://www.npmjs.com/package/log-utils): Basic utils for command line applications. | [homepage](https://github.com/jonschlinkert/log-utils) | ||
* [log-utils](https://www.npmjs.com/package/log-utils): Basic logging utils: colors, symbols and timestamp. | [homepage](https://github.com/jonschlinkert/log-utils) | ||
* [pretty-time](https://www.npmjs.com/package/pretty-time): Easily format the time from node.js `process.hrtime`. Works with timescales ranging from weeks to nanoseconds. | [homepage](https://github.com/jonschlinkert/pretty-time) | ||
@@ -237,2 +314,2 @@ * [time-diff](https://www.npmjs.com/package/time-diff): Returns the formatted, high-resolution time difference between `start` and `end` times. | [homepage](https://github.com/jonschlinkert/time-diff) | ||
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on April 13, 2016._ | ||
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on April 27, 2016._ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14717
186
311