Skip to content

Commit c2c0d30

Browse files
committedSep 18, 2019
Add TypeScript definition
1 parent aa5c387 commit c2c0d30

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed
 

‎index.d.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// <reference types="node"/>
2+
import {Metadata, PNG, PNGOptions} from 'pngjs';
3+
4+
/**
5+
Parse a PNG.
6+
7+
@param buffer - A PNG image buffer.
8+
@param options - See the [pngjs options](https://github.com/lukeapage/pngjs#options).
9+
@returns The parsed PNG image.
10+
11+
@example
12+
```
13+
import * as fs from 'fs';
14+
import parsePng = require('parse-png');
15+
16+
(async () => {
17+
const png = await parsePng(fs.readFileSync('unicorn.png'));
18+
19+
png.adjustGamma();
20+
png.pack().pipe(fs.createWriteStream('unicorn-adjusted.png'));
21+
})();
22+
```
23+
*/
24+
declare function parsePng(
25+
buffer: Buffer,
26+
options?: PNGOptions
27+
): Promise<PNG & Metadata>;
28+
29+
export = parsePng;

‎index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
22
const {PNG} = require('pngjs');
33

4-
module.exports = async (buffer, opts) => {
4+
module.exports = async (buffer, options) => {
55
if (!Buffer.isBuffer(buffer)) {
66
throw new TypeError(`Expected \`buffer\` to be of type \`Buffer\` but received type \`${typeof buffer}\``);
77
}
88

99
return new Promise((resolve, reject) => {
10-
let png = new PNG(opts);
10+
let png = new PNG(options);
1111

1212
png.on('metadata', data => {
1313
png = Object.assign(png, data);

‎index.test-d.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference types="node"/>
2+
import {expectError, expectType} from 'tsd';
3+
import {Metadata, PNG, PNGOptions} from 'pngjs';
4+
import parsePng = require('.');
5+
6+
expectType<Promise<PNG & Metadata>>(parsePng(Buffer.from('foo')));
7+
expectType<Promise<PNG & Metadata>>(
8+
parsePng(Buffer.from('foo'), {
9+
width: 0,
10+
bgColor: {red: 0, green: 0, blue: 0}
11+
})
12+
);
13+
expectError(parsePng(Buffer.from('foo'), {foo: 'bar'}));

‎package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
"node": ">=10"
1414
},
1515
"scripts": {
16-
"test": "xo && ava"
16+
"test": "xo && ava && tsd"
1717
},
1818
"files": [
19-
"index.js"
19+
"index.js",
20+
"index.d.ts"
2021
],
2122
"keywords": [
2223
"parse",
@@ -27,9 +28,12 @@
2728
"pngjs": "^3.3.0"
2829
},
2930
"devDependencies": {
31+
"@types/node": "^12.7.5",
32+
"@types/pngjs": "^3.3.2",
3033
"ava": "^2.4.0",
3134
"file-type": "^7.2.0",
3235
"get-stream": "^3.0.0",
36+
"tsd": "^0.7.4",
3337
"xo": "^0.24.0"
3438
}
3539
}

0 commit comments

Comments
 (0)
Please sign in to comment.