itns-sidechain/lib/utils/util.js

145 lines
2.5 KiB
JavaScript
Raw Normal View History

/*!
2016-11-19 10:45:31 -08:00
* util.js - utils for bcoin
2015-12-18 22:53:31 -08:00
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
2017-02-03 22:47:26 -08:00
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
2016-06-09 16:18:50 -07:00
* https://github.com/bcoin-org/bcoin
2015-12-18 22:53:31 -08:00
*/
2016-06-13 01:06:01 -07:00
'use strict';
2017-11-17 00:00:36 -08:00
const assert = require('assert');
2017-02-03 22:47:26 -08:00
/**
* @exports utils/util
*/
2017-06-29 20:54:07 -07:00
const util = exports;
2017-02-03 22:47:26 -08:00
2017-08-06 03:48:38 -07:00
/**
* Return hrtime (shim for browser).
* @param {Array} time
2017-08-13 12:21:56 -07:00
* @returns {Array} [seconds, nanoseconds]
2017-08-06 03:48:38 -07:00
*/
2017-10-30 21:02:05 -07:00
util.bench = function bench(time) {
2017-08-06 03:48:38 -07:00
if (!process.hrtime) {
2017-10-26 04:07:36 -07:00
const now = Date.now();
2017-08-06 03:48:38 -07:00
if (time) {
const [hi, lo] = time;
const start = hi * 1000 + lo / 1e6;
return now - start;
}
const ms = now % 1000;
2017-08-13 12:21:56 -07:00
// Seconds
const hi = (now - ms) / 1000;
// Nanoseconds
const lo = ms * 1e6;
return [hi, lo];
2017-08-06 03:48:38 -07:00
}
if (time) {
const [hi, lo] = process.hrtime(time);
return hi * 1000 + lo / 1e6;
}
return process.hrtime();
};
/**
* Get current time in unix time (seconds).
* @returns {Number}
*/
2016-11-19 10:45:31 -08:00
util.now = function now() {
2017-10-26 04:07:36 -07:00
return Math.floor(Date.now() / 1000);
2016-01-04 04:05:08 -08:00
};
2017-12-29 15:45:35 -08:00
/**
* Get current time in unix time (milliseconds).
* @returns {Number}
*/
util.ms = function ms() {
return Date.now();
};
2016-04-19 20:42:46 -07:00
/**
* Create a Date ISO string from time in unix time (seconds).
2017-07-25 14:23:10 -07:00
* @param {Number?} time - Seconds in unix time.
2016-04-19 20:42:46 -07:00
* @returns {String}
*/
2017-07-25 14:23:10 -07:00
util.date = function date(time) {
if (time == null)
time = util.now();
2016-04-19 20:42:46 -07:00
2017-07-25 14:23:10 -07:00
return new Date(time * 1000).toISOString().slice(0, -5) + 'Z';
2016-04-19 20:42:46 -07:00
};
/**
* Get unix seconds from a Date string.
2017-07-24 19:05:23 -07:00
* @param {String?} date - Date ISO String.
2016-04-19 20:42:46 -07:00
* @returns {Number}
*/
2016-11-19 10:45:31 -08:00
util.time = function time(date) {
2016-04-19 20:42:46 -07:00
if (date == null)
2016-11-19 10:45:31 -08:00
return util.now();
2016-04-19 20:42:46 -07:00
return new Date(date) / 1000 | 0;
};
2017-11-16 23:52:14 -08:00
/**
* Reverse a hex-string.
* @param {String} str - Hex string.
* @returns {String} Reversed hex string.
*/
util.revHex = function revHex(str) {
assert(typeof str === 'string');
assert((str.length & 1) === 0);
let out = '';
for (let i = str.length - 2; i >= 0; i -= 2)
out += str[i] + str[i + 1];
return out;
};
2018-01-02 20:24:56 -08:00
/**
* Convert u32 to padded hex.
* @param {Number} num
* @returns {String}
*/
util.hex32 = function hex32(num) {
assert((num >>> 0) === num);
num = num.toString(16);
switch (num.length) {
case 1:
return `0000000${num}`;
case 2:
return `000000${num}`;
case 3:
return `00000${num}`;
case 4:
return `0000${num}`;
case 5:
return `000${num}`;
case 6:
return `00${num}`;
case 7:
return `0${num}`;
case 8:
return `${num}`;
default:
throw new Error();
}
};