Skip to content

Commit

Permalink
Require Node.js 6, add TypeScript definition (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 10, 2019
1 parent 4b5bdda commit 70faa08
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 10 deletions.
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

0 comments on commit 70faa08

Please sign in to comment.