You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.6KB

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