Comparing version 0.1.0 to 0.2.0
156
index.js
@@ -10,5 +10,6 @@ /*! | ||
var red = require('ansi-red'); | ||
var extend = require('extend-shallow'); | ||
var isNumber = require('is-number'); | ||
var pretty = require('pretty-time'); | ||
var isNumber = require('is-number'); | ||
var log = require('log-utils'); | ||
@@ -26,6 +27,11 @@ /** | ||
function Time(smallest, digits) { | ||
function Time(options) { | ||
if (!(this instanceof Time)) { | ||
return new Time(smallest, digits); | ||
return new Time(options); | ||
} | ||
this.options = options || {}; | ||
var smallest = this.options.smallest; | ||
var digits = this.options.digits; | ||
if (isNumber(smallest)) { | ||
@@ -35,2 +41,3 @@ digits = smallest; | ||
} | ||
this.smallest = smallest; | ||
@@ -41,2 +48,14 @@ this.digits = digits; | ||
/** | ||
* Start a timer for the given `name`. | ||
* | ||
* ```js | ||
* var time = new Time(); | ||
* time.start('foo'); | ||
* ``` | ||
* @param {String} `name` Name to use for the starting time. | ||
* @return {Array} Returns the array from `process.hrtime()` | ||
* @api public | ||
*/ | ||
Time.prototype.start = function(name) { | ||
@@ -46,6 +65,31 @@ return (this.times[name] = process.hrtime()); | ||
/** | ||
* Returns the cumulative elapsed time since the **first time** `time.start(name)` | ||
* was called. | ||
* | ||
* ```js | ||
* var time = new Time(); | ||
* time.start('foo'); | ||
* | ||
* // do stuff | ||
* time.end('foo'); | ||
* //=> 104μs | ||
* | ||
* // do more stuff | ||
* time.end('foo'); | ||
* //=> 1ms | ||
* | ||
* // do more stuff | ||
* time.end('foo'); | ||
* //=> 2ms | ||
* ``` | ||
* @param {String} `name` The name of the cached starting time to create the diff | ||
* @return {Array} Returns the array from `process.hrtime()` | ||
* @api public | ||
*/ | ||
Time.prototype.end = function(name, smallest, digits) { | ||
var start = this.times[name]; | ||
if (typeof start === 'undefined') { | ||
throw new Error(red('start time not defined for "' + name + '"')); | ||
throw new Error(log.colors.red('start time not defined for "' + name + '"')); | ||
} | ||
@@ -64,2 +108,101 @@ | ||
/** | ||
* Returns a function for logging out out both the cumulative elapsed time since | ||
* the first time `.diff(name)` was called, as well as the incremental elapsed | ||
* time since the last `.diff(name)` was called. Unlike `.end()`, this method logs | ||
* to `stderr` instead of returning a string. We could probably change this to | ||
* return an object, feedback welcome. | ||
* | ||
* ```js | ||
* var time = new Time(); | ||
* var diff = time.diff('foo'); | ||
* | ||
* // do stuff | ||
* diff('foo'); | ||
* //=> 104μs | ||
* | ||
* // do more stuff | ||
* diff('bar'); | ||
* //=> 1ms | ||
* | ||
* // do more stuff | ||
* diff('baz'); | ||
* //=> 2ms | ||
* ``` | ||
* Results in something like: | ||
* | ||
* <img width="559" alt="screen shot 2016-04-13 at 6 17 31 pm" src="https://cloud.githubusercontent.com/assets/383994/14512156/93f0bf78-01aa-11e6-9859-8f2a4b47043d.png"> | ||
* | ||
* @param {String} `name` The name of the starting time to store. | ||
* @param {String} `options` | ||
* @api public | ||
*/ | ||
Time.prototype.diff = function(name, options) { | ||
var magenta = log.colors.magenta; | ||
var gray = log.colors.gray; | ||
var opts = {}; | ||
extend(opts, this.options, options); | ||
if (typeof opts.times === 'undefined') { | ||
return function() {}; | ||
} | ||
this.start(name); | ||
var time = this; | ||
var prev; | ||
function diff(msg) { | ||
var val; | ||
if (typeof prev !== 'undefined') { | ||
val = time.end(prev); | ||
} | ||
if (typeof opts.diffColor === 'function') { | ||
gray = opts.diffColor; | ||
} | ||
if (opts.color === false) { | ||
magenta = identity; | ||
gray = identity; | ||
} | ||
if (opts.times === true || opts.times === name) { | ||
var timeDiff = magenta(time.end(name)); | ||
if (typeof val === 'string') { | ||
timeDiff += gray(' (+' + val + ')'); | ||
} | ||
// create the arguments to log out | ||
var args = [name, msg, timeDiff]; | ||
// support custom `.format` function | ||
if (typeof opts.format === 'function') { | ||
opts.format.apply(null, args); | ||
} else { | ||
format.apply(null, args); | ||
} | ||
} | ||
time.start(name); | ||
prev = name; | ||
}; | ||
return diff; | ||
}; | ||
function identity(val) { | ||
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); | ||
} | ||
/** | ||
* Expose `time` | ||
@@ -69,1 +212,4 @@ */ | ||
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.1.0", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/jonschlinkert/time-diff", | ||
@@ -23,19 +23,27 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"dependencies": { | ||
"ansi-red": "^0.1.1", | ||
"extend-shallow": "^2.0.1", | ||
"is-number": "^2.1.0", | ||
"log-utils": "^0.1.0", | ||
"pretty-time": "^0.2.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*" | ||
"gulp-format-md": "^0.1.7", | ||
"minimist": "^1.2.0", | ||
"mocha": "^2.4.5", | ||
"strip-color": "^0.1.0" | ||
}, | ||
"keywords": [ | ||
"console", | ||
"diff", | ||
"difference", | ||
"elapse", | ||
"elapsed", | ||
"log", | ||
"time" | ||
"pretty", | ||
"terminal", | ||
"time", | ||
"time-diff", | ||
"timer" | ||
], | ||
"verb": { | ||
"related": { | ||
"list": [ | ||
"pretty-time" | ||
] | ||
}, | ||
"plugins": [ | ||
@@ -45,6 +53,25 @@ "gulp-format-md" | ||
"reflinks": [ | ||
"pretty-time" | ||
"pretty-time", | ||
"verb" | ||
], | ||
"layout": "default" | ||
"related": { | ||
"list": [ | ||
"log-utils", | ||
"time-diff", | ||
"pretty-time", | ||
"ansi-colors" | ||
] | ||
}, | ||
"layout": "default", | ||
"toc": { | ||
"render": false | ||
}, | ||
"run": true, | ||
"lint": { | ||
"reflinks": true | ||
}, | ||
"tasks": [ | ||
"readme" | ||
] | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
12631
175
233
4
4
+ Addedextend-shallow@^2.0.1
+ Addedlog-utils@^0.1.0
+ Addedansi-bgblack@0.1.1(transitive)
+ Addedansi-bgblue@0.1.1(transitive)
+ Addedansi-bgcyan@0.1.1(transitive)
+ Addedansi-bggreen@0.1.1(transitive)
+ Addedansi-bgmagenta@0.1.1(transitive)
+ Addedansi-bgred@0.1.1(transitive)
+ Addedansi-bgwhite@0.1.1(transitive)
+ Addedansi-bgyellow@0.1.1(transitive)
+ Addedansi-black@0.1.1(transitive)
+ Addedansi-blue@0.1.1(transitive)
+ Addedansi-bold@0.1.1(transitive)
+ Addedansi-colors@0.1.0(transitive)
+ Addedansi-cyan@0.1.1(transitive)
+ Addedansi-dim@0.1.1(transitive)
+ Addedansi-gray@0.1.1(transitive)
+ Addedansi-green@0.1.1(transitive)
+ Addedansi-grey@0.1.1(transitive)
+ Addedansi-hidden@0.1.1(transitive)
+ Addedansi-inverse@0.1.1(transitive)
+ Addedansi-italic@0.1.1(transitive)
+ Addedansi-magenta@0.1.1(transitive)
+ Addedansi-reset@0.1.1(transitive)
+ Addedansi-strikethrough@0.1.1(transitive)
+ Addedansi-underline@0.1.1(transitive)
+ Addedansi-white@0.1.1(transitive)
+ Addedansi-yellow@0.1.1(transitive)
+ Addederror-symbol@0.1.0(transitive)
+ Addedextend-shallow@2.0.1(transitive)
+ Addedinfo-symbol@0.1.0(transitive)
+ Addedis-extendable@0.1.1(transitive)
+ Addedlazy-cache@0.2.7(transitive)
+ Addedlog-ok@0.1.1(transitive)
+ Addedlog-utils@0.1.5(transitive)
+ Addedsuccess-symbol@0.1.0(transitive)
+ Addedtime-stamp@1.1.0(transitive)
+ Addedwarning-symbol@0.1.0(transitive)
- Removedansi-red@^0.1.1