Permalink
Browse files

Add natron-hrtime-format package

  • Loading branch information...
stefanr committed Dec 12, 2015
1 parent 9f8c001 commit 623951c7cf60cd3f04186627342ac2630bd51818
@@ -0,0 +1,10 @@
#
# .npmignore
#
# --------------------------------------
/doc
/src
/test
/.npmignore
/*.tgz
@@ -0,0 +1,44 @@
# [![Natron][natron-img]][natron-url]
[natron-img]: http://static.natronjs.com/img/natronjs.svg
[natron-url]: http://natronjs.com/
**High-Resolution Time Format**
[![Version][npm-img]][npm-url]
[![Downloads][dlm-img]][npm-url]
[![Build Status][travis-img]][travis-url]
[![ReadMe][readme-img]][readme-url]
[![Gitter Chat][gitter-img]][gitter-url]
[npm-img]: https://img.shields.io/npm/v/natron-hrtime-format.svg
[npm-url]: https://npmjs.org/package/natron-hrtime-format
[dlm-img]: https://img.shields.io/npm/dm/natron-hrtime-format.svg
[travis-img]: https://travis-ci.org/natronjs/natron.svg
[travis-url]: https://travis-ci.org/natronjs/natron
[readme-img]: https://img.shields.io/badge/read-me-orange.svg
[readme-url]: https://natron.readme.io/docs/module-natron-hrtime-format
[gitter-img]: https://badges.gitter.im/Join%20Chat.svg
[gitter-url]: https://gitter.im/natronjs/natron
This module is part of [Natron][natron-url] and contains
formatting utilities for [`process.hrtime`](https://nodejs.org/api/process.html#process_process_hrtime).
## Documentation
See the [documentation for `natron-hrtime-format`][readme-url]
## Usage
```js
import {format} from "natron-hrtime-format";
let start = process.hrtime();
setTimeout(() => {
let end = process.hrtime(start);
console.log(format(end));
}, Math.random() * 2000);
```
@@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _format = require("./format");
Object.defineProperty(exports, "format", {
enumerable: true,
get: function get() {
return _format.format;
}
});
@@ -0,0 +1,44 @@
"use strict";
var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; })();
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.format = format;
/**
* @module natron-hrtime-format
*/
var HRTIME_UNITS = ["ns", "μs", "ms", "s", "min", "h"];
function format(time) {
if (!(time instanceof Array)) {
time = [0, NaN];
}
var t = undefined;var i = undefined;var j = undefined;var _time = time;
var _time2 = _slicedToArray(_time, 2);
var s = _time2[0];
var ns = _time2[1];
if (s > 0) {
// Seconds
for (t = s, i = 3, j = ""; t > 60 && i < 5; i++) {
j = " " + (t % 60 | 0) + " " + HRTIME_UNITS[i];
t /= 60;
}
if (i > 3) {
return (t | 0) + " " + HRTIME_UNITS[i] + j;
}
t += ns / 1e9;
} else {
// Nanoseconds
for (t = ns, i = 0, j = ""; t > 1e3; i++) {
t /= 1e3;
}
}
var f = t.toFixed(i && t < 100 && (t < 10 ? 2 : 1));
return f + " " + HRTIME_UNITS[i];
}
@@ -0,0 +1,22 @@
{
"name": "natron-hrtime-format",
"version": "0.2.0-beta.1",
"description": "High-Resolution Time Format",
"keywords": [
"natron",
"hrtime",
"format",
"util"
],
"homepage": "http://natronjs.com/",
"author": "Stefan Rumzucker",
"repository": {
"type": "git",
"url": "git+https://github.com/natronjs/natron.git"
},
"main": "dist/cjs/__main__",
"engines": {
"node": ">=0.12"
},
"license": "MIT"
}
@@ -0,0 +1,4 @@
/**
* @module natron-hrtime-format
*/
export {format} from "./format";
@@ -0,0 +1,30 @@
/**
* @module natron-hrtime-format
*/
const HRTIME_UNITS = ["ns", "μs", "ms", "s", "min", "h"];
export function format(time: [number, number]): string {
if (!(time instanceof Array)) {
time = [0, NaN];
}
let t, i, j, [s, ns] = time;
if (s > 0) {
// Seconds
for (t = s, i = 3, j = ""; t > 60 && i < 5; i++) {
j = " " + ((t % 60)|0) + " " + HRTIME_UNITS[i];
t /= 60;
}
if (i > 3) {
return (t|0) + " " + HRTIME_UNITS[i] + j;
}
t += ns / 1e9;
} else {
// Nanoseconds
for (t = ns, i = 0, j = ""; t > 1e3; i++) {
t /= 1e3;
}
}
let f = t.toFixed(i && t < 100 && (t < 10 ? 2 : 1));
return f + " " + HRTIME_UNITS[i];
}

0 comments on commit 623951c

Please sign in to comment.