/
List.js
215 lines (193 loc) · 4.73 KB
/
List.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
var _ = require('underscore');
var BaseObject = require('./BaseObject');
var aliasMethod = require('./util').aliasMethod.bind(undefined, List);
/**
* Options:
* maxLength: List will be trimmed on push, unshift and insert
*/
function List(key, myRedis, options) {
BaseObject.apply(this, arguments);
}
module.exports = exports = List;
exports.prototype = Object.create(BaseObject.prototype);
exports.prototype.constructor = exports;
exports.prototype.maybeTrimCallback = function(callback) {
var self = this;
return function(e, res) {
if (e) {
callback(e);
} else if (self.options.maxLength) {
self.redis().ltrim(self.key, -1 * self.options.maxLength, -1, function(e, r) {
callback(e, res);
});
} else {
callback(e, res);
}
};
};
/**
* Add a member before or after pivot in the list.
*
* Redis: LINSERT
*/
exports.prototype.insert = function(where, pivot, value, callback) {
this.redis().linsert(
this.key,
where,
this.toRedis(pivot),
this.toRedis(value),
this.maybeTrimCallback(callback));
};
/**
* Add a member to the end of the list.
*
* Redis: RPUSH
*/
exports.prototype.push = function(value, callback) {
this.redis().rpush(
this.key, this.toRedis(value),
this.maybeTrimCallback(callback));
};
/**
* Add multiple members to the end of the list.
*
* Redis: RPUSH
*/
exports.prototype.pushAll = function(values, callback) {
var args = this.allToRedis(values);
args.unshift(this.key);
this.redis().rpush(
args,
this.maybeTrimCallback(callback));
};
aliasMethod('pushall', 'pushAll');
/**
* Remove a member from the end of the list.
*
* Redis: RPOP
*/
exports.prototype.pop = function(callback) {
this.redis().rpop(this.key, this.makeCallback(callback));
};
/**
* Add a member to the start of the list.
*
* Redis: LPUSH
*/
exports.prototype.unshift = function(value, callback) {
this.redis().lpush(
this.key, this.toRedis(value), this.maybeTrimCallback(callback));
};
/**
* Add multiple members to the end of the list.
*
* Redis: LPUSH
*/
exports.prototype.unshiftAll = function(values, callback) {
var args = this.allToRedis(values);
args.unshift(this.key);
this.redis().lpush(
args, this.maybeTrimCallback(callback));
};
aliasMethod('unshiftall', 'unshiftAll');
/**
* Remove a member from the start of the list.
*
* Redis: LPOP
*/
exports.prototype.shift = function(callback) {
this.redis().lpop(this.key, this.makeCallback(callback));
};
/**
* Return all values in the list.
*
* Redis: LRANGE(0, -1)
*/
exports.prototype.values = function(callback) {
this.range(0, callback);
};
/**
* Return a range of values from start index to end index. Javascript slices are
* end-exclusive, so .slice(0, -1) will return all but the last entry. If end is
* unspecified, reads all the way to the end.
*
* Redis: LRANGE
*/
exports.prototype.slice = function baseRange(index, end, callback) {
if (!_.isNumber(end)) {
callback = end;
end = undefined;
}
var args = [this.key, index, _.isNumber(end) ? end - 1 : -1];
this.redis().lrange(args, this.makeArrayCallback(callback));
};
aliasMethod('range', 'slice');
/**
* Delete the element(s) from the list that match name. If count is specified,
* only the first-N (if positive) or last-N (if negative) will be removed. Use
* .del to completely delete the entire key.
*
* Redis: LREM
*/
exports.prototype.delete = function(value, count, callback) {
if (_.isFunction(count)) {
callback = count;
count = 0;
}
this.redis().lrem(this.key, count, this.toRedis(value), callback);
};
/**
* Sets the list element at index to value.
*
* Redis: LSET
*/
exports.prototype.setAt = function(index, value, callback) {
this.redis().lset(
this.key, index, this.toRedis(value), this.makeCallback(callback));
};
aliasMethod('setat', 'setAt');
/**
* Return the value at the given index.
*
* Redis: LINDEX
*/
exports.prototype.at = function(index, callback) {
this.redis().lindex(this.key, index, this.makeCallback(callback));
};
/**
* Return the first value in the list
*
* Redis: LINDEX(0)
*/
exports.prototype.first = function(callback) {
this.at(0, callback);
};
/**
* Return the last value in the list
*
* Redis: LINDEX(-1)
*/
exports.prototype.last = function(callback) {
this.at(-1, callback);
};
/**
* Return the length of the list.
*
* Redis: LLEN
*/
exports.prototype.length = function(callback) {
this.redis().llen(this.key, callback);
};
aliasMethod('size', 'length');
/**
* Returns true if the list has no values.
*
* Redis: LLEN == 0
*/
exports.prototype.empty = function(callback) {
this.redis().scard(this.key, function(e, count) {
if (e) return callback(e);
callback(e, count === 0);
});
};
aliasMethod('isEmpty', 'empty');