pax_global_header00006660000000000000000000000064126353330460014517gustar00rootroot0000000000000052 comment=702eaba87deefa9f8fc2f8e36cb225bc2141fdc3 yallist-2.0.0/000077500000000000000000000000001263533304600131775ustar00rootroot00000000000000yallist-2.0.0/.gitignore000066400000000000000000000000611263533304600151640ustar00rootroot00000000000000node_modules/ coverage/ .nyc_output/ nyc_output/ yallist-2.0.0/.travis.yml000066400000000000000000000001151263533304600153050ustar00rootroot00000000000000sudo: false language: node_js node_js: - '0.10' - '0.12' - '4' - '5' yallist-2.0.0/CONTRIBUTING.md000066400000000000000000000003421263533304600154270ustar00rootroot000000000000001. The whole API is public. No underscore-prefixed pretend-private things or hidden Object.create magic mumbo jumbo here. Plain old objects that are created from constructors. 2. 100% test coverage must be maintained. yallist-2.0.0/LICENSE000066400000000000000000000013751263533304600142120ustar00rootroot00000000000000The ISC License Copyright (c) Isaac Z. Schlueter and Contributors Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. yallist-2.0.0/README.md000066400000000000000000000111551263533304600144610ustar00rootroot00000000000000# yallist Yet Another Linked List There are many doubly-linked list implementations like it, but this one is mine. For when an array would be too big, and a Map can't be iterated in reverse order. [![Build Status](https://travis-ci.org/isaacs/yallist.svg?branch=master)](https://travis-ci.org/isaacs/yallist) [![Coverage Status](https://coveralls.io/repos/isaacs/yallist/badge.svg?service=github)](https://coveralls.io/github/isaacs/yallist) ## basic usage ```javascript var yallist = require('yallist') var myList = yallist.create([1, 2, 3]) myList.push('foo') myList.unshift('bar') // of course pop() and shift() are there, too console.log(myList.toArray()) // ['bar', 1, 2, 3, 'foo'] myList.forEach(function (k) { // walk the list head to tail }) myList.forEachReverse(function (k, index, list) { // walk the list tail to head }) var myDoubledList = myList.map(function (k) { return k + k }) // now myDoubledList contains ['barbar', 2, 4, 6, 'foofoo'] // mapReverse is also a thing var myDoubledListReverse = myList.mapReverse(function (k) { return k + k }) // ['foofoo', 6, 4, 2, 'barbar'] var reduced = myList.reduce(function (set, entry) { set += entry return set }, 'start') console.log(reduced) // 'startfoo123bar' ``` ## api The whole API is considered "public". Functions with the same name as an Array method work more or less the same way. There's reverse versions of most things because that's the point. ### Yallist Default export, the class that holds and manages a list. Call it with either a forEach-able (like an array) or a set of arguments, to initialize the list. The Array-ish methods all act like you'd expect. No magic length, though, so if you change that it won't automatically prune or add empty spots. ### Yallist.create(..) Alias for Yallist function. Some people like factories. #### yallist.head The first node in the list #### yallist.tail The last node in the list #### yallist.length The number of nodes in the list. (Change this at your peril. It is not magic like Array length.) #### yallist.toArray() Convert the list to an array. #### yallist.forEach(fn, [thisp]) Call a function on each item in the list. #### yallist.forEachReverse(fn, [thisp]) Call a function on each item in the list, in reverse order. #### yallist.get(n) Get the data at position `n` in the list. If you use this a lot, probably better off just using an Array. #### yallist.getReverse(n) Get the data at position `n`, counting from the tail. #### yallist.map(fn, thisp) Create a new Yallist with the result of calling the function on each item. #### yallist.mapReverse(fn, thisp) Same as `map`, but in reverse. #### yallist.pop() Get the data from the list tail, and remove the tail from the list. #### yallist.push(item, ...) Insert one or more items to the tail of the list. #### yallist.reduce(fn, initialValue) Like Array.reduce. #### yallist.reduceReverse Like Array.reduce, but in reverse. #### yallist.reverse Reverse the list in place. #### yallist.shift() Get the data from the list head, and remove the head from the list. #### yallist.slice([from], [to]) Just like Array.slice, but returns a new Yallist. #### yallist.sliceReverse([from], [to]) Just like yallist.slice, but the result is returned in reverse. #### yallist.toArray() Create an array representation of the list. #### yallist.toArrayReverse() Create a reversed array representation of the list. #### yallist.unshift(item, ...) Insert one or more items to the head of the list. #### yallist.unshiftNode(node) Move a Node object to the front of the list. (That is, pull it out of wherever it lives, and make it the new head.) If the node belongs to a different list, then that list will remove it first. #### yallist.pushNode(node) Move a Node object to the end of the list. (That is, pull it out of wherever it lives, and make it the new tail.) If the node belongs to a list already, then that list will remove it first. #### yallist.removeNode(node) Remove a node from the list, preserving referential integrity of head and tail and other nodes. Will throw an error if you try to have a list remove a node that doesn't belong to it. ### Yallist.Node The class that holds the data and is actually the list. Call with `var n = new Node(value, previousNode, nextNode)` Note that if you do direct operations on Nodes themselves, it's very easy to get into weird states where the list is broken. Be careful :) #### node.next The next node in the list. #### node.prev The previous node in the list. #### node.value The data the node contains. #### node.list The list to which this node belongs. (Null if it does not belong to any list.) yallist-2.0.0/package.json000066400000000000000000000007211263533304600154650ustar00rootroot00000000000000{ "name": "yallist", "version": "2.0.0", "description": "Yet Another Linked List", "main": "yallist.js", "directories": { "test": "test" }, "dependencies": {}, "devDependencies": { "tap": "^2.3.2" }, "scripts": { "test": "tap test/*.js --cov" }, "repository": { "type": "git", "url": "git+https://github.com/isaacs/yallist.git" }, "author": "Isaac Z. Schlueter (http://blog.izs.me/)", "license": "ISC" } yallist-2.0.0/test/000077500000000000000000000000001263533304600141565ustar00rootroot00000000000000yallist-2.0.0/test/basic.js000066400000000000000000000115401263533304600155760ustar00rootroot00000000000000var t = require('tap') var Yallist = require('../yallist.js') var y = new Yallist(1,2,3,4,5) var z = new Yallist([1,2,3,4,5]) t.similar(y, z, 'build from single list or args') function add10 (i) { return i + 10 } t.similar(y.map(add10).toArray(), [11, 12, 13, 14, 15]) t.similar(y.mapReverse(add10).toArray(), [15, 14, 13, 12, 11]) t.similar(y.map(add10).toArrayReverse(), [15, 14, 13, 12, 11]) t.isa(Yallist(1,2,3), 'Yallist') t.equal(y.push(6, 7, 8), 8) t.similar(y.toArray(), [1, 2, 3, 4, 5, 6, 7, 8]) y.pop() y.shift() y.unshift(100) var expect = [100, 2, 3, 4, 5, 6, 7] var expectReverse = [ 7, 6, 5, 4, 3, 2, 100 ] t.similar(y.toArray(), expect) t.equal(y.length, y.toArray().length) t.test(function forEach (t) { t.plan(y.length * 2) y.forEach(function (item, i, list) { t.equal(item, expect[i]) t.equal(list, y) }) }) t.test(function forEach (t) { t.plan(y.length * 5) var n = 0 y.forEachReverse(function (item, i, list) { t.equal(item, expectReverse[n]) t.equal(item, expect[i]) t.equal(item, y.get(i)) t.equal(item, y.getReverse(n)) n += 1 t.equal(list, y) }) }) t.equal(y.getReverse(100), undefined) t.equal(y.get(9999), undefined) function sum (a, b) { return a + b } t.equal(y.reduce(sum), 127) t.equal(y.reduce(sum, 100), 227) t.equal(y.reduceReverse(sum), 127) t.equal(y.reduceReverse(sum, 100), 227) t.equal(Yallist().pop(), undefined) t.equal(Yallist().shift(), undefined) var x = Yallist() x.unshift(1) t.equal(x.length, 1) t.similar(x.toArray(), [1]) // verify that y.toArray() returns an array and if we create a // new Yallist from that array, we get a list matching t.similar(Yallist(y.toArray()), y) t.similar(Yallist.apply(null, y.toArray()), y) t.throws(function () { new Yallist().reduce(function () {}) }, {}, new TypeError('Reduce of empty list with no initial value')) t.throws(function () { new Yallist().reduceReverse(function () {}) }, {}, new TypeError('Reduce of empty list with no initial value')) var z = y.reverse() t.equal(z, y) t.similar(y.toArray(), expectReverse) y.reverse() t.similar(y.toArray(), expect) var a = Yallist(1,2,3,4,5,6) var cases = [ [ [2, 4], [3, 4] ], [ [2, -4], [] ], [ [2, -2], [3, 4] ], [ [1, -2], [2, 3, 4] ], [ [-1, -2], [] ], [ [-5, -2], [2, 3, 4] ], [ [-99, 2], [1, 2] ], [ [5, 99], [6] ], [ [], [1,2,3,4,5,6] ] ] t.test('slice', function (t) { t.plan(cases.length) cases.forEach(function (c) { t.test(JSON.stringify(c), function (t) { t.similar(a.slice.apply(a, c[0]), Yallist(c[1])) t.similar([].slice.apply(a.toArray(), c[0]), c[1]) t.end() }) }) }) t.test('sliceReverse', function (t) { t.plan(cases.length) cases.forEach(function (c) { var rev = c[1].slice().reverse() t.test(JSON.stringify([c[0], rev]), function (t) { t.similar(a.sliceReverse.apply(a, c[0]), Yallist(rev)) t.similar([].slice.apply(a.toArray(), c[0]).reverse(), rev) t.end() }) }) }) var inserter = Yallist(1,2,3,4,5) inserter.unshiftNode(inserter.head.next) t.similar(inserter.toArray(), [2,1,3,4,5]) inserter.unshiftNode(inserter.tail) t.similar(inserter.toArray(), [5,2,1,3,4]) inserter.unshiftNode(inserter.head) t.similar(inserter.toArray(), [5,2,1,3,4]) var single = Yallist(1) single.unshiftNode(single.head) t.similar(single.toArray(), [1]) inserter = Yallist(1,2,3,4,5) inserter.pushNode(inserter.tail.prev) t.similar(inserter.toArray(), [1,2,3,5,4]) inserter.pushNode(inserter.head) t.similar(inserter.toArray(), [2,3,5,4,1]) inserter.unshiftNode(inserter.head) t.similar(inserter.toArray(), [2,3,5,4,1]) single = Yallist(1) single.pushNode(single.tail) t.similar(single.toArray(), [1]) var swiped = Yallist(9,8,7) inserter.unshiftNode(swiped.head.next) t.similar(inserter.toArray(), [8,2,3,5,4,1]) t.similar(swiped.toArray(), [9,7]) swiped = Yallist(9,8,7) inserter.pushNode(swiped.head.next) t.similar(inserter.toArray(), [8,2,3,5,4,1,8]) t.similar(swiped.toArray(), [9,7]) swiped.unshiftNode(Yallist.Node(99)) t.similar(swiped.toArray(), [99,9,7]) swiped.pushNode(Yallist.Node(66)) t.similar(swiped.toArray(), [99,9,7,66]) var e = Yallist() e.unshiftNode(Yallist.Node(1)) t.same(e.toArray(), [1]) e = Yallist() e.pushNode(Yallist.Node(1)) t.same(e.toArray(), [1]) // steal them back, don't break the lists swiped.unshiftNode(inserter.head) t.same(swiped, Yallist(8,99,9,7,66)) t.same(inserter, Yallist(2,3,5,4,1,8)) swiped.unshiftNode(inserter.tail) t.same(inserter, Yallist(2,3,5,4,1)) t.same(swiped, Yallist(8,8,99,9,7,66)) t.throws(function remove_foreign_node () { e.removeNode(swiped.head) }, {}, new Error('removing node which does not belong to this list')) t.throws(function remove_unlisted_node () { e.removeNode(Yallist.Node('nope')) }, {}, new Error('removing node which does not belong to this list')) e = Yallist(1,2) e.removeNode(e.head) t.same(e, Yallist(2)) e = Yallist(1,2) e.removeNode(e.tail) t.same(e, Yallist(1)) yallist-2.0.0/yallist.js000066400000000000000000000157721263533304600152320ustar00rootroot00000000000000module.exports = Yallist Yallist.Node = Node Yallist.create = Yallist function Yallist (list) { var self = this if (!(self instanceof Yallist)) { self = new Yallist() } self.tail = null self.head = null self.length = 0 if (list && typeof list.forEach === 'function') { list.forEach(function (item) { self.push(item) }) } else if (arguments.length > 0) { for (var i = 0, l = arguments.length; i < l; i++) { self.push(arguments[i]) } } return self } Yallist.prototype.removeNode = function (node) { if (node.list !== this) { throw new Error('removing node which does not belong to this list') } var next = node.next var prev = node.prev if (next) { next.prev = prev } if (prev) { prev.next = next } if (node === this.head) { this.head = next } if (node === this.tail) { this.tail = prev } node.list.length -- node.next = null node.prev = null node.list = null } Yallist.prototype.unshiftNode = function (node) { if (node === this.head) { return } if (node.list) { node.list.removeNode(node) } var head = this.head node.list = this node.next = head if (head) { head.prev = node } this.head = node if (!this.tail) { this.tail = node } this.length ++ } Yallist.prototype.pushNode = function (node) { if (node === this.tail) { return } if (node.list) { node.list.removeNode(node) } var tail = this.tail node.list = this node.prev = tail if (tail) { tail.next = node } this.tail = node if (!this.head) { this.head = node } this.length ++ } Yallist.prototype.push = function () { for (var i = 0, l = arguments.length; i < l; i++) { push(this, arguments[i]) } return this.length } Yallist.prototype.unshift = function () { for (var i = 0, l = arguments.length; i < l; i++) { unshift(this, arguments[i]) } return this.length } Yallist.prototype.pop = function () { if (!this.tail) return undefined var res = this.tail.value this.tail = this.tail.prev this.tail.next = null this.length -- return res } Yallist.prototype.shift = function () { if (!this.head) return undefined var res = this.head.value this.head = this.head.next this.head.prev = null this.length -- return res } Yallist.prototype.forEach = function (fn, thisp) { thisp = thisp || this for (var walker = this.head, i = 0; walker !== null; i++) { fn.call(thisp, walker.value, i, this) walker = walker.next } } Yallist.prototype.forEachReverse = function (fn, thisp) { thisp = thisp || this for (var walker = this.tail, i = this.length - 1; walker !== null; i--) { fn.call(thisp, walker.value, i, this) walker = walker.prev } } Yallist.prototype.get = function (n) { for (var i = 0, walker = this.head; walker !== null && i < n; i++) { // abort out of the list early if we hit a cycle walker = walker.next } if (i === n && walker !== null) { return walker.value } } Yallist.prototype.getReverse = function (n) { for (var i = 0, walker = this.tail; walker !== null && i < n; i++) { // abort out of the list early if we hit a cycle walker = walker.prev } if (i === n && walker !== null) { return walker.value } } Yallist.prototype.map = function (fn, thisp) { thisp = thisp || this var res = new Yallist() for (var walker = this.head; walker !== null; ) { res.push(fn.call(thisp, walker.value, this)) walker = walker.next } return res } Yallist.prototype.mapReverse = function (fn, thisp) { thisp = thisp || this var res = new Yallist() for (var walker = this.tail; walker !== null;) { res.push(fn.call(thisp, walker.value, this)) walker = walker.prev } return res } Yallist.prototype.reduce = function (fn, initial) { var acc var walker = this.head if (arguments.length > 1) { acc = initial } else if (this.head) { walker = this.head.next acc = this.head.value } else { throw new TypeError('Reduce of empty list with no initial value') } for (var i = 0; walker !== null; i++) { acc = fn(acc, walker.value, i) walker = walker.next } return acc } Yallist.prototype.reduceReverse = function (fn, initial) { var acc var walker = this.tail if (arguments.length > 1) { acc = initial } else if (this.tail) { walker = this.tail.prev acc = this.tail.value } else { throw new TypeError('Reduce of empty list with no initial value') } for (var i = this.length - 1; walker !== null; i--) { acc = fn(acc, walker.value, i) walker = walker.prev } return acc } Yallist.prototype.toArray = function () { var arr = new Array(this.length) for (var i = 0, walker = this.head; walker !== null; i++) { arr[i] = walker.value walker = walker.next } return arr } Yallist.prototype.toArrayReverse = function () { var arr = new Array(this.length) for (var i = 0, walker = this.tail; walker !== null; i++) { arr[i] = walker.value walker = walker.prev } return arr } Yallist.prototype.slice = function (from, to) { to = to || this.length if (to < 0) { to += this.length } from = from || 0 if (from < 0) { from += this.length } var ret = new Yallist() if (to < from || to < 0) { return ret } if (from < 0) { from = 0 } if (to > this.length) { to = this.length } for (var i = 0, walker = this.head; walker !== null && i < from; i++) { walker = walker.next } for (; walker !== null && i < to; i++, walker = walker.next) { ret.push(walker.value) } return ret } Yallist.prototype.sliceReverse = function (from, to) { to = to || this.length if (to < 0) { to += this.length } from = from || 0 if (from < 0) { from += this.length } var ret = new Yallist() if (to < from || to < 0) { return ret } if (from < 0) { from = 0 } if (to > this.length) { to = this.length } for (var i = this.length, walker = this.tail; walker !== null && i > to; i--) { walker = walker.prev } for (; walker !== null && i > from; i--, walker = walker.prev) { ret.push(walker.value) } return ret } Yallist.prototype.reverse = function () { var head = this.head var tail = this.tail for (var walker = head; walker !== null; walker = walker.prev) { var p = walker.prev walker.prev = walker.next walker.next = p } this.head = tail this.tail = head return this } function push (self, item) { self.tail = new Node(item, self.tail, null, self) if (!self.head) { self.head = self.tail } self.length ++ } function unshift (self, item) { self.head = new Node(item, null, self.head, self) if (!self.tail) { self.tail = self.head } self.length ++ } function Node (value, prev, next, list) { if (!(this instanceof Node)) { return new Node(value, prev, next, list) } this.list = list this.value = value if (prev) { prev.next = this this.prev = prev } else { this.prev = null } if (next) { next.prev = this this.next = next } else { this.next = null } }