Skip to content

Commit

Permalink
update!: Remove is-utf8 and change to take encoding as an argument
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk committed Apr 9, 2022
1 parent 488fd14 commit 807d904
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var concat = require('concat-stream');
var removeBOM = require('remove-bom-stream');

fs.createReadStream('utf8-file-with-bom.txt')
.pipe(removeBOM())
.pipe(removeBOM('utf-8'))
.pipe(
concat(function (result) {
// result won't have a BOM
Expand All @@ -28,9 +28,10 @@ fs.createReadStream('utf8-file-with-bom.txt')

## API

### `removeBOM()`
### `removeBOM(encoding)`

Returns a `through2` stream that will remove a BOM, if `encoding` argument is `'utf-8'` and given the data is a UTF8 Buffer with a BOM at the beginning. If `encoding` is not `'utf-8'` or does not have a BOM, the data is not changed and this becomes a normal passthrough stream.

Returns a `through2` stream that will remove a BOM, given the data is a UTF8 Buffer with a BOM at the beginning. If the data is not UTF8 or does not have a BOM, the data is not changed and this becomes a normal passthrough stream.

## License

Expand Down
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

var through = require('through2');
var isUTF8 = require('is-utf8');

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

function removeBomStream() {
function removeBomStream(encoding) {
encoding = (encoding || '').toLowerCase();
var isUtf8 = (encoding === 'utf8' || encoding === 'utf-8');

var completed = false;
var buffer = Buffer.alloc(0);

Expand All @@ -16,7 +18,7 @@ function removeBomStream() {

buffer = null;

if (isUTF8(data)) {
if (isUtf8) {
return removeBom.decode(data);
}
return data;
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"test": "nyc mocha --async-only"
},
"dependencies": {
"is-utf8": "^0.2.1",
"through2": "^4.0.2"
},
"devDependencies": {
Expand Down
14 changes: 7 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('removeBomStream', function () {
}

pipe(
[fs.createReadStream(filepath), removeBomStream(), concat(assert)],
[fs.createReadStream(filepath), removeBomStream('utf8'), concat(assert)],
done
);
});
Expand All @@ -39,7 +39,7 @@ describe('removeBomStream', function () {
}

pipe(
[fs.createReadStream(filepath), removeBomStream(), concat(assert)],
[fs.createReadStream(filepath), removeBomStream('utf-8'), concat(assert)],
done
);
});
Expand All @@ -57,7 +57,7 @@ describe('removeBomStream', function () {
[
fs.createReadStream(filepath),
chunker(1),
removeBomStream(),
removeBomStream('UTF8'),
concat(assert),
],
done
Expand All @@ -76,7 +76,7 @@ describe('removeBomStream', function () {
}

pipe(
[fs.createReadStream(filepath), removeBomStream(), concat(assert)],
[fs.createReadStream(filepath), removeBomStream('UTF-8'), concat(assert)],
done
);
});
Expand All @@ -91,7 +91,7 @@ describe('removeBomStream', function () {
}

pipe(
[fs.createReadStream(filepath), removeBomStream(), concat(assert)],
[fs.createReadStream(filepath), removeBomStream('utf-16be'), concat(assert)],
done
);
});
Expand All @@ -106,7 +106,7 @@ describe('removeBomStream', function () {
}

pipe(
[fs.createReadStream(filepath), removeBomStream(), concat(assert)],
[fs.createReadStream(filepath), removeBomStream('utf-16be'), concat(assert)],
done
);
});
Expand All @@ -121,7 +121,7 @@ describe('removeBomStream', function () {
}

pipe(
[fs.createReadStream(filepath), removeBomStream(), concat(assert)],
[fs.createReadStream(filepath), removeBomStream('utf-16le'), concat(assert)],
done
);
});
Expand Down

0 comments on commit 807d904

Please sign in to comment.