-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
sindresorhus
merged 1 commit into
sindresorhus:master
from
BendingBender:typescript-defs
Apr 15, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
*/ | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.