Dieses Repository beinhaltet HTML- und Javascript Code zur einer NotizenWebApp auf Basis von Web Storage. Zudem sind Mocha/Chai Tests im Browser enthalten. https://meinenotizen.netlify.app/
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.

index.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. var from = require('..')
  2. var spec = require('stream-spec')
  3. var a = require('assertions')
  4. function read(stream, callback) {
  5. var actual = []
  6. stream.on('data', function (data) {
  7. actual.push(data)
  8. })
  9. stream.once('end', function () {
  10. callback(null, actual)
  11. })
  12. stream.once('error', function (err) {
  13. callback(err)
  14. })
  15. }
  16. function pause(stream) {
  17. stream.on('data', function () {
  18. if(Math.random() > 0.1) return
  19. stream.pause()
  20. process.nextTick(function () {
  21. stream.resume()
  22. })
  23. })
  24. }
  25. exports['inc'] = function (test) {
  26. var fs = from(function (i) {
  27. this.emit('data', i)
  28. if(i >= 99)
  29. return this.emit('end')
  30. return true
  31. })
  32. spec(fs).readable().validateOnExit()
  33. read(fs, function (err, arr) {
  34. test.equal(arr.length, 100)
  35. test.done()
  36. })
  37. }
  38. exports['inc - async'] = function (test) {
  39. var fs = from(function (i, next) {
  40. this.emit('data', i)
  41. if(i >= 99)
  42. return this.emit('end')
  43. next();
  44. })
  45. spec(fs).readable().validateOnExit()
  46. read(fs, function (err, arr) {
  47. test.equal(arr.length, 100)
  48. test.done()
  49. })
  50. }
  51. exports['large stream - from an array'] = function (test) {
  52. var l = 100000
  53. , expected = []
  54. while(l--) expected.push(l * Math.random())
  55. var fs = from(expected.slice())
  56. spec(fs).readable().validateOnExit()
  57. read(fs, function (err, arr) {
  58. a.deepEqual(arr, expected)
  59. test.done()
  60. })
  61. }
  62. exports['large stream - callback return true'] = function (test) {
  63. var fs = from(function (i, next) {
  64. this.emit('data', i)
  65. if(i >= 99999)
  66. return this.emit('end')
  67. return true;
  68. })
  69. spec(fs).readable().validateOnExit()
  70. read(fs, function (err, arr) {
  71. test.equal(arr.length, 100000)
  72. test.done()
  73. })
  74. }
  75. exports['large stream - callback call next()'] = function (test) {
  76. var fs = from(function (i, next) {
  77. this.emit('data', i)
  78. if(i >= 99999)
  79. return this.emit('end')
  80. next();
  81. })
  82. spec(fs).readable().validateOnExit()
  83. read(fs, function (err, arr) {
  84. test.equal(arr.length, 100000)
  85. test.done()
  86. })
  87. }
  88. exports['simple'] = function (test) {
  89. var l = 1000
  90. , expected = []
  91. while(l--) expected.push(l * Math.random())
  92. var t = from(expected.slice())
  93. spec(t)
  94. .readable()
  95. .pausable({strict: true})
  96. .validateOnExit()
  97. read(t, function (err, actual) {
  98. if(err) test.error(err) //fail
  99. a.deepEqual(actual, expected)
  100. test.done()
  101. })
  102. }
  103. exports['simple pausable'] = function (test) {
  104. var l = 1000
  105. , expected = []
  106. while(l--) expected.push(l * Math.random())
  107. var t = from(expected.slice())
  108. spec(t)
  109. .readable()
  110. .pausable({strict: true})
  111. .validateOnExit()
  112. pause(t)
  113. read(t, function (err, actual) {
  114. if(err) test.error(err) //fail
  115. a.deepEqual(actual, expected)
  116. test.done()
  117. })
  118. }
  119. exports['simple (not strictly pausable) setTimeout'] = function (test) {
  120. var l = 10
  121. , expected = []
  122. while(l--) expected.push(l * Math.random())
  123. var _expected = expected.slice()
  124. var t = from(function (i, n) {
  125. var self = this
  126. setTimeout(function () {
  127. if(_expected.length)
  128. self.emit('data', _expected.shift())
  129. else
  130. if(!self.ended)
  131. self.emit('end')
  132. n()
  133. }, 3)
  134. })
  135. /*
  136. using from in this way will not be strictly pausable.
  137. it could be extended to buffer outputs, but I think a better
  138. way would be to use a PauseStream that implements strict pause.
  139. */
  140. spec(t)
  141. .readable()
  142. .pausable({strict: false })
  143. .validateOnExit()
  144. //pause(t)
  145. var paused = false
  146. var i = setInterval(function () {
  147. if(!paused) t.pause()
  148. else t.resume()
  149. paused = !paused
  150. }, 2)
  151. t.on('end', function () {
  152. clearInterval(i)
  153. })
  154. read(t, function (err, actual) {
  155. if(err) test.error(err) //fail
  156. a.deepEqual(actual, expected)
  157. test.done()
  158. })
  159. }