• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

handshake-org / hsd / 11142522588

02 Oct 2024 10:56AM UTC coverage: 70.04% (+0.007%) from 70.033%
11142522588

push

github

nodech
Merge PR #902 from 'nodech/update-types'

7761 of 12916 branches covered (60.09%)

Branch coverage included in aggregate %.

67 of 92 new or added lines in 25 files covered. (72.83%)

8 existing lines in 5 files now uncovered.

24790 of 33559 relevant lines covered (73.87%)

34019.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

49.02
/lib/utils/util.js
1
/*!
2
 * util.js - utils for hsd
3
 * Copyright (c) 2017-2018, Christopher Jeffrey (MIT License).
4
 * https://github.com/handshake-org/hsd
5
 */
6

7
'use strict';
8

9
const assert = require('bsert');
70✔
10

11
/**
12
 * @exports utils/util
13
 */
14

15
const util = exports;
70✔
16

17
/** @typedef {ReturnType<typeof process.hrtime>} HRTime */
18

19
/**
20
 * Return hrtime (shim for browser).
21
 * @param {HRTime?} [time]
22
 * @returns {HRTime|Number} [seconds, nanoseconds]
23
 */
24

25
util.bench = function bench(time) {
70✔
26
  if (!process.hrtime) {
38,580!
27
    const now = Date.now();
×
28

29
    if (time) {
×
30
      const [hi, lo] = time;
×
31
      const start = hi * 1000 + lo / 1e6;
×
32
      return now - start;
×
33
    }
34

35
    const ms = now % 1000;
×
36

37
    // Seconds
38
    const hi = (now - ms) / 1000;
×
39

40
    // Nanoseconds
41
    const lo = ms * 1e6;
×
42

43
    return [hi, lo];
×
44
  }
45

46
  if (time) {
38,580✔
47
    const [hi, lo] = process.hrtime(time);
18,936✔
48
    return hi * 1000 + lo / 1e6;
18,936✔
49
  }
50

51
  return process.hrtime();
19,644✔
52
};
53

54
/**
55
 * Get current time in unix time (seconds).
56
 * @returns {Number}
57
 */
58

59
util.now = function now() {
70✔
60
  return Math.floor(Date.now() / 1000);
108,219✔
61
};
62

63
/**
64
 * Get current time in unix time (milliseconds).
65
 * @returns {Number}
66
 */
67

68
util.ms = function ms() {
70✔
69
  return Date.now();
×
70
};
71

72
/**
73
 * Create a Date ISO string from time in unix time (seconds).
74
 * @param {Number?} time - Seconds in unix time.
75
 * @returns {String}
76
 */
77

78
util.date = function date(time) {
70✔
79
  if (time == null)
930!
80
    time = util.now();
×
81

82
  return new Date(time * 1000).toISOString().slice(0, -5) + 'Z';
930✔
83
};
84

85
/**
86
 * Get unix seconds from a Date string.
87
 * @param {String?} date - Date ISO String.
88
 * @returns {Number}
89
 */
90

91
util.time = function time(date) {
70✔
92
  if (date == null)
×
93
    return util.now();
×
94

NEW
95
  return Number(new Date(date)) / 1000 | 0;
×
96
};
97

98
/**
99
 * Convert u32 to padded hex.
100
 * @param {Number} num
101
 * @returns {String}
102
 */
103

104
util.hex32 = function hex32(num) {
70✔
105
  assert((num >>> 0) === num);
159✔
106
  const numStr = num.toString(16);
159✔
107
  switch (numStr.length) {
159!
108
    case 1:
109
      return `0000000${numStr}`;
59✔
110
    case 2:
NEW
111
      return `000000${numStr}`;
×
112
    case 3:
NEW
113
      return `00000${numStr}`;
×
114
    case 4:
NEW
115
      return `0000${numStr}`;
×
116
    case 5:
NEW
117
      return `000${numStr}`;
×
118
    case 6:
NEW
119
      return `00${numStr}`;
×
120
    case 7:
NEW
121
      return `0${numStr}`;
×
122
    case 8:
123
      return `${numStr}`;
100✔
124
    default:
125
      throw new Error();
×
126
  }
127
};
128

129
/**
130
 * Parse hex.
131
 * @param {String} str
132
 * @param {Number} size
133
 * @returns {Buffer}
134
 */
135

136
util.parseHex = function parseHex(str, size) {
70✔
137
  if (size == null)
1,619!
138
    size = -1;
×
139

140
  assert(typeof str === 'string');
1,619✔
141
  assert(size === -1 || (size >>> 0) === size);
1,619✔
142

143
  if (str.length & 1)
1,619!
144
    throw new Error('Invalid hex string.');
×
145

146
  if (size !== -1) {
1,619✔
147
    if ((str.length >>> 1) !== size)
89!
148
      throw new Error('Invalid hex string.');
×
149
  }
150

151
  const data = Buffer.from(str, 'hex');
1,619✔
152

153
  if (data.length !== (str.length >>> 1))
1,619!
154
    throw new Error('Invalid hex string.');
×
155

156
  return data;
1,619✔
157
};
158

159
/**
160
 * Test whether a number is a safe uint64.
161
 * @param {Number} num
162
 * @returns {Boolean}
163
 */
164

165
util.isU64 = function isU64(num) {
70✔
166
  return Number.isSafeInteger(num) && num >= 0;
98,493✔
167
};
168

169
/**
170
 * Encode a uint32.
171
 * @param {Number} num
172
 * @returns {Buffer}
173
 */
174

175
util.encodeU32 = function encodeU32(num) {
70✔
176
  assert(Number.isSafeInteger(num));
×
177
  const buf = Buffer.allocUnsafe(4);
×
178
  buf[0] = num;
×
179
  num >>>= 8;
×
180
  buf[1] = num;
×
181
  num >>>= 8;
×
182
  buf[2] = num;
×
183
  num >>>= 8;
×
184
  buf[3] = num;
×
185
  return buf;
×
186
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc