Skip to content

Commit

Permalink
Add TypeScript definition (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanWebb authored and sindresorhus committed Jan 11, 2019
1 parent 0e0edd2 commit b2ad077
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
64 changes: 64 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
export interface Options {
/**
* Speed `10` has 5% lower quality, but is about 8 times faster than the default. Speed `11` disables dithering and lowers compression level.
*
* Values: `1` (brute-force) to `11` (fastest)
*
* @default 3
*/
speed?: number;

/**
* Remove optional metadata.
*
* @default false
*/
strip?: boolean;

/**
* Instructs pngquant to use the least amount of colors required to meet or exceed the max quality. If conversion results in quality below the min quality the image won't be saved.
* Min and max are numbers in range 0 (worst) to 1 (perfect), similar to JPEG.
*
* Values: `[0...1, 0...1]`
*
* @example [0.3, 0.5]
*/
quality?: [number, number];

/**
* Set the dithering level using a fractional number between 0 (none) and 1 (full).
* Pass in `false` to disable dithering.
*
* Values: 0...1
*
* @default 1
*/
dithering?: number | boolean;

/**
* Truncate number of least significant bits of color (per channel).
* Use this when image will be output on low-depth displays (e.g. 16-bit RGB). pngquant will make almost-opaque pixels fully opaque and will reduce amount of semi-transparent colors.
*/
posterize?: number;

/**
* Print verbose status messages.
*
* @default false
*/
verbose?: boolean;
}

/**
* Buffer or stream to optimize.
*
* @returns A `Promise` for a `Buffer`.
*/
export type Plugin = (input: Buffer | NodeJS.ReadableStream) => Promise<Buffer>

/**
* Imagemin plugin for pngquant.
*
* @returns An Imagemin plugin.
*/
export default function imageminPngquant(options?: Options): Plugin;
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const isStream = require('is-stream');
const pngquant = require('pngquant-bin');
const ow = require('ow');

module.exports = (options = {}) => input => {
const imageminPngquant = (options = {}) => input => {
const isBuffer = Buffer.isBuffer(input);

if (!isBuffer && !isStream(input)) {
Expand Down Expand Up @@ -76,3 +76,6 @@ module.exports = (options = {}) => input => {

return cp.stdout;
};

module.exports = imageminPngquant;
module.exports.default = imageminPngquant;
12 changes: 12 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as fs from 'fs';
import * as path from 'path';
import {expectType} from 'tsd-check';
import imageminPngquant from '.';

const buffer = await fs.readFileSync(path.join(__dirname, 'fixture.png'));

expectType<Buffer>(await imageminPngquant()(buffer));
expectType<Buffer>(await imageminPngquant({
speed: 10,
quality: [0.8, 1]
})(buffer));
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"compress",
Expand All @@ -31,8 +32,10 @@
"pngquant-bin": "^5.0.0"
},
"devDependencies": {
"@types/node": "^10.12.18",
"ava": "^1.0.1",
"get-stream": "^4.1.0",
"tsd-check": "^0.3.0",
"xo": "^0.23.0"
}
}

0 comments on commit b2ad077

Please sign in to comment.