Permalink
Browse files

Made some callbacks optional (mainly for add/remove ops)

  • Loading branch information...
1 parent 2c25fb0 commit 4b4802908c1294fe88b5d5686fb14e3be28ae024 @chriso committed May 12, 2011
View
1 lib/Structure.js
@@ -163,6 +163,7 @@ Structure.prototype.getKeys = function (structures, which) {
*/
Structure.prototype.namespaceKey = function (key) {
+ key = key || '';
if (this.namespace.length) {
key = this.namespace + ':' + key;
}
View
16 lib/advanced_structures/CappedList.js
@@ -44,12 +44,13 @@ CappedList.prototype.init = function (length) {
*
* @param {int} pivot
* @param {string} value
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
CappedList.prototype.insertBefore = function (pivot, value, callback) {
+ callback = callback || function () {};
var multi = this.client.multi()
multi.linsert(this.key, 'BEFORE', pivot, value);
multi.ltrim(this.key, -1 * this.len, -1);
@@ -62,12 +63,13 @@ CappedList.prototype.insertBefore = function (pivot, value, callback) {
*
* @param {int} pivot
* @param {string} value
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
CappedList.prototype.insertAfter = function (pivot, value, callback) {
+ callback = callback || function () {};
var multi = this.client.multi()
multi.linsert(this.key, 'AFTER', pivot, value);
multi.ltrim(this.key, -1 * this.len, -1);
@@ -78,13 +80,14 @@ CappedList.prototype.insertAfter = function (pivot, value, callback) {
/**
* Add one or more elements to the start of the list.
*
- * @param {string|array} values
- * @param {Function} callback
+ * @param {string|array} value(s)
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
CappedList.prototype.unshift = CappedList.prototype.lpush = function (values, callback) {
+ callback = callback || function () {};
var multi = this.client.multi();
if (Array.isArray(values)) {
var key = this.key;
@@ -102,13 +105,14 @@ CappedList.prototype.unshift = CappedList.prototype.lpush = function (values, ca
/**
* Add one or more elements to the end of the list.
*
- * @param {string|array} values
- * @param {Function} callback
+ * @param {string|array} value(s)
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
CappedList.prototype.push = CappedList.prototype.add = function (values, callback) {
+ callback = callback || function () {};
var multi = this.client.multi();
if (Array.isArray(values)) {
var key = this.key;
View
10 lib/advanced_structures/DensitySet.js
@@ -20,10 +20,10 @@ var SortedSet = require('../base_structures/SortedSet').SortedSet;
* `redback.createDensitySet(key);`
*
* Reference:
- * http://redis.io/topics/data-types#sorted-sets
+ * http://redis.io/topics/data-types#sorted-sets
*
* Redis Structure:
- * `(namespace:)key = zset(count => element)`
+ * `(namespace:)key = zset(count => element)`
*/
var DensitySet = exports.DensitySet = SortedSet.prototype.extend();
@@ -32,12 +32,13 @@ var DensitySet = exports.DensitySet = SortedSet.prototype.extend();
* Add one or more elements to the set.
*
* @param {string|Array} element(s)
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
DensitySet.prototype.add = function (element, callback) {
+ callback = callback || function () {};
if (Array.isArray(element)) {
return this.addAll(element, callback);
}
@@ -49,12 +50,13 @@ DensitySet.prototype.add = function (element, callback) {
* Remove one or more elements from the set.
*
* @param {string|Array} element(s)
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
DensitySet.prototype.remove = function (element, callback) {
+ callback = callback || function () {};
if (Array.isArray(element)) {
return this.removeAll(element, callback);
}
View
6 lib/advanced_structures/KeyPair.js
@@ -225,12 +225,13 @@ KeyPair.prototype.idExists = function (id, callback) {
* Deletes a unique value and its associated id.
*
* @param {string} value
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
KeyPair.prototype.delete = function (value, callback) {
+ callback = callback || function () {};
var self = this, value = this.hashValue(value);
this.client.hget(this.idkey, value, function (err, id) {
if (err || value == null) return callback(err);
@@ -243,12 +244,13 @@ KeyPair.prototype.delete = function (value, callback) {
* Deletes an id and its associated unique value.
*
* @param {int} id
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
KeyPair.prototype.deleteById = function (id, callback) {
+ callback = callback || function () {};
var self = this;
this.client.hget(this.key, id, function (err, value) {
if (err || value == null) return callback(err);
View
2 lib/advanced_structures/SocialGraph.js
@@ -34,7 +34,7 @@ var SocialGraph = exports.SocialGraph = Structure.new();
*/
SocialGraph.prototype.init = function (prefix) {
- this.key_prefix = this.namespace.length ? this.namespace + ':' : '';
+ this.key_prefix = this.namespaceKey();
if (prefix) {
this.key_prefix += prefix + ':';
}
View
6 lib/base_structures/Bitfield.js
@@ -29,12 +29,13 @@ var Bitfield = exports.Bitfield = Structure.new();
* Get a single bit
*
* @param {int} bit
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this;
* @api public
*/
Bitfield.prototype.get = function (bit, callback) {
+ callback = callback || function () {};
this.client.getbit(this.key, bit, callback);
return this;
}
@@ -44,12 +45,13 @@ Bitfield.prototype.get = function (bit, callback) {
*
* @param {int} bit
* @param {bool} value
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this;
* @api public
*/
Bitfield.prototype.set = function (bit, value, callback) {
+ callback = callback || function () {};
this.client.setbit(this.key, bit, value ? 1 : 0, callback);
return this;
}
View
17 lib/base_structures/Hash.js
@@ -68,12 +68,13 @@ Hash.prototype.length = function (callback) {
* Delete a hash key.
*
* @param {string} hash_key
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this;
* @api public
*/
Hash.prototype.delete = Hash.prototype.del = function (hash_key, callback) {
+ callback = callback || function () {};
this.client.hdel(this.key, hash_key, callback);
return this;
}
@@ -103,16 +104,17 @@ Hash.prototype.exists = function (hash_key, callback) {
*
* @param {string|Object} hash_key
* @param {string} value (optional)
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this;
* @api public
*/
Hash.prototype.set = function (hash_key, value, callback) {
- if (typeof value === 'function') {
- callback = value;
+ if (typeof hash_key === 'object') {
+ callback = value || function () {};
this.client.hmset(this.key, hash_key, callback);
} else {
+ callback = callback || function () {};
this.client.hset(this.key, hash_key, value, callback);
}
return this;
@@ -129,6 +131,7 @@ Hash.prototype.set = function (hash_key, value, callback) {
*/
Hash.prototype.add = function (hash_key, value, callback) {
+ callback = callback || function () {};
this.client.hsetnx(this.key, hash_key, value, callback);
return this;
}
@@ -166,13 +169,14 @@ Hash.prototype.get = function (hash_key, callback) {
*
* @param {string} hash_key
* @param {int} amount (optional - default is 1)
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this;
* @api public
*/
Hash.prototype.increment =
Hash.prototype.incrBy = function (hash_key, amount, callback) {
+ callback = callback || function () {};
if (typeof amount === 'function') {
callback = amount;
amount = 1;
@@ -186,13 +190,14 @@ Hash.prototype.incrBy = function (hash_key, amount, callback) {
*
* @param {string} hash_key
* @param {int} amount (optional - default is 1)
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this;
* @api public
*/
Hash.prototype.decrement =
Hash.prototype.decrBy = function (hash_key, amount, callback) {
+ callback = callback || function () {};
if (typeof amount === 'function') {
callback = amount;
amount = 1;
View
31 lib/base_structures/List.js
@@ -82,12 +82,13 @@ List.prototype.get = function (index, count, callback) {
*
* @param {int} length
* @param {bool} keep_earliest (optional - defaults to false)
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
List.prototype.cap = function (length, keep_earliest, callback) {
+ callback = callback || function () {};
var start = 0, end = -1;
if (typeof keep_earliest === 'function') {
//Keep the last `length` elements
@@ -106,12 +107,13 @@ List.prototype.cap = function (length, keep_earliest, callback) {
*
* @param {string} value
* @param {bool} count (optional - defaults to 1)
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
List.prototype.remove = function (value, count, callback) {
+ callback = callback || function () {};
if (typeof count === 'function') {
callback = count;
count = 1;
@@ -125,12 +127,13 @@ List.prototype.remove = function (value, count, callback) {
*
* @param {int} start
* @param {int} end
- * @param {Function} callback
+ * @param {Function} callback (optional_
* @return this
* @api public
*/
List.prototype.trim = function (start, end, callback) {
+ callback = callback || function () {};
this.client.ltrim(this.key, start, end, callback);
return this;
}
@@ -140,12 +143,13 @@ List.prototype.trim = function (start, end, callback) {
*
* @param {int} pivot
* @param {string} value
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
List.prototype.insertBefore = function (pivot, value, callback) {
+ callback = callback || function () {};
this.client.linsert(this.key, 'BEFORE', pivot, value, callback);
return this;
}
@@ -155,12 +159,13 @@ List.prototype.insertBefore = function (pivot, value, callback) {
*
* @param {int} pivot
* @param {string} value
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
List.prototype.insertAfter = function (pivot, value, callback) {
+ callback = callback || function () {};
this.client.linsert(this.key, 'AFTER', pivot, value, callback);
return this;
}
@@ -170,12 +175,13 @@ List.prototype.insertAfter = function (pivot, value, callback) {
*
* @param {int} index
* @param {string} value
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
List.prototype.set = function (index, value, callback) {
+ callback = callback || function () {};
this.client.lset(this.key, index, value, callback);
return this;
}
@@ -234,13 +240,14 @@ List.prototype.pop = function (wait, callback) {
/**
* Add one or more elements to the start of the list.
*
- * @param {string|array} values
- * @param {Function} callback
+ * @param {string|array} value(s)
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
List.prototype.unshift = List.prototype.lpush = function (values, callback) {
+ callback = callback || function () {};
if (Array.isArray(values)) {
var multi = this.client.multi(), key = this.key;
values.reverse().forEach(function (value) {
@@ -256,13 +263,14 @@ List.prototype.unshift = List.prototype.lpush = function (values, callback) {
/**
* Add one or more elements to the end of the list.
*
- * @param {string|array} values
- * @param {Function} callback
+ * @param {string|array} value(s)
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
List.prototype.push = List.prototype.add = function (values, callback) {
+ callback = callback || function () {};
if (Array.isArray(values)) {
var multi = this.client.multi(), key = this.key;
values.forEach(function (value) {
@@ -281,12 +289,13 @@ List.prototype.push = List.prototype.add = function (values, callback) {
*
* @param {String|List} list
* @param {bool} wait (optional)
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
List.prototype.popShift = function (list, wait, callback) {
+ callback = callback || function () {};
list = this.getKey(list);
if (typeof wait === 'function') {
callback = wait;
View
9 lib/base_structures/Set.js
@@ -29,12 +29,13 @@ var Set = exports.Set = Structure.new();
* Add one or more elements to the set.
*
* @param {string|Array} element(s)
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
Set.prototype.add = function (element, callback) {
+ callback = callback || function () {};
if (Array.isArray(element)) {
return this.addAll(element, callback);
}
@@ -46,12 +47,13 @@ Set.prototype.add = function (element, callback) {
* Remove one or more elements from the set.
*
* @param {string|Array} element(s)
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
Set.prototype.remove = function (element, callback) {
+ callback = callback || function () {};
if (Array.isArray(element)) {
return this.removeAll(element, callback);
}
@@ -77,12 +79,13 @@ Set.prototype.elements = Set.prototype.members = function (callback) {
*
* @param {string|Set} dest
* @param {string} element
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
Set.prototype.move = function (dest, element, callback) {
+ callback = callback || function () {};
this.client.smove(this.key, this.getKey(dest), element, callback);
return this;
}
View
24 lib/base_structures/SortedSet.js
@@ -38,12 +38,13 @@ var SortedSet = exports.SortedSet = Structure.new();
* set.add({foo:12, bar:3}, callback);
*
* @param {string|Object} element(s)
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
SortedSet.prototype.add = function (score, element, callback) {
+ callback = callback || function () {};
if (typeof score === 'object') {
callback = element;
element = score;
@@ -57,12 +58,13 @@ SortedSet.prototype.add = function (score, element, callback) {
* Remove one or more elements from the set.
*
* @param {string|Array} element(s)
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
SortedSet.prototype.remove = function (element, callback) {
+ callback = callback || function () {};
if (Array.isArray(element)) {
return this.removeAll(element, callback);
}
@@ -132,12 +134,13 @@ SortedSet.prototype.score = function (element, callback) {
*
* @param {string} element
* @param {int} amount (optional - default is 1)
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this;
* @api public
*/
SortedSet.prototype.increment = SortedSet.prototype.incrBy = function (element, amount, callback) {
+ callback = callback || function () {};
if (typeof amount === 'function') {
callback = amount;
amount = 1;
@@ -151,12 +154,13 @@ SortedSet.prototype.increment = SortedSet.prototype.incrBy = function (element,
*
* @param {string} element
* @param {int} amount (optional - default is 1)
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this;
* @api public
*/
SortedSet.prototype.decrement = SortedSet.prototype.decrBy = function (element, amount, callback) {
+ callback = callback || function () {};
if (typeof amount === 'function') {
callback = amount;
amount = 1;
@@ -344,12 +348,13 @@ SortedSet.prototype.getScoresReverse = function (start, end, count, offset, call
*
* @param {int} start
* @param {int} end
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
SortedSet.prototype.removeScores = function (start, end, callback) {
+ callback = callback || function () {};
if (null === start) start = '-inf';
if (null === end) end = '+inf';
this.client.zremrangebyscore(this.key, start, end, callback);
@@ -378,10 +383,10 @@ SortedSet.prototype.countScores = function (start, end, callback) {
* Get elements with ranks between the specified range (inclusive).
*
* To get the first 3 elements in the set (with the highest scores):
- * set.getRanks(0, 2, callback);
+ * set.getRanks(0, 2, callback);
*
* To get the last 3 elements in the set (lowest scores):
- * set.getRanks(-3, -1, callback);
+ * set.getRanks(-3, -1, callback);
*
* @param {int} start
* @param {int} end
@@ -426,12 +431,13 @@ SortedSet.prototype.getRanksReverse = function (end, start, callback) {
*
* @param {int} start
* @param {int} end
- * @param {Function} callback
+ * @param {Function} callback (optional)
* @return this
* @api public
*/
SortedSet.prototype.removeRanks = function (start, end, callback) {
+ callback = callback || function () {};
if (null === start) start = -1;
if (null === end) end = 0;
this.client.zremrangebyrank(this.key, start, end, callback);
@@ -479,7 +485,7 @@ SortedSet.prototype.lowestScores = function (count, callback) {
* @api public
*/
-SortedSet.prototype.inter = SortedSet.prototype.intersect = function (dest, sets, weights, aggregate, callback) {
+SortedSet.prototype.inter = function (dest, sets, weights, aggregate, callback) {
var args = [], self = this;
args.push(this.getKey(dest));

0 comments on commit 4b48029

Please sign in to comment.