microseconds
Advanced tools
Comparing version 0.1.0 to 0.2.0
16
index.js
@@ -1,12 +0,10 @@ | ||
'use strict'; | ||
'use strict' | ||
var now = require("./now"), | ||
parse = require("./parse"); | ||
const now = require('./now') | ||
const parse = require('./parse') | ||
var since = function(nano) { | ||
return now() - nano; | ||
}; | ||
const since = (nano) => now() - nano | ||
exports.now = now; | ||
exports.since = since; | ||
exports.parse = parse; | ||
exports.now = now | ||
exports.since = since | ||
exports.parse = parse |
32
now.js
/* global performance */ | ||
'use strict'; | ||
'use strict' | ||
var now; | ||
let now | ||
if (global.process && process.hrtime) { | ||
var hrtime = process.hrtime; | ||
const hrtime = process.hrtime | ||
now = function () { | ||
var hr = hrtime(); | ||
return (hr[0] * 1e9 + hr[1]) / 1e3; | ||
}; | ||
now = () => { | ||
const hr = hrtime() | ||
return (hr[0] * 1e9 + hr[1]) / 1e3 | ||
} | ||
} else if (global.performance && performance.now) { | ||
const timing = performance.timing | ||
const start = (timing && timing.navigationStart) || Date.now() | ||
var start = (performance.timing && performance.timing.navigationStart) || Date.now(); | ||
now = function() { | ||
return (start + performance.now()) * 1e3; | ||
}; | ||
now = () => (start + performance.now()) * 1e3 | ||
} else { | ||
now = function() { | ||
return Date.now() * 1e3; | ||
}; | ||
now = () => Date.now() * 1e3 | ||
} | ||
module.exports = now; | ||
module.exports = now |
{ | ||
"name": "microseconds", | ||
"version": "0.1.0", | ||
"description": "microsecond parser for node and browsers", | ||
"main": "index.js", | ||
"browser": {}, | ||
"author": "Valerio Proietti <kamicane@gmail.com>", | ||
"license": "MIT", | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/kamicane/microseconds.git" | ||
}, | ||
"version": "0.2.0", | ||
"description": "microsecond parser", | ||
"keywords": [ | ||
"microseconds", | ||
"microtime" | ||
"microtime", | ||
"time parser" | ||
], | ||
"bugs": { | ||
"url": "https://github.com/kamicane/microseconds/issues" | ||
}, | ||
"homepage": "https://github.com/kamicane/microseconds" | ||
"homepage": "https://github.com/kamicane/microseconds#readme", | ||
"bugs": "https://github.com/kamicane/microseconds/issues", | ||
"repository": "github:kamicane/microseconds", | ||
"license": "MIT", | ||
"author": "Valerio Proietti <kamicane@gmail.com>", | ||
"main": "index.js", | ||
"devDependencies": { | ||
"prettier": "^1.19.1", | ||
"prettier-plugin-packagejson": "^2.0.6", | ||
"standard": "^14.3.1" | ||
} | ||
} |
125
parse.js
@@ -1,74 +0,87 @@ | ||
'use strict'; | ||
'use strict' | ||
var toString = function() { | ||
function toString () { | ||
const microseconds = this.microseconds | ||
const milliseconds = this.milliseconds | ||
const seconds = this.seconds | ||
const minutes = this.minutes | ||
const hours = this.hours | ||
const days = this.days | ||
var microseconds = this.microseconds, | ||
milliseconds = this.milliseconds, | ||
seconds = this.seconds, | ||
minutes = this.minutes, | ||
hours = this.hours, | ||
days = this.days; | ||
const parts = [ | ||
{ | ||
name: 'day', | ||
value: days | ||
}, | ||
{ | ||
name: 'hour', | ||
value: hours | ||
}, | ||
{ | ||
name: 'minute', | ||
value: minutes | ||
}, | ||
{ | ||
name: 'second', | ||
value: seconds | ||
}, | ||
{ | ||
name: 'millisecond', | ||
value: milliseconds | ||
}, | ||
{ | ||
name: 'microsecond', | ||
value: microseconds | ||
} | ||
] | ||
var parts = [{ | ||
name: 'day', | ||
value: days | ||
}, { | ||
name: 'hour', | ||
value: hours | ||
}, { | ||
name: 'minute', | ||
value: minutes | ||
}, { | ||
name: 'second', | ||
value: seconds | ||
}, { | ||
name: 'millisecond', | ||
value: milliseconds | ||
}, { | ||
name: 'microsecond', | ||
value: microseconds | ||
}]; | ||
const time = [] | ||
var time = []; | ||
for (var i = 0; i < parts.length; i++) { | ||
var part = parts[i]; | ||
for (let i = 0; i < parts.length; i++) { | ||
const part = parts[i] | ||
if (part.value === 0) { | ||
if (!time.length) continue; // nothing was added yet | ||
if (!time.length) continue // nothing was added yet | ||
var broken = false; | ||
let broken = false | ||
for (var j = i; j < parts.length; j++) { | ||
var p = parts[j]; | ||
for (let j = i; j < parts.length; j++) { | ||
const p = parts[j] | ||
if (p.value) { | ||
broken = true; | ||
break; | ||
broken = true | ||
break | ||
} | ||
} | ||
if (!broken) break; | ||
if (!broken) break | ||
} | ||
time.push(part.value, part.value === 1 ? part.name : part.name + 's'); | ||
time.push(part.value, part.value === 1 ? part.name : part.name + 's') | ||
} | ||
return time.join(' '); | ||
}; | ||
return time.join(' ') | ||
} | ||
module.exports = function parse(nano) { | ||
module.exports = (nano) => { | ||
const ms = nano / 1000 | ||
const ss = ms / 1000 | ||
const mm = ss / 60 | ||
const hh = mm / 60 | ||
const dd = hh / 24 | ||
var ms = nano / 1000; | ||
var ss = ms / 1000; | ||
var mm = ss / 60; | ||
var hh = mm / 60; | ||
var dd = hh / 24; | ||
const microseconds = Math.round((ms % 1) * 1000) | ||
const milliseconds = Math.floor(ms % 1000) | ||
const seconds = Math.floor(ss % 60) | ||
const minutes = Math.floor(mm % 60) | ||
const hours = Math.floor(hh % 24) | ||
const days = Math.floor(dd) | ||
var microseconds = Math.round((ms % 1) * 1000); | ||
var milliseconds = Math.floor(ms % 1000); | ||
var seconds = Math.floor(ss % 60); | ||
var minutes = Math.floor(mm % 60); | ||
var hours = Math.floor(hh % 24); | ||
var days = Math.floor(dd); | ||
return { microseconds: microseconds, milliseconds: milliseconds, seconds: seconds, minutes: minutes, hours: hours, days: days, toString: toString }; | ||
}; | ||
return { | ||
microseconds, | ||
milliseconds, | ||
seconds, | ||
minutes, | ||
hours, | ||
days, | ||
toString | ||
} | ||
} |
# μs | ||
A small focused library to generate and parse microseconds, for node and browsers. | ||
Generate and parse microseconds. | ||
To get the current timestamp, it uses [hrtime](https://nodejs.org/api/process.html#process_process_hrtime) in node.js, [performance.now](https://developer.mozilla.org/en-US/docs/Web/API/Performance.now()) in browsers. Falls back to `Date.now() * 1000`. | ||
Uses [hrtime](https://nodejs.org/api/process.html#process_process_hrtime) in node.js, [performance.now](https://developer.mozilla.org/en-US/docs/Web/API/Performance.now()) in browsers. Falls back to `Date.now() * 1000`. | ||
Best browserified with [quickstart](https://github.com/spotify/quickstart). | ||
## API | ||
```js | ||
var μs = require('microseconds'); | ||
const μs = require('microseconds') | ||
``` | ||
@@ -20,4 +18,4 @@ | ||
```js | ||
var now = μs.now(); | ||
1404398280599786 | ||
const now = μs.now() | ||
// 1404398280599786 | ||
``` | ||
@@ -30,4 +28,4 @@ | ||
```js | ||
var parsed = μs.parse(now); | ||
{ microseconds: 786, milliseconds: 599, seconds: 0, minutes: 38, hours: 14, days: 16254 } | ||
const parsed = μs.parse(now) | ||
// { microseconds: 786, milliseconds: 599, seconds: 0, minutes: 38, hours: 14, days: 16254 } | ||
``` | ||
@@ -38,13 +36,13 @@ | ||
```js | ||
parsed.toString(); | ||
"16254 days 14 hours 38 minutes 0 seconds 599 milliseconds 786 microseconds" | ||
parsed.toString() | ||
// "16254 days 14 hours 38 minutes 0 seconds 599 milliseconds 786 microseconds" | ||
μs.parse(1000).toString(); | ||
"1 millisecond" | ||
μs.parse(1000).toString() | ||
// "1 millisecond" | ||
μs.parse(1).toString(); | ||
"1 microsecond" | ||
μs.parse(1).toString() | ||
// "1 microsecond" | ||
μs.parse(4231002).toString(); | ||
"4 seconds 231 milliseconds 2 microseconds" | ||
μs.parse(4231002).toString() | ||
// "4 seconds 231 milliseconds 2 microseconds" | ||
``` | ||
@@ -55,4 +53,4 @@ | ||
```js | ||
var before = μs.now(); | ||
var time = μs.since(before); // time passed | ||
const before = μs.now() | ||
const time = μs.since(before) // time passed | ||
``` |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
6
99
3949
3
1
53
1