replace.asynct.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var es = require('../')
  2. , it = require('it-is').style('colour')
  3. , d = require('ubelt')
  4. , spec = require('stream-spec')
  5. var next = process.nextTick
  6. var fizzbuzz = '12F4BF78FB11F1314FB1617F19BF2223FB26F2829FB3132F34BF3738FB41F4344FB4647F49BF5253FB56F5859FB6162F64BF6768FB71F7374FB7677F79BF8283FB86F8889FB9192F94BF9798FB'
  7. , fizz7buzz = '12F4BFseven8FB11F1314FB161sevenF19BF2223FB26F2829FB3132F34BF3seven38FB41F4344FB464sevenF49BF5253FB56F5859FB6162F64BF6seven68FBseven1Fseven3seven4FBseven6sevensevenFseven9BF8283FB86F8889FB9192F94BF9seven98FB'
  8. , fizzbuzzwhitespce = ' 12F4BF78FB11F1314FB1617F19BF2223FB26F2829FB3132F34BF3738FB41F4344FB4647F49BF5253FB56F5859FB6162F64BF6768FB71F7374FB7677F79BF8283FB86F8889FB9192F94BF9798FB '
  9. exports ['fizz buzz'] = function (test) {
  10. var readThis = d.map(1, 100, function (i) {
  11. return (
  12. ! (i % 3 || i % 5) ? "FB" :
  13. !(i % 3) ? "F" :
  14. !(i % 5) ? "B" :
  15. ''+i
  16. )
  17. }) //array of multiples of 3 < 100
  18. var reader = es.readArray(readThis)
  19. var join = es.wait(function (err, string){
  20. it(string).equal(fizzbuzz)
  21. test.done()
  22. })
  23. reader.pipe(join)
  24. }
  25. exports ['fizz buzz replace'] = function (test) {
  26. var split = es.split(/(1)/)
  27. var replace = es.replace('7', 'seven')
  28. // var x = spec(replace).through()
  29. split
  30. .pipe(replace)
  31. .pipe(es.join(function (err, string) {
  32. it(string).equal(fizz7buzz)
  33. }))
  34. replace.on('close', function () {
  35. // x.validate()
  36. test.done()
  37. })
  38. split.write(fizzbuzz)
  39. split.end()
  40. }
  41. exports ['fizz buzz replace whitespace using regexp'] = function (test) {
  42. var split = es.split(/(1)/)
  43. var replaceLeading = es.replace(/^[\s]*/, '')
  44. var replaceTrailing = es.replace(/[\s]*$/, '')
  45. // var x = spec(replace).through()
  46. split
  47. .pipe(replaceLeading)
  48. .pipe(replaceTrailing)
  49. .pipe(es.join(function (err, string) {
  50. it(string).equal(fizzbuzz)
  51. }))
  52. replaceTrailing.on('close', function () {
  53. // x.validate()
  54. test.done()
  55. })
  56. split.write(fizzbuzz)
  57. split.end()
  58. }
  59. require('./helper')(module)