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 #49

Merged
merged 1 commit into from
Apr 9, 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
33 changes: 33 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
Write (copy) to the clipboard asynchronously.

@param text - The text to write to the clipboard.
*/
export function write(text: string): Promise<void>;

/**
Write (copy) to the clipboard synchronously.

@param text - The text to write to the clipboard.

@example
```
import * as clipboardy from 'clipboardy';

clipboardy.writeSync('🦄');

clipboardy.readSync();
//=> '🦄'
```
*/
export function writeSync(text: string): void;

/**
Read (paste) from the clipboard asynchronously.
*/
export function read(): Promise<string>;

/**
Read (paste) from the clipboard synchronously.
*/
export function readSync(): string;
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,29 @@ function getPlatformLib() {
if (process.env.PREFIX !== '/data/data/com.termux/files/usr') {
throw new Error('You need to install Termux for this module to work on Android: https://termux.com');
}

return termux;
default:
return linux;
}
}

exports.write = input => {
if (typeof input !== 'string') {
return Promise.reject(new TypeError(`Expected a string, got ${typeof input}`));
exports.write = text => {
if (typeof text !== 'string') {
return Promise.reject(new TypeError(`Expected a string, got ${typeof text}`));
}

return platformLib.copy({input}).then(() => {});
return platformLib.copy({input: text}).then(() => {});
};

exports.read = () => platformLib.paste({stripEof: false});

exports.writeSync = input => {
if (typeof input !== 'string') {
throw new TypeError(`Expected a string, got ${typeof input}`);
exports.writeSync = text => {
if (typeof text !== 'string') {
throw new TypeError(`Expected a string, got ${typeof text}`);
}

platformLib.copySync({input});
platformLib.copySync({input: text});
};

exports.readSync = () => platformLib.pasteSync({stripEof: false}).stdout;
7 changes: 7 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {expectType} from 'tsd';
import * as clipboardy from '.';

clipboardy.writeSync('🦄');
expectType<Promise<void>>(clipboardy.write('🦄'));
expectType<string>(clipboardy.readSync());
expectType<Promise<string>>(clipboardy.read());
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.d.ts",
"lib",
"fallbacks"
],
Expand All @@ -34,11 +35,12 @@
"xsel"
],
"dependencies": {
"arch": "^2.1.0",
"execa": "^0.10.0"
"arch": "^2.1.1",
"execa": "^1.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
12 changes: 8 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,30 @@ clipboardy.readSync();

### clipboardy

#### .write(input)
#### .write(text)

Write (copy) to the clipboard asynchronously. Returns a `Promise`.

##### input
##### text

Type: `string`

The text to write to the clipboard.

#### .read()

Read (paste) from the clipboard asynchronously. Returns a `Promise`.

#### .writeSync(input)
#### .writeSync(text)

Write (copy) to the clipboard synchronously.

##### input
##### text

Type: `string`

The text to write to the clipboard.

#### .readSync()

Read (paste) from the clipboard synchronously.
Expand Down
10 changes: 5 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {EOL} from 'os';
import {serial as test} from 'ava';
import m from '.';
import clipboardy from '.';

const writeRead = async input => {
await m.write(input);
return m.read();
await clipboardy.write(input);
return clipboardy.read();
};

const writeReadSync = input => {
m.writeSync(input);
return m.readSync();
clipboardy.writeSync(input);
return clipboardy.readSync();
};

test('async', async t => {
Expand Down