Make `core.has()` accept a range
MODIFY
README.md
README.md
@@ -118,6 +118,10 @@ Additional options include
}
```
+#### `const has = await core.has(start, [end])`
+
+Check if the core has all blocks between `start` and `end`.
+
#### `const updated = await core.update()`
Wait for the core to try and find a signed update to it's length.

MODIFY
index.js
index.js
@@ -666,10 +666,15 @@ module.exports = class Hypercore extends EventEmitter {
return req.promise
}
- async has (index) {
+ async has (start, end = start + 1) {
if (this.opened === false) await this.opening
- return this.core.bitfield.get(index)
+ const length = end - start
+ if (length <= 0) return false
+ if (length === 1) return this.core.bitfield.get(start)
+
+ const i = this.core.bitfield.firstUnset(start)
+ return i === -1 || i >= end
}
async get (index, opts) {

MODIFY
test/basic.js
test/basic.js
@@ -263,3 +263,37 @@ test('defaults for wait', async function (t) {
await s.close()
await core.close()
})
+
+test('has', async function (t) {
+ const core = await create()
+ await core.append(['a', 'b', 'c', 'd', 'e', 'f'])
+
+ for (let i = 0; i < core.length; i++) {
+ t.ok(await core.has(i), `has ${i}`)
+ }
+
+ await core.clear(2)
+ t.comment('2 cleared')
+
+ for (let i = 0; i < core.length; i++) {
+ if (i === 2) {
+ t.absent(await core.has(i), `does not have ${i}`)
+ } else {
+ t.ok(await core.has(i), `has ${i}`)
+ }
+ }
+})
+
+test('has range', async function (t) {
+ const core = await create()
+ await core.append(['a', 'b', 'c', 'd', 'e', 'f'])
+
+ t.ok(await core.has(0, 5), 'has 0 to 4')
+
+ await core.clear(2)
+ t.comment('2 cleared')
+
+ t.absent(await core.has(0, 5), 'does not have 0 to 4')
+ t.ok(await core.has(0, 2), 'has 0 to 1')
+ t.ok(await core.has(3, 5), 'has 3 to 4')
+})

Make `core.has()` accept a range (#330)