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