buffer-prefix-range

Easily define lexicographical ranges of byte strings using a prefix. Can be used to define ranges for queries in leveldb or similar databases

Usage no npm install needed!

<script type="module">
  import bufferPrefixRange from 'https://cdn.skypack.dev/buffer-prefix-range';
</script>

README

buffer-prefix-range

Easily define lexicographical ranges of byte strings using a prefix. Can be used to define ranges for queries in leveldb or similar databases.

Node.js:

Build Status

Browsers:

Selenium Test Status

Installation

npm install --save buffer-prefix-range

Usage

This library has one function which accepts a bytestring - 'prefix' - and returns an object that:

  • Contains two other bytestrings - 'start' and 'end' - where the set of all bytestrings whose prefix are equal to 'prefix' is bounded by 'start' and exclusively bounded by 'end'. In other words, if a bytestring is prefixed by 'prefix' then it will be greater or equal than 'start' and lesser than 'end'(lexicographically).

  • Has a method 'contains' that tells if a bytestring is contained within the lexicographical range of 'start' and 'end'

Its main use case is to create queries in leveldb-style dbs(databases that have arbitrary bytestrings as keys) that filter out keys that are not prefixed by a certain key prefix. For example:

> var level = require('level');
> db = level('./mydb');
> db.put('foo', ...);
> db.put('foobar', ...);
> db.put('foofoo', ...);
> db.put('fobar', ...);

Then suppose you want to query all entries that start with 'foo':

> var bytePrefixRange = require('byte-prefix-range');
> var range = bytePrefixRange('foo');
> db.createReadStream({start: range.start, end: range.end});
// will return 'foo', 'foobar' and 'foofoo'

Buffer objects or strings(which will be utf8-encoded) prefixes can be used.