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

handshake-org / hsd / 15727801848

18 Jun 2025 08:26AM UTC coverage: 71.588% (+0.3%) from 71.308%
15727801848

push

github

nodech
Merge PR #928 from 'nodech/wallet-coinselection'

8199 of 13307 branches covered (61.61%)

Branch coverage included in aggregate %.

550 of 574 new or added lines in 12 files covered. (95.82%)

4 existing lines in 3 files now uncovered.

26119 of 34631 relevant lines covered (75.42%)

34022.07 hits per line

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

60.34
/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) {
68✔
19
  let start = 0;
27,331✔
20
  let end = items.length - 1;
27,331✔
21

22
  while (start <= end) {
27,331✔
23
    const pos = (start + end) >>> 1;
134,042✔
24
    const cmp = compare(items[pos], key);
134,042✔
25

26
    if (cmp === 0)
134,042✔
27
      return pos;
259✔
28

29
    if (cmp < 0)
133,783✔
30
      start = pos + 1;
130,248✔
31
    else
32
      end = pos - 1;
3,535✔
33
  }
34

35
  if (!insert)
27,072!
36
    return -1;
×
37

38
  return start;
27,072✔
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) {
68✔
51
  const i = exports.search(items, item, compare, true);
436✔
52

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

58
  if (i === 0)
436✔
59
    items.unshift(item);
355✔
60
  else if (i === items.length)
81✔
61
    items.push(item);
40✔
62
  else
63
    items.splice(i, 0, item);
41✔
64

65
  return i;
436✔
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) {
68✔
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