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