Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

redis-rank

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redis-rank - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

dist/PeriodicLeaderboard.d.ts

4

dist/index.d.ts

@@ -1,2 +0,2 @@

import Leaderboard from './Leaderboard';
export { Leaderboard };
export * from './Leaderboard';
export * from './PeriodicLeaderboard';
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
var Leaderboard_1 = __importDefault(require("./Leaderboard"));
exports.Leaderboard = Leaderboard_1.default;
__export(require("./Leaderboard"));
__export(require("./PeriodicLeaderboard"));
import { Redis, KeyType } from 'ioredis';
declare type ID = string;
declare type LeaderboardOptions = {
export declare type ID = string;
export declare type LeaderboardOptions = {
/** sorted set key */

@@ -9,12 +9,15 @@ path: KeyType;

};
declare type Entry = {
export declare type Entry = {
/** identifier */
id: ID;
/** score */
score: number;
/** ranking */
rank: number;
};
export default class Leaderboard {
export declare class Leaderboard {
/** ioredis client */
client: Redis;
private client;
/** options */
options: LeaderboardOptions;
private options;
/**

@@ -100,3 +103,10 @@ * Create a new leaderboard

total(): Promise<number>;
/**
* Key of the sorted set in Redis
*/
getPath(): KeyType;
/**
* Is this leaderboard ranking from lower to higher scores
*/
isLowToHigh(): boolean;
}
export {};

@@ -292,4 +292,16 @@ "use strict";

};
/**
* Key of the sorted set in Redis
*/
Leaderboard.prototype.getPath = function () {
return this.options.path;
};
/**
* Is this leaderboard ranking from lower to higher scores
*/
Leaderboard.prototype.isLowToHigh = function () {
return this.options.lowToHigh;
};
return Leaderboard;
}());
exports.default = Leaderboard;
exports.Leaderboard = Leaderboard;
{
"name": "redis-rank",
"version": "1.0.1",
"version": "1.1.0",
"description": "Back-end to generate and manage leaderboards using Redis. Written in TypeScript and Promise-based.",

@@ -15,3 +15,3 @@ "keywords": [

"promises",
"typescript"
"periodic"
],

@@ -31,3 +31,3 @@ "repository": "https://github.com/mlomb/redis-rank",

"prepare": "npm run test && npm run build",
"test": "jest --coverage",
"test": "jest --coverage --verbose",
"dev": "nodemon --exec \"npm run test\" --watch src --watch tests -e ts,tsx"

@@ -47,4 +47,5 @@ },

"dependencies": {
"ioredis": "^4.14.1"
"ioredis": "^4.14.1",
"moment": "^2.24.0"
}
}

@@ -13,4 +13,7 @@ # redis-rank

* Plain Leaderboards. Insert and update entries. List them in multiple ways.
* *PLANNED*: Periodic Leaderboards (daily, weekly, monthly, all-time)
* Plain Leaderboards: Insert and update entries. List them in multiple ways.
* Periodic Leaderboards: automatically create leaderboards for different time spans (*supported: minute, hourly, daily, weekly, monthly, yearly, all-time*)
* Guaranteed *at most* one trip to Redis on each function call, taking advantage of Redis's EVAL.
Planned features:
* *PLANNED*: Matrix Leaderboards (multiple dimensions)

@@ -27,9 +30,7 @@ * *PLANNED*: Archive (export) leaderboards to another database for long-term storage

Redis 2.6.12 or newer is required.
[ioredis](https://github.com/luin/ioredis) package is a dependency.
Redis 2.6.12 or newer is required. Packages [ioredis](https://www.npmjs.com/package/ioredis) and [moment](https://www.npmjs.com/package/moment) are dependencies.
## Import and connect
You must provide the [ioredis](https://github.com/luin/ioredis) connection object.
See [here](https://github.com/luin/ioredis#connect-to-redis) for more information on how to set it up.
First import/require `ioredis` and `redis-rank`.

@@ -40,10 +41,3 @@ ES5

const RedisRank = require('redis-rank');
// setup connection
let ioredis_client = new Redis({
host: "127.0.0.1",
port: 6379
});
// create a leaderboard
let lb = new RedisRank.Leaderboard(ioredis_client);
const Leaderboard = RedisRank.Leaderboard;
```

@@ -54,3 +48,9 @@ ES6

import { Leaderboard } from 'redis-rank';
```
Then create a Leaderboard.
You will have to provide a [ioredis](https://github.com/luin/ioredis) connection object.
See [here](https://github.com/luin/ioredis#connect-to-redis) for more information on how to set it up.
```javascript
// setup connection

@@ -62,3 +62,10 @@ let ioredis_client = new Redis({

// create a leaderboard
let lb = new Leaderboard(ioredis_client);
let lb = new Leaderboard(ioredis_client, {
/* optional options */
// redis key to store the sorted set
path: "lb",
// inverse leaderboard: true if lower scores are better
lowToHigh: false
});
```

@@ -100,2 +107,3 @@

lb.around("id", 10); // get 10 entries above and below the queried entry
lb.around("id", 10, true); // pass true to make sure you get 10+1+10 entries even near the borders

@@ -108,2 +116,32 @@ // remove all entries

## Periodic Leaderboard
```javascript
let plb = new PeriodicLeaderboard(redis, {
/* optional options */
// base key to store the leaderboards (plb:<time key>)
path: "plb",
// leaderboard cycle
timeFrame: 'all-time', // 'minute' | 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly' | 'all-time'
// you can also provide a custom function to evaluate the current time
now(): () => new Date(),
leaderboardOptions: { // LeaderboardOptions
lowToHigh: false,
...
}
});
```
Then every time you need it, call `getCurrent` to get the corresponding Leaderboard for the current time.
```javascript
let lb = plb.getCurrent();
// now use lb as any other Leaderboard
lb.add("pepe", 99);
lb.top(10);
// etc
```
## API

@@ -110,0 +148,0 @@ You can [peek at the documented code](src/Leaderboard.ts) for more information.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc