From b021f7cdd43476609c2aa9e850aa67cf9d378d0f Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 13 Aug 2017 12:21:56 -0700 Subject: [PATCH] util: minor. --- lib/utils/util.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/utils/util.js b/lib/utils/util.js index 84dd6c86..0e3659ea 100644 --- a/lib/utils/util.js +++ b/lib/utils/util.js @@ -183,7 +183,7 @@ util.isAscii = function isAscii(str) { */ util.isBase58 = function isBase58(str) { - return typeof str === 'string' && /^[1-9a-zA-Z]+$/.test(str); + return typeof str === 'string' && /^[1-9A-Za-z]+$/.test(str); }; /** @@ -197,7 +197,7 @@ util.isBase58 = function isBase58(str) { util.isHex = function isHex(str) { if (typeof str !== 'string') return false; - return /^[0-9a-f]+$/i.test(str) && str.length % 2 === 0; + return str.length % 2 === 0 && /^[0-9A-Fa-f]+$/.test(str); }; /** @@ -358,7 +358,7 @@ util.error = function error(...args) { /** * Return hrtime (shim for browser). * @param {Array} time - * @returns {Array} + * @returns {Array} [seconds, nanoseconds] */ util.hrtime = function hrtime(time) { @@ -372,9 +372,14 @@ util.hrtime = function hrtime(time) { } const ms = now % 1000; - const sec = (now - ms) / 1000; - return [sec, ms * 1e6]; + // Seconds + const hi = (now - ms) / 1000; + + // Nanoseconds + const lo = ms * 1e6; + + return [hi, lo]; } if (time) { @@ -773,8 +778,11 @@ util.binaryRemove = function binaryRemove(items, item, compare) { */ util.isUpperCase = function isUpperCase(str) { + assert(typeof str === 'string'); + if (str.length === 0) return false; + return (str.charCodeAt(0) & 32) === 0; }; @@ -786,15 +794,14 @@ util.isUpperCase = function isUpperCase(str) { */ util.startsWith = function startsWith(str, prefix) { + assert(typeof str === 'string'); + + if (!str.startsWith) + return str.indexOf(prefix) === 0; + return str.startsWith(prefix); }; -if (!''.startsWith) { - util.startsWith = function startsWith(str, prefix) { - return str.indexOf(prefix) === 0; - }; -} - /** * Get memory usage info. * @returns {Object}