Skip to content

Commit

Permalink
Fix usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Sep 2, 2021
1 parent 80797e7 commit acc266e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 24 deletions.
13 changes: 8 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ Check if a Buffer/Uint8Array is a [PNG](https://en.wikipedia.org/wiki/Portable_N
@example
```
// Node.js:
import readChunk from 'read-chunk';
import {readChunk} from 'read-chunk';
import isPng from 'is-png';
const buffer = readChunk.sync('unicorn.png', 0, 8);
const buffer = await readChunk('unicorn.png', {length: 8});
isPng(buffer);
//=> true
```
// Browser:
import readChunk from 'read-chunk';
@example
```
import isPng from 'is-png';
// Browser:
const response = await fetch('unicorn.png');
const buffer = await response.arrayBuffer();
isPng(new Uint8Array(buffer));
//=> true
```
*/
export default function isPng(buffer: Uint8Array | Buffer): boolean;
export default function isPng(buffer: Uint8Array): boolean;
18 changes: 8 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ export default function isPng(buffer) {
return false;
}

return (
buffer[0] === 0x89 &&
buffer[1] === 0x50 &&
buffer[2] === 0x4E &&
buffer[3] === 0x47 &&
buffer[4] === 0x0D &&
buffer[5] === 0x0A &&
buffer[6] === 0x1A &&
buffer[7] === 0x0A
);
return buffer[0] === 0x89
&& buffer[1] === 0x50
&& buffer[2] === 0x4E
&& buffer[3] === 0x47
&& buffer[4] === 0x0D
&& buffer[5] === 0x0A
&& buffer[6] === 0x1A
&& buffer[7] === 0x0A;
}
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference lib="dom"/>
import {Buffer} from 'node:buffer';
import {expectType} from 'tsd';
import isPng from './index.js';

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
"uint8array"
],
"devDependencies": {
"@types/node": "^14.14.41",
"@types/node": "^16.7.10",
"ava": "^3.15.0",
"read-chunk": "^3.2.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
"read-chunk": "^4.0.1",
"tsd": "^0.17.0",
"xo": "^0.44.0"
}
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ $ npm install is-png
##### Node.js

```js
import readChunk from 'read-chunk'; // npm install read-chunk
import {readChunk} from 'read-chunk';
import isPng from 'is-png';

const buffer = readChunk.sync('unicorn.png', 0, 8);
const buffer = await readChunk('unicorn.png', {length: 8});

isPng(buffer);
//=> true
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import test from 'ava';
import readChunk from 'read-chunk';
import {readChunkSync} from 'read-chunk';
import isPng from './index.js';

const check = filename => isPng(readChunk.sync(filename, 0, 8));
const check = filename => isPng(readChunkSync(filename, {length: 8}));

test('main', t => {
t.true(check('fixture.png'));
Expand Down

0 comments on commit acc266e

Please sign in to comment.