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