scorerank
v0.2.1
Published
Simple but powerfull score ranking with redis
Downloads
5
Maintainers
Readme
scorerank
Simple score ranking with redis.
Installation
$ npm install scorerank
Usage
const scorerank = require('scorerank')
const url = 'redis://127.0.0.1:6379'
const options = { prefix: 'scores' }
const score = scorerank(url, options)
// Add scores
score.add('user1', 20)
score.add('user2', 21)
score.add('user3', 22)
// Get top 10 ranking
const tops = await score.top([0, 10])
console.log(tops) // => ['user3', 'user2', 'user1']
// Get user specific ranking
const rank = await score.rank('user2')
console.log(rank) // => 1
// Get user specific score
const sc = await score.score('user2')
console.log(sc) // => 21
// Remove user specific score
const sc = await score.remove('user2')
API
scorerank([options])
Get an instance of scorerank. options
is an object that besides prefix
it can have any option property supported by ioredis
.
const options = {
prefix: 'scores',
port: 6379,
host: '127.0.0.1',
password: ''
}
const score = scorerank(options)
scorerank.add(id, score, [ns])
Add scores by id.
scorerank.add('5d48a5e9b06120387b7587dc', 7)
scorerank.score(id, [ns])
Get score by user id.
scorerank.score('5d48a5e9b06120387b7587dc')
scorerank.rank(id, [ns])
Get ranking of an specific id.
scorerank.rank('5d48a5e9b06120387b7587dc')
scorerank.top([range, [ns]])
Get top 100 ranking.
scorerank.top()
Or get specific range ranking.
// get top 10 ranking
scorerank.top([0, 10])
// get ranking between the range of 10 to 20
scorerank.top([10, 20])
scorerank.remove(id, [ns])
Remove score by id.
scorerank.remove('5d48a5e9b06120387b7587dc')
Run tests
$ yarn test
License
(The MIT License)
Copyright (c) 2019 Jonathan Brumley <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.