Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 15, 2021
1 parent 0cd5f76 commit 8e648be
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 39 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
10 changes: 4 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/// <reference types="node"/>
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.
@example
```
import * as fs from 'fs';
import stripBomBuffer = require('strip-bom-buf');
import fs from 'node:fs';
import stripBomBuffer from 'strip-bom-buf';
stripBomBuffer(fs.readFileSync('unicorn.txt'));
//=> 'unicorn'
```
*/
declare function stripBomBuffer(buffer: Buffer): Buffer;

export = stripBomBuffer;
export default function stripBomBuffer(buffer: Buffer): Buffer;
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';
const isUtf8 = require('is-utf8');
import {Buffer} from 'node:buffer';
import isUtf8 from 'is-utf8';

module.exports = buffer => {
export default function strimBomBuffer(buffer) {
if (!Buffer.isBuffer(buffer)) {
throw new TypeError(`Expected a Buffer, got ${typeof buffer}`);
throw new TypeError(`Expected a \`Buffer\`, got \`${typeof buffer}\``);
}

if (buffer[0] === 0xEF && buffer[1] === 0xBB && buffer[2] === 0xBF && isUtf8(buffer)) {
return buffer.slice(3);
}

return buffer;
};
}
6 changes: 4 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as fs from 'fs';
import fs from 'node:fs';
import Buffer from 'node:buffer';
import {expectType} from 'tsd';
import stripBomBuffer = require('.');
import stripBomBuffer from './index.js';

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
expectType<Buffer>(stripBomBuffer(fs.readFileSync('unicorn.txt')));
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "Strip UTF-8 byte order mark (BOM) from a buffer",
"license": "MIT",
"repository": "sindresorhus/strip-bom-buf",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -38,9 +41,9 @@
"is-utf8": "^0.2.1"
},
"devDependencies": {
"@types/node": "^11.13.8",
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"@types/node": "^16.6.1",
"ava": "^3.15.0",
"tsd": "^0.17.0",
"xo": "^0.44.0"
}
}
12 changes: 2 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,23 @@ 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
```


## Usage

```js
const fs = require('fs');
const stripBomBuffer = require('strip-bom-buf');
import fs from 'node:fs';
import stripBomBuffer from 'strip-bom-buf';

stripBomBuffer(fs.readFileSync('unicorn.txt'));
//=> 'unicorn'
```


## Related

- [strip-bom](https://github.com/sindresorhus/strip-bom) - String version of this module
- [strip-bom-stream](https://github.com/sindresorhus/strip-bom-stream) - Stream version of this module


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
12 changes: 8 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import fs from 'fs';
import path from 'path';
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 stripBomBuffer from '..';
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'));
const fixture = fs.readFileSync(path.join(__dirname, 'fixture-utf8'));
t.true(stripBomBuffer(fixture).equals(Buffer.from('Unicorn\n')));
});

Expand Down

0 comments on commit 8e648be

Please sign in to comment.