https://github.com/vweevers/lexicographic-integer-encoding
Lexicographically ordered integers for level(up).
https://github.com/vweevers/lexicographic-integer-encoding
Last synced: 12 months ago
JSON representation
Lexicographically ordered integers for level(up).
- Host: GitHub
- URL: https://github.com/vweevers/lexicographic-integer-encoding
- Owner: vweevers
- License: mit
- Created: 2017-10-23T20:47:12.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-12-11T17:48:17.000Z (over 5 years ago)
- Last Synced: 2025-06-19T06:15:16.721Z (12 months ago)
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 5
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# lexicographic-integer-encoding
**Lexicographically ordered integers for `level(up)`. Wraps [`lexicographic-integer`].**
[](https://www.npmjs.org/package/lexicographic-integer-encoding)
[](https://www.npmjs.org/package/lexicographic-integer-encoding) [](http://travis-ci.org/vweevers/lexicographic-integer-encoding) [](https://ci.appveyor.com/project/vweevers/lexicographic-integer-encoding) [](https://standardjs.com) [](https://david-dm.org/vweevers/lexicographic-integer-encoding)
## usage with [`level`]
```js
const level = require('level')
const lexint = require('lexicographic-integer-encoding')('hex')
const db = level('./db', { keyEncoding: lexint })
db.put(2, 'example', (err) => {
db.put(10, 'example', (err) => {
// Without our encoding, the keys would sort as 10, 2.
db.createKeyStream().on('data', console.log) // 2, 10
})
})
```
## usage with [`levelup`]
```js
const levelup = require('levelup')
const encode = require('encoding-down')
const leveldown = require('leveldown')
const lexint = require('lexicographic-integer-encoding')('hex')
const db = levelup(encode(leveldown('./db'), { keyEncoding: lexint }))
```
## api
### `lexint = require('lexicographic-integer-encoding')(encoding, [options])`
- `encoding` (string, required): `'hex'` or `'buffer'`
- `options.strict` (boolean): opt-in to type-checking input. If true, encode will throw:
- A `TypeError` if input is not a number or if `NaN`
- A `RangeError` if input is < 0 or > `Number.MAX_SAFE_INTEGER`
Returns a [`level-codec` compliant encoding](https://github.com/Level/codec#encoding-format) object.
## see also
- [`lexicographic-integer`]: main encoding logic
- [`unique-lexicographic-integer`]: `lexicographic-integer` plus a suffix if input is the same as the last call;
- [`monotonic-lexicographic-timestamp`]: `unique-lexicographic-integer` with `Date.now()` to get a monotonically increasing timestamp with lexicographic order.
## install
With [npm](https://npmjs.org) do:
```
npm install lexicographic-integer-encoding
```
## license
[MIT](http://opensource.org/licenses/MIT) © Vincent Weevers
[`lexicographic-integer`]: https://github.com/substack/lexicographic-integer
[`level`]: https://github.com/Level/level
[`levelup`]: https://github.com/Level/levelup
[`unique-lexicographic-integer`]: https://github.com/vweevers/unique-lexicographic-integer
[`monotonic-lexicographic-timestamp`]: https://github.com/vweevers/monotonic-lexicographic-timestamp