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

Require Node.js 6, add TypeScript definition #24

Merged
merged 1 commit into from
Mar 10, 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
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
language: node_js
node_js:
- 'node'
- '10'
- '8'
- '6'
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Copy text to the clipboard.
*
* Must be called in response to a user gesture event, like `click` or `keyup`.
*
* @param text - The text to copy to clipboard.
* @returns Whether it succeeded to copy text.
*/
export default function copyTextToClipboard(text: string): boolean;
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
module.exports = input => {
const copyTextToClipboard = input => {
const el = document.createElement('textarea');

el.value = input;
Expand All @@ -18,7 +18,7 @@ module.exports = input => {
originalRange = selection.getRangeAt(0);
}

document.body.appendChild(el);
document.body.appendChild(el); // eslint-disable-line unicorn/prefer-node-append
el.select();

// Explicit selection workaround for iOS
Expand All @@ -28,7 +28,7 @@ module.exports = input => {
let success = false;
try {
success = document.execCommand('copy');
} catch (err) {}
} catch (error) {}

document.body.removeChild(el);

Expand All @@ -39,3 +39,6 @@ module.exports = input => {

return success;
};

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

expectType<boolean>(copyTextToClipboard('🦄🌈'));
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
"node": ">=6"
},
"scripts": {
"test": "xo"
"test": "xo && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"copy",
Expand All @@ -27,7 +28,8 @@
"modern"
],
"devDependencies": {
"xo": "*"
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
},
"xo": {
"envs": [
Expand Down