UNPKG

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