Comparing version 1.0.1 to 1.0.2
@@ -24,2 +24,3 @@ ;(function() { // closure for web browsers | ||
} | ||
, pop: function () { | ||
@@ -37,2 +38,3 @@ if (this.length === 0) return undefined | ||
} | ||
, unshift: function (data) { | ||
@@ -43,2 +45,3 @@ this._head = new Item(data, null, this._head) | ||
} | ||
, shift: function () { | ||
@@ -56,2 +59,3 @@ if (this.length === 0) return undefined | ||
} | ||
, item: function (n) { | ||
@@ -63,2 +67,3 @@ if (n < 0) n = this.length + n | ||
} | ||
, slice: function (n, m) { | ||
@@ -70,6 +75,10 @@ if (!n) n = 0 | ||
if (m <= n) { | ||
throw new Error("invalid offset: "+n+","+m) | ||
if (m === n) { | ||
return [] | ||
} | ||
if (m < n) { | ||
throw new Error("invalid offset: "+n+","+m+" (length="+this.length+")") | ||
} | ||
var len = m - n | ||
@@ -86,5 +95,50 @@ , ret = new Array(len) | ||
} | ||
, drop: function () { | ||
FastList.call(this) | ||
} | ||
, forEach: function (fn, thisp) { | ||
var p = this._head | ||
, i = 0 | ||
, len = this.length | ||
while (i < len && p) { | ||
fn.call(thisp || this, p.data, i, this) | ||
p = p.next | ||
i ++ | ||
} | ||
} | ||
, map: function (fn, thisp) { | ||
var n = new FastList() | ||
this.forEach(function (v, i, me) { | ||
n.push(fn.call(thisp || me, v, i, me)) | ||
}) | ||
return n | ||
} | ||
, filter: function (fn, thisp) { | ||
var n = new FastList() | ||
this.forEach(function (v, i, me) { | ||
if (fn.call(thisp || me, v, i, me)) n.push(v) | ||
}) | ||
return n | ||
} | ||
, reduce: function (fn, val, thisp) { | ||
var i = 0 | ||
, p = this._head | ||
, len = this.length | ||
if (!val) { | ||
i = 1 | ||
val = p && p.data | ||
p = p && p.next | ||
} | ||
while (i < len && p) { | ||
val = fn.call(thisp || this, val, p.data, this) | ||
i ++ | ||
p = p.next | ||
} | ||
return val | ||
} | ||
} | ||
@@ -91,0 +145,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "A fast linked list (good for queues, stacks, etc.)", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"repository": { | ||
@@ -18,5 +18,5 @@ "type": "git", | ||
"scripts": { | ||
"test": "node test.js", | ||
"test": "tap test.js", | ||
"bench": "node bench.js" | ||
} | ||
} |
29
test.js
@@ -82,3 +82,32 @@ var tap = require("tap") | ||
// verify that map, reduce, and filter all match their | ||
// array counterparts. This implies forEach coverage as well, | ||
// since map and filter rely on it. | ||
function reduce (l, r) { | ||
l[r] = true | ||
return l | ||
} | ||
t.deepEqual( list.reduce(reduce, {}) | ||
, list.slice().reduce(reduce, {}) | ||
, "reduce") | ||
// filter out the first three items | ||
function filter (v) { | ||
return v.charAt(0) !== "b" | ||
} | ||
t.deepEqual( list.filter(filter).slice() | ||
, list.slice().filter(filter) | ||
, "filter") | ||
// double all the items | ||
function map (v) { | ||
return v + v | ||
} | ||
t.deepEqual( list.map(map).slice() | ||
, list.slice().map(map) | ||
, "map") | ||
t.end() | ||
}) | ||
10179
7
273