npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

index-encoder

v3.4.0

Published

Encode multiple values into sorted keys

Readme

index-encoder

Encode multiple values into sorted keys

npm install index-encoder

Usage

const IndexEncoder = require('index-encoder')

const idx = new IndexEncoder([
  IndexEncoder.STRING,
  IndexEncoder.UINT
])

const buf = idx.encode(['foo', 42])
console.log(idx.decode(buf)) // ['foo', 42]

// pass any compact encoding name to map it to the relevant encoding
const uintType = IndexEncoder.lookup('uint')
const bufferType = IndexEncoder.lookup('buffer')
const alsoBufferType = IndexEncoder.lookup('fixed32')

API

const idx = new IndexEncoder(encodings, { prefix = -1 })

Create an index encoder using the array of encodings. The index encoder will produce a buffer encoding in the same order as encodings. A prefix can be defined to differentiate encoded buffers with the same encodings.

const buf = idx.encode(keys)

Encode the provided keys as a buffer. keys must be in the same order as the encodings when constructing the index encoder. keys can have less elements than the defined encodings to create a prefix buffer. When used with hyperbee, this can be used to search for only a subset of the total keys.

const range = idx.encodeRange(query)

Encodes the query object values into their buffer equivalent values. Useful for creating range queries for hyperbee based on the index encoder.

The query specifies the range you want to read:

{
  gt,  // Return values only > than this
  gte, // Return values >= than this
  lt,  // Return values < than this
  lte: // Return values <= than this
}

Returned query will be bounded by the prefix if defined and the query is unbounded. This ensures ranges only query ranges with the same prefix.

const values = idx.decode(buffer)

Decode the buffer into the original values. values will be in the same order as the defined encodings. Buffers encoded with a subset of keys will return the values encoded with remaining values being 0 filled if encoded with UNIT else null:

const idx = new IndexEncoder([
  IndexEncoder.STRING,
  IndexEncoder.UINT,
  IndexEncoder.STRING
])

const buf = idx.encode(['foo'])
console.log(idx.decode(buf)) // ['foo', 0, null]

Encoding Types

The following are the types for encodings are supported provided as static properties on the IndexEncoder class:

  • IndexEncoder.BUFFER : Encodes a buffer.
  • IndexEncoder.STRING : Encodes a string.
  • IndexEncoder.UINT : Encodes an unsigned integer.

const enc = IndexEncoder.lookup(c)

Returns the encoding type for a given compact encoding name (c). If c is not supported, it will throw an Unknown type error.

License

Apache 2.0