Permalink
Browse files

returns `null` when getting non existant keys

  • Loading branch information...
1 parent ceb7483 commit e72383c4ac3c7d0a4deea192c5a400e57c91f422 @fent fent committed Feb 7, 2012
Showing with 68 additions and 65 deletions.
  1. +24 −21 lib/eventvat.js
  2. +2 −2 test/events-test.js
  3. +42 −42 test/methods-test.js
View
@@ -186,7 +186,7 @@
EventVat.prototype.randomkey = function() {
var keys = this.keys(/^/);
var index = Math.floor(Math.random()*keys.length);
- var key = keys[index];
+ var key = keys[index] || null;
this.emit('randomkey', key);
return key;
};
@@ -202,7 +202,7 @@
delete this.hash[oldKey];
this.emit('rename ' + oldKey, newKey);
this.emit('rename', oldKey, newKey);
- return this.hash[newKey].value;
+ return true;
}
else {
return false;
@@ -218,7 +218,7 @@
this.hash[newKey] = this.hash[oldKey];
this.emit('renamenx ' + oldKey, newKey);
this.emit('renamenx', oldKey, newKey);
- return this.hash[key].value;
+ return true;
}
else {
return false;
@@ -279,16 +279,19 @@
// Append a value to a key
//
EventVat.prototype.append = function(key, value) {
- if(has(this.hash, key) && this.type(key) === 'string') {
+ var type = this.type(key);
+ if (type === 'none') {
+ this.hash[key] = { value: '' };
+ } else if (type !== 'string') {
+ return 0;
+ } else {
this.persist(key);
- var newValue = this.hash[key].value += value;
- this.emit('append ' + key, value, newValue);
- this.emit('append', key, value, newValue);
- return newValue;
- }
- else {
- return false;
}
+
+ var newValue = this.hash[key].value += value;
+ this.emit('append ' + key, value, newValue);
+ this.emit('append', key, value, newValue);
+ return newValue.length;
};
//
@@ -334,7 +337,7 @@
return newValue;
}
else {
- return false;
+ return null;
}
};
@@ -563,7 +566,7 @@
this.emit('strlen', key, l);
return l;
}
- return false;
+ return 0;
};
//
@@ -615,7 +618,7 @@
return value;
}
else {
- return false;
+ return null;
}
};
@@ -876,7 +879,7 @@
index = len + index;
}
if (index < 0 || index >= len) {
- return false;
+ return null;
}
var value = list[index];
@@ -916,12 +919,12 @@
EventVat.prototype.lpop = function(key) {
if (this.type(key) === 'list') {
this.persist(key);
- var value = this.hash[key].value.shift();
+ var value = this.hash[key].value.shift() || null;
this.emit('lpop ' + key, value);
this.emit('lpop', key, value);
return value;
} else {
- return false;
+ return null;
}
};
@@ -1073,12 +1076,12 @@
EventVat.prototype.rpop = function(key) {
if (this.type(key) === 'list') {
this.persist(key);
- var value = this.hash[key].value.pop();
+ var value = this.hash[key].value.pop() || null;
this.emit('rpop ' + key, value);
this.emit('rpop', key, value);
return value;
} else {
- return false;
+ return null;
}
};
@@ -1094,7 +1097,7 @@
dest = [];
this.hash[destination] = { value: dest, type: 'list' };
} else if (dtype !== 'list') {
- return false;
+ return null;
} else {
this.persist(destination);
dest = this.hash[destination].value;
@@ -1111,7 +1114,7 @@
this.emit('lpush', destination, value);
return value;
} else {
- return false;
+ return null;
}
};
View
@@ -861,11 +861,11 @@ module.exports = simpleEvents({
vat.on('hmget', function(key, values) {
test.equal(key, 'foo');
- test.deepEqual(values, [1, 2, false]);
+ test.deepEqual(values, [1, 2, null]);
});
vat.on('hmget foo', function(values) {
- test.deepEqual(values, [1, 2, false]);
+ test.deepEqual(values, [1, 2, null]);
});
vat.hset('foo', 'a', 1);
Oops, something went wrong.

0 comments on commit e72383c

Please sign in to comment.