https://github.com/ealmansi/elen
ELEN - Efficient Lexicographic Encoding of Numbers
https://github.com/ealmansi/elen
efficient elen encoding javascript lexicographic numbers peter-seymour
Last synced: 9 months ago
JSON representation
ELEN - Efficient Lexicographic Encoding of Numbers
- Host: GitHub
- URL: https://github.com/ealmansi/elen
- Owner: ealmansi
- License: mit
- Created: 2017-12-15T22:18:09.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T18:31:15.000Z (about 3 years ago)
- Last Synced: 2025-06-01T06:31:55.212Z (10 months ago)
- Topics: efficient, elen, encoding, javascript, lexicographic, numbers, peter-seymour
- Language: JavaScript
- Homepage:
- Size: 1.93 MB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 18
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ELEN - Efficient Lexicographic Encoding of Numbers
[](https://travis-ci.com/ealmansi/elen)
Based on the [paper](https://github.com/ealmansi/elen/blob/master/resources/elen.pdf) by Peter Seymour.
## Installation
`$ npm install --save elen`
## Why would I want to use ELEN?
ELEN provides a way of textually representing numbers such that their natural order is preserved as a lexicographical order (i.e. alphabetical order) of their representations.
Imagine you need to represent numbers textually. A simple solution would be to simply call `num.toString()`. E.g.:
```
7..toString() // '7'
11..toString() // '11'
```
For many applications, this will suffice. However, notice that the natural order of the input numbers does not match the lexicographical order of the generated strings. I.e.:
```
[7, 11].sort((a, b) => a - b) // [ 7, 11 ]
['7', '11'].sort() // [ '11', '7' ]
```
That's where ELEN comes in. ELEN provides a way of textually representing numbers such that the following property is fulfilled:
```
assert.deepEqual(
nums.map(elen.encode).sort().map(elen.decode),
nums.sort((a, b) => a - b)
)
```
## Usage
### In Node.js
```
const elen = require('elen')
const encoded = [
elen.encode(0),
elen.encode(42),
elen.encode(1),
elen.encode(-10),
elen.encode(5e100),
elen.encode(-Infinity),
]
encoded.sort()
elen.decode(encoded[4]) // 42
```
### Browser
#### Script Tag
```
...
...
```
## Documentation
### Generate and Browse Locally
```
$ npm run docs
```
### Online
Browse automatically generated jsdocs [online](https://cdn.rawgit.com/ealmansi/elen/master/docs/global.html#encode).