basic.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. var t = require('tap')
  2. var Yallist = require('../yallist.js')
  3. var y = new Yallist(1,2,3,4,5)
  4. var z = new Yallist([1,2,3,4,5])
  5. t.similar(y, z, 'build from single list or args')
  6. function add10 (i) {
  7. return i + 10
  8. }
  9. t.similar(y.map(add10).toArray(), [11, 12, 13, 14, 15])
  10. t.similar(y.mapReverse(add10).toArray(), [15, 14, 13, 12, 11])
  11. t.similar(y.map(add10).toArrayReverse(), [15, 14, 13, 12, 11])
  12. t.isa(Yallist(1,2,3), 'Yallist')
  13. t.equal(y.push(6, 7, 8), 8)
  14. t.similar(y.toArray(), [1, 2, 3, 4, 5, 6, 7, 8])
  15. y.pop()
  16. y.shift()
  17. y.unshift(100)
  18. var expect = [100, 2, 3, 4, 5, 6, 7]
  19. var expectReverse = [ 7, 6, 5, 4, 3, 2, 100 ]
  20. t.similar(y.toArray(), expect)
  21. t.equal(y.length, y.toArray().length)
  22. t.test(function forEach (t) {
  23. t.plan(y.length * 2)
  24. y.forEach(function (item, i, list) {
  25. t.equal(item, expect[i])
  26. t.equal(list, y)
  27. })
  28. })
  29. t.test(function forEach (t) {
  30. t.plan(y.length * 5)
  31. var n = 0
  32. y.forEachReverse(function (item, i, list) {
  33. t.equal(item, expectReverse[n])
  34. t.equal(item, expect[i])
  35. t.equal(item, y.get(i))
  36. t.equal(item, y.getReverse(n))
  37. n += 1
  38. t.equal(list, y)
  39. })
  40. })
  41. t.equal(y.getReverse(100), undefined)
  42. t.equal(y.get(9999), undefined)
  43. function sum (a, b) { return a + b }
  44. t.equal(y.reduce(sum), 127)
  45. t.equal(y.reduce(sum, 100), 227)
  46. t.equal(y.reduceReverse(sum), 127)
  47. t.equal(y.reduceReverse(sum, 100), 227)
  48. t.equal(Yallist().pop(), undefined)
  49. t.equal(Yallist().shift(), undefined)
  50. var x = Yallist()
  51. x.unshift(1)
  52. t.equal(x.length, 1)
  53. t.similar(x.toArray(), [1])
  54. // verify that y.toArray() returns an array and if we create a
  55. // new Yallist from that array, we get a list matching
  56. t.similar(Yallist(y.toArray()), y)
  57. t.similar(Yallist.apply(null, y.toArray()), y)
  58. t.throws(function () {
  59. new Yallist().reduce(function () {})
  60. }, {}, new TypeError('Reduce of empty list with no initial value'))
  61. t.throws(function () {
  62. new Yallist().reduceReverse(function () {})
  63. }, {}, new TypeError('Reduce of empty list with no initial value'))
  64. var z = y.reverse()
  65. t.equal(z, y)
  66. t.similar(y.toArray(), expectReverse)
  67. y.reverse()
  68. t.similar(y.toArray(), expect)
  69. var a = Yallist(1,2,3,4,5,6)
  70. var cases = [
  71. [ [2, 4], [3, 4] ],
  72. [ [2, -4], [] ],
  73. [ [2, -2], [3, 4] ],
  74. [ [1, -2], [2, 3, 4] ],
  75. [ [-1, -2], [] ],
  76. [ [-5, -2], [2, 3, 4] ],
  77. [ [-99, 2], [1, 2] ],
  78. [ [5, 99], [6] ],
  79. [ [], [1,2,3,4,5,6] ]
  80. ]
  81. t.test('slice', function (t) {
  82. t.plan(cases.length)
  83. cases.forEach(function (c) {
  84. t.test(JSON.stringify(c), function (t) {
  85. t.similar(a.slice.apply(a, c[0]), Yallist(c[1]))
  86. t.similar([].slice.apply(a.toArray(), c[0]), c[1])
  87. t.end()
  88. })
  89. })
  90. })
  91. t.test('sliceReverse', function (t) {
  92. t.plan(cases.length)
  93. cases.forEach(function (c) {
  94. var rev = c[1].slice().reverse()
  95. t.test(JSON.stringify([c[0], rev]), function (t) {
  96. t.similar(a.sliceReverse.apply(a, c[0]), Yallist(rev))
  97. t.similar([].slice.apply(a.toArray(), c[0]).reverse(), rev)
  98. t.end()
  99. })
  100. })
  101. })
  102. var inserter = Yallist(1,2,3,4,5)
  103. inserter.unshiftNode(inserter.head.next)
  104. t.similar(inserter.toArray(), [2,1,3,4,5])
  105. inserter.unshiftNode(inserter.tail)
  106. t.similar(inserter.toArray(), [5,2,1,3,4])
  107. inserter.unshiftNode(inserter.head)
  108. t.similar(inserter.toArray(), [5,2,1,3,4])
  109. var single = Yallist(1)
  110. single.unshiftNode(single.head)
  111. t.similar(single.toArray(), [1])
  112. inserter = Yallist(1,2,3,4,5)
  113. inserter.pushNode(inserter.tail.prev)
  114. t.similar(inserter.toArray(), [1,2,3,5,4])
  115. inserter.pushNode(inserter.head)
  116. t.similar(inserter.toArray(), [2,3,5,4,1])
  117. inserter.unshiftNode(inserter.head)
  118. t.similar(inserter.toArray(), [2,3,5,4,1])
  119. single = Yallist(1)
  120. single.pushNode(single.tail)
  121. t.similar(single.toArray(), [1])
  122. var swiped = Yallist(9,8,7)
  123. inserter.unshiftNode(swiped.head.next)
  124. t.similar(inserter.toArray(), [8,2,3,5,4,1])
  125. t.similar(swiped.toArray(), [9,7])
  126. swiped = Yallist(9,8,7)
  127. inserter.pushNode(swiped.head.next)
  128. t.similar(inserter.toArray(), [8,2,3,5,4,1,8])
  129. t.similar(swiped.toArray(), [9,7])
  130. swiped.unshiftNode(Yallist.Node(99))
  131. t.similar(swiped.toArray(), [99,9,7])
  132. swiped.pushNode(Yallist.Node(66))
  133. t.similar(swiped.toArray(), [99,9,7,66])
  134. var e = Yallist()
  135. e.unshiftNode(Yallist.Node(1))
  136. t.same(e.toArray(), [1])
  137. e = Yallist()
  138. e.pushNode(Yallist.Node(1))
  139. t.same(e.toArray(), [1])
  140. // steal them back, don't break the lists
  141. swiped.unshiftNode(inserter.head)
  142. t.same(swiped, Yallist(8,99,9,7,66))
  143. t.same(inserter, Yallist(2,3,5,4,1,8))
  144. swiped.unshiftNode(inserter.tail)
  145. t.same(inserter, Yallist(2,3,5,4,1))
  146. t.same(swiped, Yallist(8,8,99,9,7,66))
  147. t.throws(function remove_foreign_node () {
  148. e.removeNode(swiped.head)
  149. }, {}, new Error('removing node which does not belong to this list'))
  150. t.throws(function remove_unlisted_node () {
  151. e.removeNode(Yallist.Node('nope'))
  152. }, {}, new Error('removing node which does not belong to this list'))
  153. e = Yallist(1,2)
  154. e.removeNode(e.head)
  155. t.same(e, Yallist(2))
  156. e = Yallist(1,2)
  157. e.removeNode(e.tail)
  158. t.same(e, Yallist(1))