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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. 'use strict';
  2. var es = require('../')
  3. , it = require('it-is')
  4. , u = require('ubelt')
  5. , spec = require('stream-spec')
  6. , Stream = require('stream')
  7. , from = require('from')
  8. , through = require('through')
  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'] = function (test) {
  60. var input = u.map(1, 1000, function () {
  61. return Math.random()
  62. })
  63. var expected = input.map(function (v) {
  64. return v * 2
  65. })
  66. var pause = pauseStream(0.1)
  67. var fs = from(input)
  68. var ts = es.writeArray(function (err, ar) {
  69. it(ar).deepEqual(expected)
  70. test.done()
  71. })
  72. var map = es.through(function (data) {
  73. this.emit('data', data * 2)
  74. })
  75. spec(map).through().validateOnExit()
  76. spec(pause).through().validateOnExit()
  77. fs.pipe(map).pipe(pause).pipe(ts)
  78. }
  79. exports ['simple map applied to a stream'] = function (test) {
  80. var input = [1,2,3,7,5,3,1,9,0,2,4,6]
  81. //create event stream from
  82. var doubler = es.map(function (data, cb) {
  83. cb(null, data * 2)
  84. })
  85. spec(doubler).through().validateOnExit()
  86. //a map is only a middle man, so it is both readable and writable
  87. it(doubler).has({
  88. readable: true,
  89. writable: true,
  90. })
  91. readStream(doubler, function (err, output) {
  92. it(output).deepEqual(input.map(function (j) {
  93. return j * 2
  94. }))
  95. // process.nextTick(x.validate)
  96. test.done()
  97. })
  98. writeArray(input, doubler)
  99. }
  100. exports['pipe two maps together'] = function (test) {
  101. var input = [1,2,3,7,5,3,1,9,0,2,4,6]
  102. //create event stream from
  103. function dd (data, cb) {
  104. cb(null, data * 2)
  105. }
  106. var doubler1 = es.map(dd), doubler2 = es.map(dd)
  107. doubler1.pipe(doubler2)
  108. spec(doubler1).through().validateOnExit()
  109. spec(doubler2).through().validateOnExit()
  110. readStream(doubler2, function (err, output) {
  111. it(output).deepEqual(input.map(function (j) {
  112. return j * 4
  113. }))
  114. test.done()
  115. })
  116. writeArray(input, doubler1)
  117. }
  118. //next:
  119. //
  120. // test pause, resume and drian.
  121. //
  122. // then make a pipe joiner:
  123. //
  124. // plumber (evStr1, evStr2, evStr3, evStr4, evStr5)
  125. //
  126. // will return a single stream that write goes to the first
  127. exports ['map will not call end until the callback'] = function (test) {
  128. var ticker = es.map(function (data, cb) {
  129. process.nextTick(function () {
  130. cb(null, data * 2)
  131. })
  132. })
  133. spec(ticker).through().validateOnExit()
  134. ticker.write('x')
  135. ticker.end()
  136. ticker.on('end', function () {
  137. test.done()
  138. })
  139. }
  140. exports ['emit error thrown'] = function (test) {
  141. var err = new Error('INTENSIONAL ERROR')
  142. , mapper =
  143. es.map(function () {
  144. throw err
  145. })
  146. mapper.on('error', function (_err) {
  147. it(_err).equal(err)
  148. test.done()
  149. })
  150. // onExit(spec(mapper).basic().validate)
  151. //need spec that says stream may error.
  152. mapper.write('hello')
  153. }
  154. exports ['emit error calledback'] = function (test) {
  155. var err = new Error('INTENSIONAL ERROR')
  156. , mapper =
  157. es.map(function (data, callback) {
  158. callback(err)
  159. })
  160. mapper.on('error', function (_err) {
  161. it(_err).equal(err)
  162. test.done()
  163. })
  164. mapper.write('hello')
  165. }
  166. exports ['do not emit drain if not paused'] = function (test) {
  167. var map = es.map(function (data, callback) {
  168. u.delay(callback)(null, 1)
  169. return true
  170. })
  171. spec(map).through().pausable().validateOnExit()
  172. map.on('drain', function () {
  173. it(false).ok('should not emit drain unless the stream is paused')
  174. })
  175. it(map.write('hello')).equal(true)
  176. it(map.write('hello')).equal(true)
  177. it(map.write('hello')).equal(true)
  178. setTimeout(function () {map.end()},10)
  179. map.on('end', test.done)
  180. }
  181. exports ['emits drain if paused, when all '] = function (test) {
  182. var active = 0
  183. var drained = false
  184. var map = es.map(function (data, callback) {
  185. active ++
  186. u.delay(function () {
  187. active --
  188. callback(null, 1)
  189. })()
  190. console.log('WRITE', false)
  191. return false
  192. })
  193. spec(map).through().validateOnExit()
  194. map.on('drain', function () {
  195. drained = true
  196. it(active).equal(0, 'should emit drain when all maps are done')
  197. })
  198. it(map.write('hello')).equal(false)
  199. it(map.write('hello')).equal(false)
  200. it(map.write('hello')).equal(false)
  201. process.nextTick(function () {map.end()},10)
  202. map.on('end', function () {
  203. console.log('end')
  204. it(drained).ok('shoud have emitted drain before end')
  205. test.done()
  206. })
  207. }
  208. exports ['map applied to a stream with filtering'] = function (test) {
  209. var input = [1,2,3,7,5,3,1,9,0,2,4,6]
  210. var doubler = es.map(function (data, callback) {
  211. if (data % 2)
  212. callback(null, data * 2)
  213. else
  214. callback()
  215. })
  216. readStream(doubler, function (err, output) {
  217. it(output).deepEqual(input.filter(function (j) {
  218. return j % 2
  219. }).map(function (j) {
  220. return j * 2
  221. }))
  222. test.done()
  223. })
  224. spec(doubler).through().validateOnExit()
  225. writeArray(input, doubler)
  226. }
  227. exports ['simple mapSync applied to a stream'] = function (test) {
  228. var input = [1,2,3,7,5,3,1,9,0,2,4,6]
  229. var doubler = es.mapSync(function (data) {
  230. return data * 2
  231. })
  232. readStream(doubler, function (err, output) {
  233. it(output).deepEqual(input.map(function (j) {
  234. return j * 2
  235. }))
  236. test.done()
  237. })
  238. spec(doubler).through().validateOnExit()
  239. writeArray(input, doubler)
  240. }
  241. exports ['mapSync applied to a stream with filtering'] = function (test) {
  242. var input = [1,2,3,7,5,3,1,9,0,2,4,6]
  243. var doubler = es.mapSync(function (data) {
  244. if (data % 2)
  245. return data * 2
  246. })
  247. readStream(doubler, function (err, output) {
  248. it(output).deepEqual(input.filter(function (j) {
  249. return j % 2
  250. }).map(function (j) {
  251. return j * 2
  252. }))
  253. test.done()
  254. })
  255. spec(doubler).through().validateOnExit()
  256. writeArray(input, doubler)
  257. }
  258. require('./helper')(module)