diff --git a/.gitattributes b/.gitattributes index 391f0a4..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf diff --git a/.gitignore b/.gitignore index 3c3629e..239ecff 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +yarn.lock diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.travis.yml b/.travis.yml index f178ec0..2ae9d62 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,5 @@ language: node_js node_js: - - 'node' + - '10' + - '8' + - '6' diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..3546eb9 --- /dev/null +++ b/index.d.ts @@ -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; diff --git a/index.js b/index.js index a6ba794..eaf2958 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ 'use strict'; -module.exports = input => { +const copyTextToClipboard = input => { const el = document.createElement('textarea'); el.value = input; @@ -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 @@ -28,7 +28,7 @@ module.exports = input => { let success = false; try { success = document.execCommand('copy'); - } catch (err) {} + } catch (error) {} document.body.removeChild(el); @@ -39,3 +39,6 @@ module.exports = input => { return success; }; + +module.exports = copyTextToClipboard; +module.exports.default = copyTextToClipboard; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..d67988e --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,4 @@ +import {expectType} from 'tsd-check'; +import copyTextToClipboard from '.'; + +expectType(copyTextToClipboard('🦄🌈')); diff --git a/package.json b/package.json index 808b4ec..5c7c6f4 100644 --- a/package.json +++ b/package.json @@ -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", @@ -27,7 +28,8 @@ "modern" ], "devDependencies": { - "xo": "*" + "tsd-check": "^0.3.0", + "xo": "^0.24.0" }, "xo": { "envs": [