Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/axelpale/redis-sorted-set
JavaScript implementation of Redis' Sorted Set
https://github.com/axelpale/redis-sorted-set
Last synced: about 2 months ago
JSON representation
JavaScript implementation of Redis' Sorted Set
- Host: GitHub
- URL: https://github.com/axelpale/redis-sorted-set
- Owner: axelpale
- License: mit
- Created: 2017-05-16T20:14:09.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-12-12T01:32:23.000Z (about 1 year ago)
- Last Synced: 2024-10-31T17:51:52.828Z (2 months ago)
- Language: JavaScript
- Homepage:
- Size: 47.9 KB
- Stars: 22
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# redis-sorted-set
[![npm version](https://img.shields.io/npm/v/redis-sorted-set?color=green)](https://www.npmjs.com/package/redis-sorted-set)
[![license](https://img.shields.io/npm/l/redis-sorted-set)](#license)A JavaScript implementation of Redis' [Sorted Sets](https://redis.io/commands/?group=sorted-set). Keeps a set of keys in order based on their score. Uses skip lists under the hood, [like Redis does](http://stackoverflow.com/a/9626334/638546).
This is a fork of the brilliant but abandoned [sorted-map](https://www.npmjs.com/package/sorted-map) package by [Eli Skeggs](https://github.com/skeggse).
![A Skip List](/doc/skip-list.png?raw=true)
Image: The skip list data structure allows search, insert, and removal in O(log(n)) time in average.
## Install
With [NPM](https://www.npmjs.com/package/redis-sorted-set):
```sh
$ npm install redis-sorted-set
```## Usage
The API follows Redis' [Sorted Set Commands](https://redis.io/commands#sorted_set) as precisely as possible, with a few additional methods such as `.has(member)`.
```js
var SortedSet = require('redis-sorted-set')
var z = new SortedSet()
// average O(log(N))
z.add('Terminator', 8.0) // => null
z.add('District 9', 8.0) // => null
z.add('Ex Machina', 0.7) // => null
z.add('Ex Machina', 7.7) // => 0.7
// alias
z.set('The Matrix', 8.7) // => null// average O(1)
z.has('Terminator') // => true
z.has('Blade Runner') // => false// average O(1)
z.score('Ex Machina') // => 7.7
z.score('Blade Runner') // => null
// alias
z.get('The Matrix') // => 8.7// average O(log(N))
z.rem('Ex Machina') // => 7.7
// average O(1)
z.rem('Ex Machina') // => null
// alias
z.del('Ex Machina') // => null// average O(log(N)+M) where M is the number of elements between min and max
z.rangeByScore(7, 8)
// => ['Ex Machina', 'District 9', 'Terminator']
z.rangeByScore(8) // [8.0-∞)
// => ['District 9', 'Terminator', 'The Matrix']
z.rangeByScore(8, null, { withScores: true })
// => [['District 9', 8.0], ['Terminator', 8.0], ['The Matrix', 8.7]]// average O(log(N)+log(M)) where M as in rangeByScore
z.count(7, 8) // => 3// average O(log(N))
z.rank('Ex Machina') // => 0
z.rank('Terminator') // => 2
z.rank('Blade Runner') // => null// average O(log(N)+M) where M as in range
z.range(0, 2)
// => ['Ex Machina', 'District 9', 'Terminator']
z.range(0, 2, { withScores: true })
// => [['Ex Machina', 7.7],
// ['District 9', 8],
// ['Terminator', 8]]
z.range(-1) // => ['The Matrix']
// almost alias
z.slice(0, 3)
// => ['Ex Machina', 'District 9', 'Terminator']// Set cardinality (number of elements)
// average O(1)
z.card() // => 4
// alias
z.length // => 4```
## Intersection
```js
var a = new SortedSet()
var b = new SortedSet()a.add('5a600e10', 16)
a.add('5a600e12', 10)
a.add('5a600e14', 9)
a.add('5a600e15', 14)
a.add('5a600e17', 20)
a.add('5a600e18', 13)
a.add('5a600e19', 15)
a.add('5a600e1a', 19)
a.add('5a600e1b', 7)
a.add('5a600e1c', 13)
a.add('5a600e1e', 10)b.add('5a600e10', 0)
b.add('5a600e11', 15)
b.add('5a600e13', 5)
b.add('5a600e14', 3)
b.add('5a600e15', 14)
b.add('5a600e17', 12)
b.add('5a600e19', 12)
b.add('5a600e1b', 16)
b.add('5a600e1c', 12)
b.add('5a600e1d', 17)
b.add('5a600e1f', 3)SortedSet.intersect(a, b)
// => ['5a600e10', '5a600e14', '5a600e17', '5a600e19', '5a600e1c', '5a600e15', '5a600e1b']SortedSet.intersect(b, a)
// => ['5a600e1b', '5a600e14', '5a600e1c', '5a600e15', '5a600e19', '5a600e10', '5a600e17']// works, but not preferred
a.intersect(b)
// => ['5a600e10', '5a600e14', '5a600e17', '5a600e19', '5a600e1c', '5a600e15', '5a600e1b']var c = new SortedSet()
c.add('5a600e10', 7)
c.add('5a600e12', 20)
c.add('5a600e13', 9)
c.add('5a600e14', 19)
c.add('5a600e16', 19)
c.add('5a600e17', 1)
c.add('5a600e18', 18)
c.add('5a600e1a', 6)
c.add('5a600e1c', 15)
c.add('5a600e1f', 4)// for best performance, the smallest set should be first
SortedSet.intersect(c, a, b)
// => ['5a600e10', '5a600e14', '5a600e17', '5a600e1c']
```## Unique
You can enable unique values with the unique option, which causes `set` to throw an error if the value provided already belongs to a different key.
```js
var z = new SortedSet({ unique: true })z.add('5a600e10', 16)
z.add('5a600e11', 6)
z.add('5a600e12', 17)
z.add('5a600e13', 11)
z.add('5a600e14', 14)
z.add('5a600e15', 19)
z.add('5a600e16', 3)
z.add('5a600e17', 12)
z.add('5a600e18', 10)// currently O(log(N)) because it needs to attempt to insert the value
z.add('5a600e19', 11) // throws
z.add('5a600e14', 14) // => 14
```## Contribute
Pull requests and [bug reports](https://github.com/axelpale/redis-sorted-set/issues) via [GitHub](https://github.com/axelpale/redis-sorted-set) are highly appreciated. Please test your contribution with the following scripts:
```sh
$ npm install
$ npm test
```## Licence
[MIT](LICENSE)