{"name":"index-encoder","version":"3.4.0","author":{"url":"@mafintosh","name":"Mathias Buus"},"license":"Apache-2.0","_id":"index-encoder@3.4.0","maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"}],"homepage":"https://github.com/holepunchto/index-encoder","bugs":{"url":"https://github.com/holepunchto/index-encoder/issues"},"dist":{"shasum":"cfb8974d472b3bc62a2bfd6a39e0a407c575adc7","tarball":"http://registry-npm.ywwl.com/index-encoder/download/index-encoder-3.4.0.tgz","fileCount":7,"integrity":"sha512-k3+ENtseFYI9ZPOIZzVH8LlONUvXAcd4jvCPo+Nob/T/2t5R5Rfh8XiFXBG++gHHuVby7HBDp/3YbyEmE481cg==","signatures":[{"sig":"MEQCIG7OHZytiUKRBRzHOnpnLAAXgJ/pqviU57Bf2MHfnK6lAiBPnAlvFLA3va4zevXPdcVy0yZ73bypfuecl4gtwzd6og==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":27151,"size":8328,"noattachment":false,"key":"/index-encoder/-/index-encoder-3.4.0.tgz"},"main":"index.js","gitHead":"af50f7c1920fdef58493b12e0c07b8044c675fd1","scripts":{"test":"standard && brittle test.js"},"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"repository":{"url":"git+https://github.com/holepunchto/index-encoder.git","type":"git"},"_npmVersion":"10.9.0","description":"Encode multiple values into sorted keys","directories":{},"_nodeVersion":"22.12.0","dependencies":{"b4a":"^1.6.4"},"_hasShrinkwrap":false,"devDependencies":{"brittle":"^3.2.1","hyperbee":"^2.10.5","standard":"^17.0.0","hypercore":"^10.9.2","random-access-memory":"^6.2.0"},"_npmOperationalInternal":{"tmp":"tmp/index-encoder_3.4.0_1745227736278_0.5398116477450818","host":"s3://npm-registry-packages-npm-production"},"readme":"# index-encoder\n\nEncode multiple values into sorted keys\n\n```\nnpm install index-encoder\n```\n\n## Usage\n\n```js\nconst IndexEncoder = require('index-encoder')\n\nconst idx = new IndexEncoder([IndexEncoder.STRING, IndexEncoder.UINT])\n\nconst buf = idx.encode(['foo', 42])\nconsole.log(idx.decode(buf)) // ['foo', 42]\n\n// pass any compact encoding name to map it to the relevant encoding\nconst uintType = IndexEncoder.lookup('uint')\nconst bufferType = IndexEncoder.lookup('buffer')\nconst alsoBufferType = IndexEncoder.lookup('fixed32')\n```\n\n## API\n\n#### `const idx = new IndexEncoder(encodings, { prefix = -1 })`\n\nCreate 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`.\n\n#### `const buf = idx.encode(keys)`\n\nEncode 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`](https://github.com/holepunchto/hyperbee), this can be used to search for only a subset of the total keys.\n\n#### `const range = idx.encodeRange(query)`\n\nEncodes the `query` object values into their buffer equivalent values. Useful for creating range queries for [`hyperbee`](https://github.com/holepunchto/hyperbee?tab=readme-ov-file#const-stream--dbcreatereadstreamrange-options) based on the index encoder.\n\nThe `query` specifies the range you want to read:\n\n```\n{\n  gt,  // Return values only > than this\n  gte, // Return values >= than this\n  lt,  // Return values < than this\n  lte: // Return values <= than this\n}\n```\n\nReturned query will be bounded by the `prefix` if defined and the query is unbounded. This ensures ranges only query ranges with the same `prefix`.\n\n#### `const values = idx.decode(buffer)`\n\nDecode 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`:\n\n```\nconst idx = new IndexEncoder([\n  IndexEncoder.STRING,\n  IndexEncoder.UINT,\n  IndexEncoder.STRING\n])\n\nconst buf = idx.encode(['foo'])\nconsole.log(idx.decode(buf)) // ['foo', 0, null]\n```\n\n## Encoding Types\n\nThe following are the types for `encodings` are supported provided as static properties on the `IndexEncoder` class:\n\n- `IndexEncoder.BUFFER` : Encodes a buffer.\n- `IndexEncoder.STRING` : Encodes a string.\n- `IndexEncoder.UINT` : Encodes an unsigned integer.\n- `IndexEncoder.INT` : Encodes an signed integer.\n- `IndexEncoder.DATE` : Encodes a date.\n- `IndexEncoder.BOOL` : Encodes a boolean.\n\n#### `const enc = IndexEncoder.lookup(c)`\n\nReturns the encoding type for a given compact encoding name (`c`). If `c` is not supported, it will throw an `Unknown type` error.\n\n## License\n\nApache 2.0\n","publish_time":1745227736454,"_cnpm_publish_time":1745227736454,"dist-tags":{"latest":"3.5.0"}}