Browse files

implemeneted `ltrim`

  • Loading branch information...
1 parent 90ad037 commit 49516cd8eaf639f3a78192a4abde9bfe065a4043 @fent fent committed Feb 7, 2012
Showing with 52 additions and 1 deletion.
  1. +8 −1 lib/eventvat.js
  2. +26 −0 test/events-test.js
  3. +18 −0 test/methods-test.js
View
9 lib/eventvat.js
@@ -1036,7 +1036,14 @@
// Trim a list to the specified range
//
EventVat.prototype.ltrim = function(key, start, stop) {
- throw new Error('Not implemented.');
+ if (this.type(key) === 'list') {
+ this.hash[key].value = this.hash[key].value.slice(start, stop);
+ this.emit('ltrim ' + key, start, stop);
+ this.emit('ltrim', key, start, stop);
+ return true;
+ } else {
+ return false;
+ }
};
//
View
26 test/events-test.js
@@ -1248,5 +1248,31 @@ module.exports = simpleEvents({
test.done();
},
+ 'Raise event on `ltrim` method invokation': function(test) {
+
+ var vat = EventVat();
+
+ vat.on('ltrim', function(key, start, stop) {
+ test.equal(key, 'list');
+ test.equal(start, 0);
+ test.equal(stop, 3);
+ });
+
+ vat.on('ltrim list', function(start, stop) {
+ test.equal(start, 0);
+ test.equal(stop, 3);
+ });
+
+ vat.rpush('list', 'one')
+ vat.rpush('list', 'two');
+ vat.rpush('list', 'three');
+ vat.rpush('list', 'four');
+ vat.ltrim('list', 0, 3);
+
+ test.expect(5);
+ vat.die();
+ test.done();
+
+ },
});
View
18 test/methods-test.js
@@ -953,4 +953,22 @@ this.methodSuite = {
test.done();
},
+ 'Invoke `ltrim` method and report new length and values stored': function(test) {
+
+ var vat = EventVat();
+
+ vat.rpush('list', 'one')
+ vat.rpush('list', 'two');
+ vat.rpush('list', 'three');
+ vat.rpush('list', 'four');
+ test.equal(vat.llen('list'), 4);
+
+ vat.ltrim('list', 0, 3);
+ test.equal(vat.llen('list'), 3);
+ test.deepEqual(vat.lrange('list', 0, 100), ['one', 'two', 'three']);
+
+ vat.die();
+ test.done();
+
+ },
};

0 comments on commit 49516cd

Please sign in to comment.