-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzrevrangebyscore.js
More file actions
31 lines (23 loc) · 906 Bytes
/
zrevrangebyscore.js
File metadata and controls
31 lines (23 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var redis = require('redis');
var constants = require('./constants.js');
exports.handler = (event, context) => {
var rCli = redis.createClient(constants.redisCred);
rCli.on('error', (err) => console.log('Error: '+err));
rCli.on('connect', () => {
rCli.zrevrangebyscore([event.set, '+inf', '-inf', 'WITHSCORES'], (err, reply) => {
//reduce method to have the next value of array as the value of current key
var output = reply.reduce((result, item, index) => {
if((index +1) <= reply.length / 2) {
//0*2 = 0 then 0*2 +1 = 1
//1*2 = 2 then 1*2 +1 = 3
//2*2 = 4 then 2*2+1 = 5
result[reply[index*2]] = reply[(index*2)+1];
}
return result;
}, {}); //to store result in an object (also the initial value)
//will return a json style object instead of simple array
context.succeed(output);
rCli.quit();
});
});
}