Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript definition #18

Merged
merged 1 commit into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/// <reference types="node"/>
import * as stream from 'stream';
import {ZlibOptions} from 'zlib';

declare namespace gzipSize {
type Options = ZlibOptions;

interface GzipSizeStream extends stream.PassThrough {
addListener(event: 'gzip-size', listener: (size: number) => void): this;
addListener(
event: string | symbol,
listener: (...args: any[]) => void
): this;
on(event: 'gzip-size', listener: (size: number) => void): this;
on(event: string | symbol, listener: (...args: any[]) => void): this;
once(event: 'gzip-size', listener: (size: number) => void): this;
once(event: string | symbol, listener: (...args: any[]) => void): this;
removeListener(event: 'gzip-size', listener: (size: number) => void): this;
removeListener(
event: string | symbol,
listener: (...args: any[]) => void
): this;
off(event: 'gzip-size', listener: (size: number) => void): this;
off(event: string | symbol, listener: (...args: any[]) => void): this;
emit(event: 'gzip-size', size: number): boolean;
emit(event: string | symbol, ...args: any[]): boolean;
prependListener(event: 'gzip-size', listener: (size: number) => void): this;
prependListener(
event: string | symbol,
listener: (...args: any[]) => void
): this;
prependOnceListener(
event: 'gzip-size',
listener: (size: number) => void
): this;
prependOnceListener(
event: string | symbol,
listener: (...args: any[]) => void
): this;

/**
Contains the gzip size of the stream after it is finished. Since this happens asynchronously, it is recommended you use the `gzip-size` event instead.
*/
gzipSize?: number;
}
}

declare const gzipSize: {
/**
Get the gzipped size of a string or buffer.

@returns The gzipped size of `input`.
*/
(input: string | Buffer, options?: gzipSize.Options): Promise<number>;

/**
Synchronously get the gzipped size of a string or buffer.

@returns The gzipped size of `input`.

@example
```
import gzipSize = require('gzip-size');

const text = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.';

console.log(text.length);
//=> 191

console.log(gzipSize.sync(text));
//=> 78
```
*/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to get one example to show for multiple methods without copy-pasting it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None that I know of.

sync(input: string | Buffer, options?: gzipSize.Options): number;

/**
@returns A stream that emits a `gzip-size` event and has a `gzipSize` property.
*/
stream(options?: gzipSize.Options): gzipSize.GzipSizeStream;

/**
Get the gzipped size of a file.

@returns The size of the file.
*/
file(path: string, options?: gzipSize.Options): Promise<number>;
};

export = gzipSize;
16 changes: 16 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {expectType} from 'tsd';
import * as fs from 'fs';
import gzipSize = require('.');

expectType<Promise<number>>(gzipSize('foo'));
expectType<Promise<number>>(gzipSize('foo', {chunkSize: 1}));
expectType<number>(gzipSize.sync('foo'));
expectType<number>(gzipSize.sync('foo', {chunkSize: 1}));
const gstream = fs
.createReadStream('index.d.ts')
.pipe(gzipSize.stream())
.pipe(gzipSize.stream({chunkSize: 1}))
.on('gzip-size', size => expectType<number>(size));
expectType<number | undefined>(gstream.gzipSize);
expectType<Promise<number>>(gzipSize.file('index.d.ts'));
expectType<Promise<number>>(gzipSize.file('index.d.ts', {chunkSize: 1}));
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"app",
Expand All @@ -30,11 +31,12 @@
],
"dependencies": {
"duplexer": "^0.1.1",
"pify": "^3.0.0"
"pify": "^4.0.1"
},
"devDependencies": {
"ava": "*",
"ava": "^1.4.1",
"p-event": "^2.1.0",
"xo": "*"
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
28 changes: 14 additions & 14 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import fs from 'fs';
import test from 'ava';
import pEvent from 'p-event';
import m from '.';
import gzipSize from '.';

const fixture = fs.readFileSync('test.js', 'utf8');

test('get the gzipped size', async t => {
t.true(await m(fixture) < fixture.length);
t.true(await gzipSize(fixture) < fixture.length);
});

test('gzip compression level', async t => {
t.true(await m(fixture, {level: 6}) < await m(fixture, {level: 1}));
t.true(await gzipSize(fixture, {level: 6}) < await gzipSize(fixture, {level: 1}));
});

test('sync - get the gzipped size', t => {
t.true(m.sync(fixture) < fixture.length);
t.true(gzipSize.sync(fixture) < fixture.length);
});

test('sync - match async version', async t => {
t.is(m.sync(fixture), await m(fixture));
t.is(gzipSize.sync(fixture), await gzipSize(fixture));
});

test('sync - gzip compression level', t => {
t.true(m.sync(fixture, {level: 6}) < m.sync(fixture, {level: 1}));
t.true(gzipSize.sync(fixture, {level: 6}) < gzipSize.sync(fixture, {level: 1}));
});

test('stream', async t => {
const stream = fs.createReadStream('test.js').pipe(m.stream());
const stream = fs.createReadStream('test.js').pipe(gzipSize.stream());
await pEvent(stream, 'end');
t.is(stream.gzipSize, m.sync(fixture));
t.is(stream.gzipSize, gzipSize.sync(fixture));
});

test('gzip-size event', async t => {
const stream = fs.createReadStream('test.js').pipe(m.stream());
const stream = fs.createReadStream('test.js').pipe(gzipSize.stream());
const size = await pEvent(stream, 'gzip-size');
t.is(size, m.sync(fixture));
t.is(size, gzipSize.sync(fixture));
});

test('passthrough', async t => {
let out = '';

const stream = fs.createReadStream('test.js')
.pipe(m.stream())
.pipe(gzipSize.stream())
.on('data', buffer => {
out += buffer;
});
Expand All @@ -51,13 +51,13 @@ test('passthrough', async t => {
});

test('file - get the gzipped size', async t => {
t.true(await m.file('test.js') < fixture.length);
t.true(await gzipSize.file('test.js') < fixture.length);
});

test('fileSync - get the gzipped size', t => {
t.is(m.fileSync('test.js'), m.sync(fixture));
t.is(gzipSize.fileSync('test.js'), gzipSize.sync(fixture));
});

test('file - match async version', async t => {
t.is(await m.file('test.js'), await m(fixture));
t.is(await gzipSize.file('test.js'), await gzipSize(fixture));
});