From 40eee0a442fd7a47c50a076c9783e504d20c0555 Mon Sep 17 00:00:00 2001 From: pyoor Date: Mon, 21 May 2018 12:18:09 -0400 Subject: [PATCH] Add utils.script.runner --- lib/utils/script.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/utils/script.js b/lib/utils/script.js index af7f874..81ef852 100644 --- a/lib/utils/script.js +++ b/lib/utils/script.js @@ -87,6 +87,31 @@ class script extends utils { return `${random.number()} % ${o.pick(objName)}.length` } + static runner (cmds) { + /** + * Wrap command(s) in setInterval, setTimeout, loop or run directly + * @param {Object|String} cmds - Command(s) to be executed + */ + cmds = (Array.isArray(cmds)) ? cmds : [cmds] + cmds = cmds.filter(function (i) { return i !== undefined }) + if (cmds.length) { + if (random.chance(50)) { + // Wrap each command in try/catch for use in setInterval, setTimeout, repeater + switch (random.number(3)) { + case 0: + return `setInterval(function () { ${utils.script.safely(cmds)} }, ${random.range(100, 400)} )` + case 1: + return `setTimeout(function () { ${utils.script.safely(cmds)} }, ${random.range(100, 400)} )` + case 2: + let n = random.number(random.number(30)) + return `for (let i = 0; i < ${n}; i++) { ${utils.script.safely(cmds)} }` + } + } else { + return cmds + } + } + } + static safely (obj) { if (Array.isArray(obj)) { return obj.map(s => utils.script.safely(s)).join(' ')