bom-handling.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. var BOMChar = '\uFEFF';
  3. exports.PrependBOM = PrependBOMWrapper
  4. function PrependBOMWrapper(encoder, options) {
  5. this.encoder = encoder;
  6. this.addBOM = true;
  7. }
  8. PrependBOMWrapper.prototype.write = function(str) {
  9. if (this.addBOM) {
  10. str = BOMChar + str;
  11. this.addBOM = false;
  12. }
  13. return this.encoder.write(str);
  14. }
  15. PrependBOMWrapper.prototype.end = function() {
  16. return this.encoder.end();
  17. }
  18. //------------------------------------------------------------------------------
  19. exports.StripBOM = StripBOMWrapper;
  20. function StripBOMWrapper(decoder, options) {
  21. this.decoder = decoder;
  22. this.pass = false;
  23. this.options = options || {};
  24. }
  25. StripBOMWrapper.prototype.write = function(buf) {
  26. var res = this.decoder.write(buf);
  27. if (this.pass || !res)
  28. return res;
  29. if (res[0] === BOMChar) {
  30. res = res.slice(1);
  31. if (typeof this.options.stripBOM === 'function')
  32. this.options.stripBOM();
  33. }
  34. this.pass = true;
  35. return res;
  36. }
  37. StripBOMWrapper.prototype.end = function() {
  38. return this.decoder.end();
  39. }