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

handshake-org / hsd / 12515507044

27 Dec 2024 11:26AM UTC coverage: 71.242% (-0.02%) from 71.265%
12515507044

push

github

nodech
Merge PR #914 from 'nodech/add-mid-checkpoint'

8053 of 13154 branches covered (61.22%)

Branch coverage included in aggregate %.

25712 of 34241 relevant lines covered (75.09%)

34514.12 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;
26,768✔
20
  let end = items.length - 1;
26,768✔
21

22
  while (start <= end) {
26,768✔
23
    const pos = (start + end) >>> 1;
131,257✔
24
    const cmp = compare(items[pos], key);
131,257✔
25

26
    if (cmp === 0)
131,257✔
27
      return pos;
256✔
28

29
    if (cmp < 0)
131,001✔
30
      start = pos + 1;
127,497✔
31
    else
32
      end = pos - 1;
3,504✔
33
  }
34

35
  if (!insert)
26,512!
36
    return -1;
×
37

38
  return start;
26,512✔
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!
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

© 2025 Coveralls, Inc