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

handshake-org / hsd / 15042451478

15 May 2025 10:16AM UTC coverage: 19.792% (-51.5%) from 71.269%
15042451478

Pull #928

github

web-flow
Merge fa1d30da4 into 5f11d622b
Pull Request #928: Wallet coinselection

1570 of 13236 branches covered (11.86%)

Branch coverage included in aggregate %.

217 of 388 new or added lines in 6 files covered. (55.93%)

17866 existing lines in 127 files now uncovered.

7855 of 34384 relevant lines covered (22.84%)

129.11 hits per line

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

27.59
/lib/utils/binary.js
1
/*!
2
 * binary.js - binary search 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
/**
10
 * Perform a binary search on a sorted array.
11
 * @param {Array} items
12
 * @param {Object} key
13
 * @param {Function} compare
14
 * @param {Boolean} [insert=false]
15
 * @returns {Number} Index.
16
 */
17

18
exports.search = function search(items, key, compare, insert) {
1✔
19
  let start = 0;
2,750✔
20
  let end = items.length - 1;
2,750✔
21

22
  while (start <= end) {
2,750✔
23
    const pos = (start + end) >>> 1;
13,600✔
24
    const cmp = compare(items[pos], key);
13,600✔
25

26
    if (cmp === 0)
13,600!
UNCOV
27
      return pos;
×
28

29
    if (cmp < 0)
13,600!
30
      start = pos + 1;
13,600✔
31
    else
UNCOV
32
      end = pos - 1;
×
33
  }
34

35
  if (!insert)
2,750!
36
    return -1;
×
37

38
  return start;
2,750✔
39
};
40

41
/**
42
 * Perform a binary insert on a sorted array.
43
 * @param {Array} items
44
 * @param {Object} item
45
 * @param {Function} compare
46
 * @param {Boolean} [uniq=false]
47
 * @returns {Number} index
48
 */
49

50
exports.insert = function insert(items, item, compare, uniq) {
1✔
UNCOV
51
  const i = exports.search(items, item, compare, true);
×
52

UNCOV
53
  if (uniq && i < items.length) {
×
54
    if (compare(items[i], item) === 0)
×
55
      return -1;
×
56
  }
57

UNCOV
58
  if (i === 0)
×
UNCOV
59
    items.unshift(item);
×
UNCOV
60
  else if (i === items.length)
×
UNCOV
61
    items.push(item);
×
62
  else
UNCOV
63
    items.splice(i, 0, item);
×
64

UNCOV
65
  return i;
×
66
};
67

68
/**
69
 * Perform a binary removal on a sorted array.
70
 * @param {Array} items
71
 * @param {Object} item
72
 * @param {Function} compare
73
 * @returns {Boolean}
74
 */
75

76
exports.remove = function remove(items, item, compare) {
1✔
77
  const i = exports.search(items, item, compare, false);
×
78

79
  if (i === -1)
×
80
    return false;
×
81

82
  splice(items, i);
×
83

84
  return true;
×
85
};
86

87
/*
88
 * Helpers
89
 */
90

91
/**
92
 * @param {Array} list
93
 * @param {Number} i
94
 */
95

96
function splice(list, i) {
97
  if (i === 0) {
×
98
    list.shift();
×
99
    return;
×
100
  }
101

102
  let k = i + 1;
×
103

104
  while (k < list.length)
×
105
    list[i++] = list[k++];
×
106

107
  list.pop();
×
108
}
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

© 2026 Coveralls, Inc