Skip to content

Commit

Permalink
Require Node.js 18
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 24, 2023
1 parent 05b22f7 commit 12a8028
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 37 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 20
- 18
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
6 changes: 2 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {Buffer} from 'node:buffer';

/**
Strip UTF-8 [byte order mark](http://en.wikipedia.org/wiki/Byte_order_mark#UTF-8) (BOM) from a buffer.
Strip UTF-8 [byte order mark](http://en.wikipedia.org/wiki/Byte_order_mark#UTF-8) (BOM) from a `Uint8Array`.
@example
```
Expand All @@ -12,4 +10,4 @@ stripBomBuffer(fs.readFileSync('unicorn.txt'));
//=> 'unicorn'
```
*/
export default function stripBomBuffer(buffer: Buffer): Buffer;
export default function stripBomBuffer(byteArray: Uint8Array): Uint8Array;
14 changes: 6 additions & 8 deletions index.js
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;
}
5 changes: 2 additions & 3 deletions index.test-d.ts
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'))));
24 changes: 15 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "strip-bom-buf",
"version": "3.0.1",
"description": "Strip UTF-8 byte order mark (BOM) from a buffer",
"description": "Strip UTF-8 byte order mark (BOM) from a Uint8Array",
"license": "MIT",
"repository": "sindresorhus/strip-bom-buf",
"funding": "https://github.com/sponsors/sindresorhus",
Expand All @@ -11,10 +11,14 @@
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
"node": ">=18"
},
"sideEffects": false,
"scripts": {
"test": "xo && ava && tsd"
},
Expand All @@ -35,15 +39,17 @@
"delete",
"trim",
"text",
"buffer"
"buffer",
"uint8array"
],
"dependencies": {
"is-utf8": "^0.2.1"
"is-utf8": "^0.2.1",
"uint8array-extras": "^0.3.0"
},
"devDependencies": {
"@types/node": "^16.6.1",
"ava": "^3.15.0",
"tsd": "^0.17.0",
"xo": "^0.44.0"
"@types/node": "^20.8.8",
"ava": "^5.3.1",
"tsd": "^0.29.0",
"xo": "^0.56.0"
}
}
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# strip-bom-buf

> Strip UTF-8 [byte order mark](http://en.wikipedia.org/wiki/Byte_order_mark#UTF-8) (BOM) from a buffer
> Strip UTF-8 [byte order mark](http://en.wikipedia.org/wiki/Byte_order_mark#UTF-8) (BOM) from a `Uint8Array`
From Wikipedia:

> The Unicode Standard permits the BOM in UTF-8, but does not require nor recommend its use. Byte order has no meaning in UTF-8.
## Install

```
$ npm install strip-bom-buf
```sh
npm install strip-bom-buf
```

## Usage
Expand Down
14 changes: 7 additions & 7 deletions test/test.js
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);
});

0 comments on commit 12a8028

Please sign in to comment.