UNPKG

1.11 kBJavaScriptView Raw
1"use strict";
2
3var BOMChar = '\uFEFF';
4
5exports.PrependBOM = PrependBOMWrapper
6function PrependBOMWrapper(encoder, options) {
7 this.encoder = encoder;
8 this.addBOM = true;
9}
10
11PrependBOMWrapper.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
20PrependBOMWrapper.prototype.end = function() {
21 return this.encoder.end();
22}
23
24
25//------------------------------------------------------------------------------
26
27exports.StripBOM = StripBOMWrapper;
28function StripBOMWrapper(decoder, options) {
29 this.decoder = decoder;
30 this.pass = false;
31 this.options = options || {};
32}
33
34StripBOMWrapper.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
49StripBOMWrapper.prototype.end = function() {
50 return this.decoder.end();
51}
52