Skip to content

Commit

Permalink
update: Use TextDecoder to remove BOM from a string
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk committed Apr 9, 2022
1 parent 5e63630 commit 488fd14
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
var through = require('through2');
var isUTF8 = require('is-utf8');

var removeBom = new TextDecoder('utf-8', { ignoreBOM: false });

function removeBomStream() {
var completed = false;
var buffer = Buffer.alloc(0);
Expand All @@ -14,7 +16,10 @@ function removeBomStream() {

buffer = null;

return removeBom(data);
if (isUTF8(data)) {
return removeBom.decode(data);
}
return data;
}

function onChunk(data, enc, cb) {
Expand Down Expand Up @@ -47,19 +52,4 @@ function removeBomStream() {
}
}

function removeBom(buf) {
if (Buffer.isBuffer(buf) && matchBOM(buf) && maybeUTF8(buf)) {
return buf.slice(3);
}
return buf;
}

function matchBOM(buf) {
return buf[0] === 0xEF && buf[1] === 0xBB && buf[2] === 0xBF;
}

function maybeUTF8(buf) {
return isUTF8(buf.slice(3, 7));
}

module.exports = removeBomStream;

0 comments on commit 488fd14

Please sign in to comment.