-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05b22f7
commit 12a8028
Showing
7 changed files
with
39 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
import {Buffer} from 'node:buffer'; | ||
import isUtf8 from 'is-utf8'; | ||
import {assertUint8Array} from 'uint8array-extras'; | ||
|
||
export default function strimBomBuffer(buffer) { | ||
if (!Buffer.isBuffer(buffer)) { | ||
throw new TypeError(`Expected a \`Buffer\`, got \`${typeof buffer}\``); | ||
} | ||
export default function stripBomBuffer(byteArray) { | ||
assertUint8Array(byteArray); | ||
|
||
if (buffer[0] === 0xEF && buffer[1] === 0xBB && buffer[2] === 0xBF && isUtf8(buffer)) { | ||
return buffer.slice(3); | ||
if (byteArray[0] === 0xEF && byteArray[1] === 0xBB && byteArray[2] === 0xBF && isUtf8(byteArray)) { | ||
return byteArray.slice(3); | ||
} | ||
|
||
return buffer; | ||
return byteArray; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import fs from 'node:fs'; | ||
import {Buffer} from 'node:buffer'; | ||
import {expectType} from 'tsd'; | ||
import stripBomBuffer from './index.js'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
expectType<Buffer>(stripBomBuffer(fs.readFileSync('unicorn.txt'))); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
expectType<Uint8Array>(stripBomBuffer(new Uint8Array(fs.readFileSync('unicorn.txt')))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
import fs from 'node:fs'; | ||
import {Buffer} from 'node:buffer'; | ||
import {fileURLToPath} from 'node:url'; | ||
import path from 'node:path'; | ||
import test from 'ava'; | ||
import {stringToUint8Array} from 'uint8array-extras'; | ||
import stripBomBuffer from '../index.js'; | ||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
||
test('strips BOM from UTF-8 buffer', t => { | ||
const fixture = fs.readFileSync(path.join(__dirname, 'fixture-utf8')); | ||
t.true(stripBomBuffer(fixture).equals(Buffer.from('Unicorn\n'))); | ||
const fixture = new Uint8Array(fs.readFileSync(path.join(__dirname, 'fixture-utf8'))); | ||
t.deepEqual(stripBomBuffer(fixture), stringToUint8Array('Unicorn\n')); | ||
}); | ||
|
||
test('doesn\'t strip anything that looks like a UTF-8-encoded BOM from UTF16LE', t => { | ||
const fixture = fs.readFileSync(path.join(__dirname, 'fixture-utf16le')); | ||
t.is(stripBomBuffer(fixture), fixture); | ||
const fixture = new Uint8Array(fs.readFileSync(path.join(__dirname, 'fixture-utf16le'))); | ||
t.deepEqual(stripBomBuffer(fixture), fixture); | ||
}); | ||
|
||
test('doesn\'t strip anything that looks like a UTF-8-encoded BOM from UTF16BE', t => { | ||
const fixture = fs.readFileSync(path.join(__dirname, 'fixture-utf16be')); | ||
t.is(stripBomBuffer(fixture), fixture); | ||
const fixture = new Uint8Array(fs.readFileSync(path.join(__dirname, 'fixture-utf16be'))); | ||
t.deepEqual(stripBomBuffer(fixture), fixture); | ||
}); |