Navigation Menu

Skip to content

Commit

Permalink
Add TypeScript definition (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 5, 2019
1 parent 6150eca commit afa45a3
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 14 deletions.
55 changes: 55 additions & 0 deletions index.d.ts
@@ -0,0 +1,55 @@
/// <reference types="node"/>
import {ImageOptions} from 'ansi-escapes';

declare class UnsupportedTerminalErrorClass extends Error {
readonly name: 'UnsupportedTerminalError';

constructor();
}

declare namespace termImg {
interface Options<FallbackType = unknown> extends ImageOptions {
/**
Enables you to do something else when the terminal doesn't support images.
@default () => throw new UnsupportedTerminalError()
*/
readonly fallback?: () => FallbackType;
}

type UnsupportedTerminalError = UnsupportedTerminalErrorClass;
}

declare const termImg: {
/**
Log the image to the terminal directly.
@param image - Filepath to an image or an image as a buffer.
@example
```
import termImg = require('term-img');
function fallback() {
// Do something else when not supported
}
termImg('unicorn.jpg', {fallback});
```
*/
(image: string | Buffer, options?: termImg.Options): void;

/**
Get the image as a `string` that you can log manually.
@param image - Filepath to an image or an image as a buffer.
*/
string<FallbackType = unknown>(
image: string | Buffer,
options?: termImg.Options<FallbackType>
): string | FallbackType;

UnsupportedTerminalError: typeof UnsupportedTerminalErrorClass;
};

export = termImg;
6 changes: 5 additions & 1 deletion index.js
Expand Up @@ -38,7 +38,7 @@ function main(image, options = {}) {
return ansiEscapes.image(image, options);
}

module.exports = (image, options) => {
const termImg = (image, options) => {
const ret = main(image, options);

if (typeof ret === 'function') {
Expand All @@ -49,6 +49,8 @@ module.exports = (image, options) => {
console.log(ret);
};

module.exports = termImg;

module.exports.string = (image, options) => {
const ret = main(image, options);

Expand All @@ -58,3 +60,5 @@ module.exports.string = (image, options) => {

return ret;
};

module.exports.UnsupportedTerminalError = UnsupportedTerminalError;
18 changes: 18 additions & 0 deletions index.test-d.ts
@@ -0,0 +1,18 @@
import {expectType} from 'tsd';
import termImg = require('.');
import {UnsupportedTerminalError} from '.';

expectType<void>(termImg('/foo/bar.jpg'));
expectType<void>(termImg(new Buffer(1)));
expectType<void>(termImg('/foo/bar.jpg', {width: 1}));
expectType<void>(termImg('/foo/bar.jpg', {fallback: () => false}));

expectType<string>(termImg.string('/foo/bar.jpg'));
expectType<string>(termImg.string(new Buffer(1)));
expectType<string>(termImg.string('/foo/bar.jpg', {width: 1}));
expectType<string | boolean>(
termImg.string('/foo/bar.jpg', {fallback: () => false})
);

const unsupportedTerminalError = new UnsupportedTerminalError();
expectType<UnsupportedTerminalError>(unsupportedTerminalError);
15 changes: 9 additions & 6 deletions package.json
Expand Up @@ -13,10 +13,11 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"term",
Expand All @@ -41,11 +42,13 @@
"jpeg"
],
"dependencies": {
"ansi-escapes": "^3.1.0",
"iterm2-version": "^4.0.0"
"ansi-escapes": "^4.1.0",
"iterm2-version": "^4.1.0"
},
"devDependencies": {
"ava": "^0.25.0",
"xo": "^0.23.0"
"@types/node": "^11.13.0",
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
10 changes: 5 additions & 5 deletions readme.md
Expand Up @@ -31,17 +31,17 @@ termImg('unicorn.jpg', {fallback});

## API

### termImg(input, [options])
### termImg(image, [options])

Log the image to the terminal directly.

### termImg.string(input, [options])
### termImg.string(image, [options])

Get the image as a `string` that you can log manually.

#### input
#### image

Type: `string` `Buffer`
Type: `string | Buffer`

Filepath to an image or an image as a buffer.

Expand All @@ -50,7 +50,7 @@ Filepath to an image or an image as a buffer.
##### width
##### height

Type: `string` `number`
Type: `'auto' | string | number`

The width and height are given as a number followed by a unit, or the word `'auto'`.

Expand Down
4 changes: 2 additions & 2 deletions test.js
@@ -1,7 +1,7 @@
import test from 'ava';
import m from '.';
import termImg from '.';

test('main', t => {
// TODO: Write some real tests
t.is(typeof m, 'function');
t.is(typeof termImg, 'function');
});

0 comments on commit afa45a3

Please sign in to comment.