diff --git a/src/index.js b/src/index.js index 62f179d..2dfbb45 100644 --- a/src/index.js +++ b/src/index.js @@ -1,14 +1,14 @@ -const React = require('react'); +const React = require("react"); -const icon = require('./icon.png'); +const icon = require("./icon.png"); -const KEYWORD = 'genpw'; +const KEYWORD = "genpw"; const STRLEN = 16; const mkstr = length => { - var result = ''; + var result = ""; var characters = - 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@/?#$%&*()[]{},.!~'; + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@/?#$%&*()[]{},.!~"; var charactersLength = characters.length; for (var i = 0; i < length; i++) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); @@ -16,29 +16,28 @@ const mkstr = length => { return result; }; -const order = 10; - const generate = ({ term, actions, display }) => { - let regex = new RegExp('^' + KEYWORD + '\\s+([0-9]+)', 'i'); - let match = term.match(regex); + if (term.startsWith(KEYWORD)) { + let match = term.match(/^.+\s+([0-9]+)/i); - if (match) { - var length = match[1]; - } else { - var length = STRLEN; + if (match && match[1]) { + var length = match[1]; + } else { + var length = STRLEN; + } + + var newstr = mkstr(length); + const result = { + icon, + term, + title: "Generate a random string", + subtitle: "Add a " + length + " character random string to the clipboard", + onSelect: () => actions.copyToClipboard(newstr), + getPreview: () => "" + newstr + "" + }; + + display(result); } - - var newstr = mkstr(length); - const result = { - icon, - order, - term, - title: 'Add the following ' + length + ' character random string to the clipboard', - subtitle: newstr, - onSelect: () => actions.copyToClipboard(newstr) - }; - - display(result); }; module.exports = {