• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..20-Sep-2020-

LICENSEH A D26-Oct-1985765 1612

README.mdH A D26-Oct-19854.6 KiB205123

package.jsonH A D20-Sep-20201.7 KiB6766

README.md

1# yallist
2
3Yet Another Linked List
4
5There are many doubly-linked list implementations like it, but this
6one is mine.
7
8For when an array would be too big, and a Map can't be iterated in
9reverse order.
10
11
12[![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)
13
14## basic usage
15
16```javascript
17var yallist = require('yallist')
18var myList = yallist.create([1, 2, 3])
19myList.push('foo')
20myList.unshift('bar')
21// of course pop() and shift() are there, too
22console.log(myList.toArray()) // ['bar', 1, 2, 3, 'foo']
23myList.forEach(function (k) {
24  // walk the list head to tail
25})
26myList.forEachReverse(function (k, index, list) {
27  // walk the list tail to head
28})
29var myDoubledList = myList.map(function (k) {
30  return k + k
31})
32// now myDoubledList contains ['barbar', 2, 4, 6, 'foofoo']
33// mapReverse is also a thing
34var myDoubledListReverse = myList.mapReverse(function (k) {
35  return k + k
36}) // ['foofoo', 6, 4, 2, 'barbar']
37
38var reduced = myList.reduce(function (set, entry) {
39  set += entry
40  return set
41}, 'start')
42console.log(reduced) // 'startfoo123bar'
43```
44
45## api
46
47The whole API is considered "public".
48
49Functions with the same name as an Array method work more or less the
50same way.
51
52There's reverse versions of most things because that's the point.
53
54### Yallist
55
56Default export, the class that holds and manages a list.
57
58Call it with either a forEach-able (like an array) or a set of
59arguments, to initialize the list.
60
61The Array-ish methods all act like you'd expect.  No magic length,
62though, so if you change that it won't automatically prune or add
63empty spots.
64
65### Yallist.create(..)
66
67Alias for Yallist function.  Some people like factories.
68
69#### yallist.head
70
71The first node in the list
72
73#### yallist.tail
74
75The last node in the list
76
77#### yallist.length
78
79The number of nodes in the list.  (Change this at your peril.  It is
80not magic like Array length.)
81
82#### yallist.toArray()
83
84Convert the list to an array.
85
86#### yallist.forEach(fn, [thisp])
87
88Call a function on each item in the list.
89
90#### yallist.forEachReverse(fn, [thisp])
91
92Call a function on each item in the list, in reverse order.
93
94#### yallist.get(n)
95
96Get the data at position `n` in the list.  If you use this a lot,
97probably better off just using an Array.
98
99#### yallist.getReverse(n)
100
101Get the data at position `n`, counting from the tail.
102
103#### yallist.map(fn, thisp)
104
105Create a new Yallist with the result of calling the function on each
106item.
107
108#### yallist.mapReverse(fn, thisp)
109
110Same as `map`, but in reverse.
111
112#### yallist.pop()
113
114Get the data from the list tail, and remove the tail from the list.
115
116#### yallist.push(item, ...)
117
118Insert one or more items to the tail of the list.
119
120#### yallist.reduce(fn, initialValue)
121
122Like Array.reduce.
123
124#### yallist.reduceReverse
125
126Like Array.reduce, but in reverse.
127
128#### yallist.reverse
129
130Reverse the list in place.
131
132#### yallist.shift()
133
134Get the data from the list head, and remove the head from the list.
135
136#### yallist.slice([from], [to])
137
138Just like Array.slice, but returns a new Yallist.
139
140#### yallist.sliceReverse([from], [to])
141
142Just like yallist.slice, but the result is returned in reverse.
143
144#### yallist.toArray()
145
146Create an array representation of the list.
147
148#### yallist.toArrayReverse()
149
150Create a reversed array representation of the list.
151
152#### yallist.unshift(item, ...)
153
154Insert one or more items to the head of the list.
155
156#### yallist.unshiftNode(node)
157
158Move a Node object to the front of the list.  (That is, pull it out of
159wherever it lives, and make it the new head.)
160
161If the node belongs to a different list, then that list will remove it
162first.
163
164#### yallist.pushNode(node)
165
166Move a Node object to the end of the list.  (That is, pull it out of
167wherever it lives, and make it the new tail.)
168
169If the node belongs to a list already, then that list will remove it
170first.
171
172#### yallist.removeNode(node)
173
174Remove a node from the list, preserving referential integrity of head
175and tail and other nodes.
176
177Will throw an error if you try to have a list remove a node that
178doesn't belong to it.
179
180### Yallist.Node
181
182The class that holds the data and is actually the list.
183
184Call with `var n = new Node(value, previousNode, nextNode)`
185
186Note that if you do direct operations on Nodes themselves, it's very
187easy to get into weird states where the list is broken.  Be careful :)
188
189#### node.next
190
191The next node in the list.
192
193#### node.prev
194
195The previous node in the list.
196
197#### node.value
198
199The data the node contains.
200
201#### node.list
202
203The list to which this node belongs.  (Null if it does not belong to
204any list.)
205