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.

simple-map.asynct.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. 'use strict';
  2. var map = require('../')
  3. , it = require('it-is')
  4. , u = require('ubelt')
  5. , spec = require('stream-spec')
  6. , from = require('from')
  7. , Stream = require('stream')
  8. , es = require('event-stream')
  9. //REFACTOR THIS TEST TO USE es.readArray and es.writeArray
  10. function writeArray(array, stream) {
  11. array.forEach( function (j) {
  12. stream.write(j)
  13. })
  14. stream.end()
  15. }
  16. function readStream(stream, done) {
  17. var array = []
  18. stream.on('data', function (data) {
  19. array.push(data)
  20. })
  21. stream.on('error', done)
  22. stream.on('end', function (data) {
  23. done(null, array)
  24. })
  25. }
  26. //call sink on each write,
  27. //and complete when finished.
  28. function pauseStream (prob, delay) {
  29. var pauseIf = (
  30. 'number' == typeof prob
  31. ? function () {
  32. return Math.random() < prob
  33. }
  34. : 'function' == typeof prob
  35. ? prob
  36. : 0.1
  37. )
  38. var delayer = (
  39. !delay
  40. ? process.nextTick
  41. : 'number' == typeof delay
  42. ? function (next) { setTimeout(next, delay) }
  43. : delay
  44. )
  45. return es.through(function (data) {
  46. if(!this.paused && pauseIf()) {
  47. console.log('PAUSE STREAM PAUSING')
  48. this.pause()
  49. var self = this
  50. delayer(function () {
  51. console.log('PAUSE STREAM RESUMING')
  52. self.resume()
  53. })
  54. }
  55. console.log("emit ('data', " + data + ')')
  56. this.emit('data', data)
  57. })
  58. }
  59. exports ['simple map applied to a stream'] = function (test) {
  60. var input = [1,2,3,7,5,3,1,9,0,2,4,6]
  61. //create event stream from
  62. var doubler = map(function (data, cb) {
  63. cb(null, data * 2)
  64. })
  65. spec(doubler).through().validateOnExit()
  66. //a map is only a middle man, so it is both readable and writable
  67. it(doubler).has({
  68. readable: true,
  69. writable: true,
  70. })
  71. readStream(doubler, function (err, output) {
  72. it(output).deepEqual(input.map(function (j) {
  73. return j * 2
  74. }))
  75. // process.nextTick(x.validate)
  76. test.done()
  77. })
  78. writeArray(input, doubler)
  79. }
  80. exports ['stream comes back in the correct order'] = function (test) {
  81. var input = [3, 2, 1]
  82. var delayer = map(function(data, cb){
  83. setTimeout(function () {
  84. cb(null, data)
  85. }, 100 * data)
  86. })
  87. readStream(delayer, function (err, output) {
  88. it(output).deepEqual(input)
  89. test.done()
  90. })
  91. writeArray(input, delayer)
  92. }
  93. exports ['continues on error event with failures `true`'] = function (test) {
  94. var input = [1, 2, 3]
  95. var delayer = map(function(data, cb){
  96. cb(new Error('Something gone wrong'), data)
  97. }, { failures: true })
  98. readStream(delayer, function (err, output) {
  99. it(output).deepEqual(input)
  100. test.done()
  101. })
  102. writeArray(input, delayer)
  103. }
  104. exports['pipe two maps together'] = function (test) {
  105. var input = [1,2,3,7,5,3,1,9,0,2,4,6]
  106. //create event stream from
  107. function dd (data, cb) {
  108. cb(null, data * 2)
  109. }
  110. var doubler1 = map(dd), doubler2 = map(dd)
  111. doubler1.pipe(doubler2)
  112. spec(doubler1).through().validateOnExit()
  113. spec(doubler2).through().validateOnExit()
  114. readStream(doubler2, function (err, output) {
  115. it(output).deepEqual(input.map(function (j) {
  116. return j * 4
  117. }))
  118. test.done()
  119. })
  120. writeArray(input, doubler1)
  121. }
  122. //next:
  123. //
  124. // test pause, resume and drian.
  125. //
  126. // then make a pipe joiner:
  127. //
  128. // plumber (evStr1, evStr2, evStr3, evStr4, evStr5)
  129. //
  130. // will return a single stream that write goes to the first
  131. exports ['map will not call end until the callback'] = function (test) {
  132. var ticker = map(function (data, cb) {
  133. process.nextTick(function () {
  134. cb(null, data * 2)
  135. })
  136. })
  137. spec(ticker).through().validateOnExit()
  138. ticker.write('x')
  139. ticker.end()
  140. ticker.on('end', function () {
  141. test.done()
  142. })
  143. }
  144. exports ['emit failures with opts.failures === `ture`'] = function (test) {
  145. var err = new Error('INTENSIONAL ERROR')
  146. , mapper =
  147. map(function () {
  148. throw err
  149. }, { failures: true })
  150. mapper.on('failure', function (_err) {
  151. it(_err).equal(err)
  152. test.done()
  153. })
  154. mapper.write('hello')
  155. }
  156. exports ['emit error thrown'] = function (test) {
  157. var err = new Error('INTENSIONAL ERROR')
  158. , mapper =
  159. map(function () {
  160. throw err
  161. })
  162. mapper.on('error', function (_err) {
  163. it(_err).equal(err)
  164. test.done()
  165. })
  166. mapper.write('hello')
  167. }
  168. exports ['emit error calledback'] = function (test) {
  169. var err = new Error('INTENSIONAL ERROR')
  170. , mapper =
  171. map(function (data, callback) {
  172. callback(err)
  173. })
  174. mapper.on('error', function (_err) {
  175. it(_err).equal(err)
  176. test.done()
  177. })
  178. mapper.write('hello')
  179. }
  180. exports ['do not emit drain if not paused'] = function (test) {
  181. var maps = map(function (data, callback) {
  182. u.delay(callback)(null, 1)
  183. return true
  184. })
  185. spec(maps).through().pausable().validateOnExit()
  186. maps.on('drain', function () {
  187. it(false).ok('should not emit drain unless the stream is paused')
  188. })
  189. it(maps.write('hello')).equal(true)
  190. it(maps.write('hello')).equal(true)
  191. it(maps.write('hello')).equal(true)
  192. setTimeout(function () {maps.end()},10)
  193. maps.on('end', test.done)
  194. }
  195. exports ['emits drain if paused, when all '] = function (test) {
  196. var active = 0
  197. var drained = false
  198. var maps = map(function (data, callback) {
  199. active ++
  200. u.delay(function () {
  201. active --
  202. callback(null, 1)
  203. })()
  204. console.log('WRITE', false)
  205. return false
  206. })
  207. spec(maps).through().validateOnExit()
  208. maps.on('drain', function () {
  209. drained = true
  210. it(active).equal(0, 'should emit drain when all maps are done')
  211. })
  212. it(maps.write('hello')).equal(false)
  213. it(maps.write('hello')).equal(false)
  214. it(maps.write('hello')).equal(false)
  215. process.nextTick(function () {maps.end()},10)
  216. maps.on('end', function () {
  217. console.log('end')
  218. it(drained).ok('shoud have emitted drain before end')
  219. test.done()
  220. })
  221. }
  222. exports ['map applied to a stream with filtering'] = function (test) {
  223. var input = [1,2,3,7,5,3,1,9,0,2,4,6]
  224. var doubler = map(function (data, callback) {
  225. if (data % 2)
  226. callback(null, data * 2)
  227. else
  228. callback()
  229. })
  230. readStream(doubler, function (err, output) {
  231. it(output).deepEqual(input.filter(function (j) {
  232. return j % 2
  233. }).map(function (j) {
  234. return j * 2
  235. }))
  236. test.done()
  237. })
  238. spec(doubler).through().validateOnExit()
  239. writeArray(input, doubler)
  240. }