redis-rank
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -10,2 +10,3 @@ import { Redis } from 'ioredis'; | ||
* a fill border policy | ||
* * `zrangescore` & `zrevrangescore`: return the entries between scores | ||
* * `zmatrixfind`, `zmatrixrange` and `zmatrixaround`: equivalent to their | ||
@@ -12,0 +13,0 @@ * non-matrix versions but using a matrix of leaderboards |
@@ -24,2 +24,10 @@ "use strict"; | ||
var zkeeptop = function (dir) { return "\nlocal c = redis.call('zcard', KEYS[1]);\nlocal n = tonumber(ARGV[1])\nlocal dif = c - n\nif dif > 0 then\n " + (dir === 'asc' ? "\n -- low to high\n redis.call('zremrangebyrank', KEYS[1], -1, - dif)\n " : "\n -- high to low\n redis.call('zremrangebyrank', KEYS[1], 0, dif - 1)\n ") + "\nend\n"; }; | ||
/** | ||
* `KEYS[1]`: leaderboard key | ||
* `ARGV[1]`: min score | ||
* `ARGV[2]`: max score | ||
* | ||
* Returns [ lowest_rank, [[id, score], ...] ] | ||
*/ | ||
var zrangescore = function (dir) { return "\nlocal c = redis.call(\n 'z" + (dir === 'desc' ? 'rev' : '') + "rangebyscore',\n KEYS[1],\n " + (dir === 'desc' ? 'ARGV[2]' : 'ARGV[1]') + ",\n " + (dir === 'desc' ? 'ARGV[1]' : 'ARGV[2]') + ",\n 'WITHSCORES'\n);\nif #c > 0 then\n local r = redis.call('z" + (dir === 'desc' ? 'rev' : '') + "rank', KEYS[1], c[1]);\n return { r, c }\nelse\n return { -1, {} }\nend\n"; }; | ||
var aroundRange = "\nlocal function aroundRange(path, id, distance, fill_borders, sort_dir)\n local r = redis.call((sort_dir == 'low-to-high') and 'zrank' or 'zrevrank', path, id) -- entry rank\n\n if r == false or r == nil then\n -- entry does not exist\n return { -1, -1, -1, -1 }\n end\n \n local c = redis.call('zcard', path) -- lb size\n local l = math.max(0, r - distance) -- lower bound rank\n local h = 0 -- upper bound rank\n\n if fill_borders == 'true' then\n h = l + 2 * distance\n if h >= c then \n h = math.min(c, r + distance)\n l = math.max(0, h - 2 * distance - 1)\n end\n else\n h = math.min(c, r + distance)\n end\n\n -- lower bound, upper bound, lb card, query rank\n return { l, h, c, r };\nend\n"; | ||
@@ -75,2 +83,3 @@ /** | ||
* a fill border policy | ||
* * `zrangescore` & `zrevrangescore`: return the entries between scores | ||
* * `zmatrixfind`, `zmatrixrange` and `zmatrixaround`: equivalent to their | ||
@@ -93,2 +102,4 @@ * non-matrix versions but using a matrix of leaderboards | ||
client.defineCommand("zaround", { numberOfKeys: 1, lua: zaround }); | ||
client.defineCommand("zrangescore", { numberOfKeys: 1, lua: zrangescore('asc') }); | ||
client.defineCommand("zrevrangescore", { numberOfKeys: 1, lua: zrangescore('desc') }); | ||
client.defineCommand("zmatrixfind", { lua: zmatrixfind }); | ||
@@ -95,0 +106,0 @@ client.defineCommand("zmatrixrange", { lua: zmatrixrange }); |
@@ -10,2 +10,4 @@ "use strict"; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
@@ -12,0 +14,0 @@ function __() { this.constructor = d; } |
@@ -182,2 +182,12 @@ /// <reference types="node" /> | ||
/** | ||
* Retrieve entries between scores | ||
* | ||
* Complexity: `O(log(N)+M)` where N is the number of entries in the | ||
* leaderboard and M the number of entries returned | ||
* | ||
* @param min min score to query (inclusive) | ||
* @param max max score to query (inclusive) | ||
*/ | ||
listByScore(min: Score, max: Score): Promise<Entry[]>; | ||
/** | ||
* Retrieve the top entries | ||
@@ -184,0 +194,0 @@ * |
@@ -338,2 +338,37 @@ "use strict"; | ||
/** | ||
* Retrieve entries between scores | ||
* | ||
* Complexity: `O(log(N)+M)` where N is the number of entries in the | ||
* leaderboard and M the number of entries returned | ||
* | ||
* @param min min score to query (inclusive) | ||
* @param max max score to query (inclusive) | ||
*/ | ||
Leaderboard.prototype.listByScore = function (min, max) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result, entries, rank, i; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, (this.options.sortPolicy === 'high-to-low' ? | ||
// @ts-ignore | ||
this.client.zrevrangescore(this.key, min, max) : | ||
// @ts-ignore | ||
this.client.zrangescore(this.key, min, max))]; | ||
case 1: | ||
result = _a.sent(); | ||
entries = []; | ||
rank = 0; | ||
for (i = 0; i < result[1].length; i += 2) { | ||
entries.push({ | ||
id: result[1][i], | ||
rank: 1 + result[0] + rank++, | ||
score: parseFloat(result[1][i + 1]) | ||
}); | ||
} | ||
return [2 /*return*/, entries]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Retrieve the top entries | ||
@@ -340,0 +375,0 @@ * |
@@ -38,8 +38,6 @@ "use strict"; | ||
}; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from) { | ||
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) | ||
to[j] = from[i]; | ||
return to; | ||
}; | ||
@@ -390,3 +388,3 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
filter.features.push(featureToSort); | ||
return this.execMatrix.apply(this, __spreadArrays([fnName, filter, sortLb.redisKey], args)); | ||
return this.execMatrix.apply(this, __spreadArray([fnName, filter, sortLb.redisKey], args)); | ||
}; | ||
@@ -415,3 +413,3 @@ /** | ||
return [2 /*return*/, []]; | ||
return [4 /*yield*/, (_b = this.client)[fnName].apply(_b, __spreadArrays([queryInfo.keys.length, | ||
return [4 /*yield*/, (_b = this.client)[fnName].apply(_b, __spreadArray([queryInfo.keys.length, | ||
queryInfo.keys, | ||
@@ -418,0 +416,0 @@ queryInfo.sortPolicies, |
{ | ||
"name": "redis-rank", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Manage real-time leaderboards using Redis", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -42,3 +42,3 @@ | ||
const Redis = require('ioredis'); | ||
const { Leaderboard } = require('redis-rank'); | ||
const { Leaderboard, PeriodicLeaderboard, LeaderboardMatrix } = require('redis-rank'); | ||
``` | ||
@@ -48,3 +48,3 @@ ES6 | ||
import { Redis } from 'ioredis'; | ||
import { Leaderboard } from 'redis-rank'; | ||
import { Leaderboard, PeriodicLeaderboard, LeaderboardMatrix } from 'redis-rank'; | ||
``` | ||
@@ -56,3 +56,3 @@ | ||
```javascript | ||
let client = new Redis({ | ||
const client = new Redis({ | ||
host: "127.0.0.1", | ||
@@ -59,0 +59,0 @@ port: 6379 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
95106
2010
0