diff options
author | Alexander Early <alexander.early@gmail.com> | 2017-04-06 22:17:49 -0700 |
---|---|---|
committer | Alexander Early <alexander.early@gmail.com> | 2017-04-06 22:19:23 -0700 |
commit | 3dc9f8f1e120f0c95f67221ac0d1a9bac7b88cd4 (patch) | |
tree | 18407f9b503e0b01a29536e02cbb1bcd9e541737 /lib/internal | |
parent | 1af382d62b535f255a699fa03a4043faa5d3514e (diff) | |
download | async-3dc9f8f1e120f0c95f67221ac0d1a9bac7b88cd4.tar.gz |
added tests for DLL, filter(), non-leaking empty()
Diffstat (limited to 'lib/internal')
-rw-r--r-- | lib/internal/DoublyLinkedList.js | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/lib/internal/DoublyLinkedList.js b/lib/internal/DoublyLinkedList.js index 79c3bb5..0feea1b 100644 --- a/lib/internal/DoublyLinkedList.js +++ b/lib/internal/DoublyLinkedList.js @@ -23,7 +23,10 @@ DLL.prototype.removeLink = function(node) { return node; } -DLL.prototype.empty = DLL; +DLL.prototype.empty = function () { + while(this.head) this.shift(); + return this; +}; DLL.prototype.insertAfter = function(node, newNode) { newNode.prev = node; @@ -60,3 +63,26 @@ DLL.prototype.shift = function() { DLL.prototype.pop = function() { return this.tail && this.removeLink(this.tail); }; + +DLL.prototype.toArray = function () { + var arr = Array(this.length); + var idx = 0; + var curr = this.head; + for(idx = 0; idx < this.length; idx++) { + arr[idx] = curr.data; + curr = curr.next; + } + return arr; +} + +DLL.prototype.filter = function (testFn) { + var curr = this.head; + while(!!curr) { + var next = curr.next; + if (!testFn(curr)) { + this.removeLink(curr); + } + curr = next; + } + return this; +} |