Skip to content

Commit

Permalink
Added LinkedList.every() with tests and docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
clayzermk1 committed Jan 29, 2014
1 parent 062430c commit 96179fe
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
18 changes: 17 additions & 1 deletion docs/LinkedList.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ _Source: [./lib/LinkedList.js](../lib/LinkedList.js)_

- <a name="toc_linkedlistarray"></a>[LinkedList](#linkedlistarray)
- <a name="toc_linkedlistprototypeconcatlist1-listn"></a><a name="toc_linkedlistprototype"></a>[LinkedList.prototype.concat](#linkedlistprototypeconcatlist1-listn)
- <a name="toc_linkedlistprototypeeverycallback-thisarg"></a>[LinkedList.prototype.every](#linkedlistprototypeeverycallback-thisarg)
- <a name="toc_linkedlistprototypeforeachcallback-thisarg"></a>[LinkedList.prototype.forEach](#linkedlistprototypeforeachcallback-thisarg)
- <a name="toc_linkedlistprototypegetindex"></a>[LinkedList.prototype.get](#linkedlistprototypegetindex)
- <a name="toc_linkedlistprototypeindexofsearch-fromindex"></a>[LinkedList.prototype.indexOf](#linkedlistprototypeindexofsearch-fromindex)
Expand Down Expand Up @@ -48,13 +49,28 @@ _Source: [./lib/LinkedList.js](../lib/LinkedList.js)_

<sub>Go: [TOC](#tableofcontents) | [LinkedList.prototype](#toc_linkedlistprototype)</sub>

# LinkedList.prototype.every(callback, thisArg)

> Determines whether each value in the [LinkedList](#linkedlistarray) passes a truth test.
**Parameters:**

- `{function} callback` The callback function to use as a truth test. Will be passed (value, index, [LinkedList](#linkedlistarray), [thisArg]). Should return true or false.
- `{object} thisArg` Optional context to pass as `this`.

**Return:**

`{boolean}` Whether every value in the [LinkedList](#linkedlistarray) passed the truth test.

<sub>Go: [TOC](#tableofcontents) | [LinkedList.prototype](#toc_linkedlistprototype)</sub>

# LinkedList.prototype.forEach(callback, thisArg)

> Executes a provided function once per node value.
**Parameters:**

- `{function} callback` The callback function to call for each node value. Will be passed (value, index, [LinkedList](#linkedlistarray), [thisArg])
- `{function} callback` The callback function to call for each node value. Will be passed (value, index, [LinkedList](#linkedlistarray), [thisArg]).
- `{object} thisArg` Optional context to pass as `this`.

**Return:**
Expand Down
22 changes: 20 additions & 2 deletions lib/LinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,32 @@ var LinkedList = (function () {
return this;
};

//TODO LinkedList.prototype.every
/**
* Determines whether each value in the LinkedList passes a truth test.
*
* @param {function} callback The callback function to use as a truth test. Will be passed (value, index, LinkedList, [thisArg]). Should return true or false.
* @param {object} thisArg Optional context to pass as `this`.
* @return {boolean} Whether every value in the LinkedList passed the truth test.
*/
LinkedList.prototype.every = function (callback, thisArg) {
// Iterate over each node and call callback for each node value
var current = this.head;
for (var i = 0; i < this.length; i++) {
if (!callback(current.value, i, this, thisArg)) {
return false;
}
current = current.next;
}

return true;
};

//TODO LinkedList.prototype.filter

/**
* Executes a provided function once per node value.
*
* @param {function} callback The callback function to call for each node value. Will be passed (value, index, LinkedList, [thisArg])
* @param {function} callback The callback function to call for each node value. Will be passed (value, index, LinkedList, [thisArg]).
* @param {object} thisArg Optional context to pass as `this`.
* @return {LinkedList} The LinkedList.
*/
Expand Down
23 changes: 23 additions & 0 deletions test/LinkedListSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ test("LinkedList.prototype.concat()", function (t) {
t.end();
});

test("LinkedList.prototype.every()", function (t) {
var l = new LinkedList([ "foo", "bar", "baz" ]);
var res = l.every(function (val, idx, ll, context) {
var v, i;
switch (idx) {
case 0: v = "foo"; i = 0; break;
case 1: v = "bar"; i = 1; break;
case 2: v = "baz"; i = 2; break;
}
t.equal(val, v, "val should be equal to " + v);
t.equal(idx, i, "idx should be equal to " + i);
t.deepEqual(ll, l, "ll should be the same as l");
t.deepEqual(context, { "cat": "dog" }, "context should be the same");
return (val !== "bar");
}, { "cat": "dog" });
t.deepEqual(res, false, "every value must not be equal to bar");
res = l.every(function (val, idx, ll, context) {
return (val !== "boom");
});
t.deepEqual(res, true, "every value must not be equal to boom");
t.end();
});

test("LinkedList.prototype.forEach()", function (t) {
var l = new LinkedList([ "foo", "bar", "baz" ]);
var res = l.forEach(function (val, idx, ll, context) {
Expand Down

0 comments on commit 96179fe

Please sign in to comment.