le-unix.js 961 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. const stream = require('stream');
  3. const Transform = stream.Transform;
  4. /**
  5. * Ensures that only <LF> is used for linebreaks
  6. *
  7. * @param {Object} options Stream options
  8. */
  9. class LeWindows extends Transform {
  10. constructor(options) {
  11. super(options);
  12. // init Transform
  13. this.options = options || {};
  14. }
  15. /**
  16. * Escapes dots
  17. */
  18. _transform(chunk, encoding, done) {
  19. let buf;
  20. let lastPos = 0;
  21. for (let i = 0, len = chunk.length; i < len; i++) {
  22. if (chunk[i] === 0x0d) {
  23. // \n
  24. buf = chunk.slice(lastPos, i);
  25. lastPos = i + 1;
  26. this.push(buf);
  27. }
  28. }
  29. if (lastPos && lastPos < chunk.length) {
  30. buf = chunk.slice(lastPos);
  31. this.push(buf);
  32. } else if (!lastPos) {
  33. this.push(chunk);
  34. }
  35. done();
  36. }
  37. }
  38. module.exports = LeWindows;