Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //filter will reemit the data if cb(err,pass) pass is truthy
  2. // reduce is more tricky
  3. // maybe we want to group the reductions or emit progress updates occasionally
  4. // the most basic reduce just emits one 'data' event after it has recieved 'end'
  5. var through = require('through')
  6. var Decoder = require('string_decoder').StringDecoder
  7. module.exports = split
  8. //TODO pass in a function to map across the lines.
  9. function split (matcher, mapper, options) {
  10. var decoder = new Decoder()
  11. var soFar = ''
  12. var maxLength = options && options.maxLength;
  13. var trailing = options && options.trailing === false ? false : true
  14. if('function' === typeof matcher)
  15. mapper = matcher, matcher = null
  16. if (!matcher)
  17. matcher = /\r?\n/
  18. function emit(stream, piece) {
  19. if(mapper) {
  20. try {
  21. piece = mapper(piece)
  22. }
  23. catch (err) {
  24. return stream.emit('error', err)
  25. }
  26. if('undefined' !== typeof piece)
  27. stream.queue(piece)
  28. }
  29. else
  30. stream.queue(piece)
  31. }
  32. function next (stream, buffer) {
  33. var pieces = ((soFar != null ? soFar : '') + buffer).split(matcher)
  34. soFar = pieces.pop()
  35. if (maxLength && soFar.length > maxLength)
  36. return stream.emit('error', new Error('maximum buffer reached'))
  37. for (var i = 0; i < pieces.length; i++) {
  38. var piece = pieces[i]
  39. emit(stream, piece)
  40. }
  41. }
  42. return through(function (b) {
  43. next(this, decoder.write(b))
  44. },
  45. function () {
  46. if(decoder.end)
  47. next(this, decoder.end())
  48. if(trailing && soFar != null)
  49. emit(this, soFar)
  50. this.queue(null)
  51. })
  52. }