Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

time-diff

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

time-diff - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

README.md

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;

49

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc