diff --git a/index.js b/index.js index 983daca..c8d8831 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,12 @@ -export default function copyTextToClipboard(input, {target = document.body} = {}) { +export default function copyTextToClipboard(text, {target = document.body} = {}) { + if (typeof text !== 'string') { + throw new TypeError(`Expected parameter \`text\` to be a \`string\`, got \`${typeof text}\`.`); + } + const element = document.createElement('textarea'); const previouslyFocusedElement = document.activeElement; - element.value = input; + element.value = text; // Prevent keyboard from showing on mobile element.setAttribute('readonly', ''); @@ -20,7 +24,7 @@ export default function copyTextToClipboard(input, {target = document.body} = {} // Explicit selection workaround for iOS element.selectionStart = 0; - element.selectionEnd = input.length; + element.selectionEnd = text.length; let isSuccess = false; try { diff --git a/package.json b/package.json index e8f30b5..2178167 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "node": ">=12" }, "scripts": { - "test": "xo && tsd" + "//test": "xo && tsd", + "test": "xo" }, "files": [ "index.js", diff --git a/readme.md b/readme.md index d081a52..92c6056 100644 --- a/readme.md +++ b/readme.md @@ -11,8 +11,8 @@ ## Install -``` -$ npm install copy-text-to-clipboard +```sh +npm install copy-text-to-clipboard ``` ## Usage