🚀 Launch Week Day 4:Introducing the Alert Details Page: A Better Way to Explore Alerts.Learn More →
Socket
Book a DemoInstallSign in
Socket

hisect

Package Overview
Dependencies
Maintainers
2
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hisect

Binary search for Hypercore

latest
npmnpm
Version
2.0.0
Version published
Maintainers
2
Created
Source

hisect

Binary search on a Hypercore.

Pass a comparator. Gets you the index of the first block that matches, or -1 if not found.

npm install hisect

Usage

import hisect from 'hisect'
import Hypercore from 'hypercore'
import b4a from 'b4a'

const core = new Hypercore('./core')
await core.ready()

for (let i = 0; i <= 10000; i++) {
  await core.append(b4a.from(`${i}`))
}

const since = 9999
const index = await hisect(core, since, (since, block) => {
  const value = Number(b4a.toString(block))
  if (value < since) return -1
  if (value > since) return 1
  return 0
})

console.log('Start index:', index)

Once you have the index, you can stream from it or do whatever.

const stream = core.createReadStream({ start: index })
for await (const data of stream) {
  console.log(b4a.toString(data))
}

API

const index = await hisect(core, target, compare)

Does a binary search over the blocks in core. compare is a sync function that receives the block and should return:

  • < 0 if the target is after this block

  • > 0 if the target is before this block

  • 0 if it's a match

Returns the index of the first matching block. If there's no match, returns -1.

hisect.lte

hisect.lt

hisect.gte

hisect.gt

FAQs

Package last updated on 13 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts