From 394390bf3432dfdfb4fd7436f15ec06f72f3e319 Mon Sep 17 00:00:00 2001 From: sttk Date: Sat, 9 Apr 2022 19:20:42 +0900 Subject: [PATCH] update!: Remove is-utf8 and change to take encoding as an argument --- README.md | 7 ++++--- index.js | 9 ++++++--- package.json | 1 - test/index.js | 14 +++++++------- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index b9a6e4e..4353783 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/index.js b/index.js index 59be8e9..7db33bf 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,14 @@ 'use strict'; var through = require('through2'); -var isUTF8 = require('is-utf8'); +var TextDecoder = require('util').TextDecoder; 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); @@ -16,7 +19,7 @@ function removeBomStream() { buffer = null; - if (isUTF8(data)) { + if (isUtf8) { return removeBom.decode(data); } return data; diff --git a/package.json b/package.json index 7dc04ac..be77b04 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ "test": "nyc mocha --async-only" }, "dependencies": { - "is-utf8": "^0.2.1", "through2": "^4.0.2" }, "devDependencies": { diff --git a/test/index.js b/test/index.js index fb51e29..9c9756f 100644 --- a/test/index.js +++ b/test/index.js @@ -24,7 +24,7 @@ describe('removeBomStream', function () { } pipe( - [fs.createReadStream(filepath), removeBomStream(), concat(assert)], + [fs.createReadStream(filepath), removeBomStream('utf8'), concat(assert)], done ); }); @@ -39,7 +39,7 @@ describe('removeBomStream', function () { } pipe( - [fs.createReadStream(filepath), removeBomStream(), concat(assert)], + [fs.createReadStream(filepath), removeBomStream('utf-8'), concat(assert)], done ); }); @@ -57,7 +57,7 @@ describe('removeBomStream', function () { [ fs.createReadStream(filepath), chunker(1), - removeBomStream(), + removeBomStream('UTF8'), concat(assert), ], done @@ -76,7 +76,7 @@ describe('removeBomStream', function () { } pipe( - [fs.createReadStream(filepath), removeBomStream(), concat(assert)], + [fs.createReadStream(filepath), removeBomStream('UTF-8'), concat(assert)], done ); }); @@ -91,7 +91,7 @@ describe('removeBomStream', function () { } pipe( - [fs.createReadStream(filepath), removeBomStream(), concat(assert)], + [fs.createReadStream(filepath), removeBomStream('utf-16be'), concat(assert)], done ); }); @@ -106,7 +106,7 @@ describe('removeBomStream', function () { } pipe( - [fs.createReadStream(filepath), removeBomStream(), concat(assert)], + [fs.createReadStream(filepath), removeBomStream('utf-16be'), concat(assert)], done ); }); @@ -121,7 +121,7 @@ describe('removeBomStream', function () { } pipe( - [fs.createReadStream(filepath), removeBomStream(), concat(assert)], + [fs.createReadStream(filepath), removeBomStream('utf-16le'), concat(assert)], done ); });