
I say "quickest", but really for most cases I think we're talking about a difference of milliseconds, so for the purposes of this discussion, let's refer to some desirable hybrid of code concision and processing performance. I know that sounds dangerously like "best" for a forum topic, but I'll take my chances. =P I have a situation where I want to prioritize processing one argument in the API command line before the others. For instance, given a command line like this: !the_script_name --foo:bar --qud:zu --port:out --starboard:home --posh --notes:Best Practices: vol. 1 I want to process the qud argument first, because what I do with it will change what I do with the others. For the purposes of the " quickest best most elegant" portion of this question, I should note that there are both splittable arguments (i.e., foo:bar, etc.) and non-splittable, 'trigger' arguments ( posh ). We also have arguments that can potentially contain the character driving our split (the value of notes includes the ':' character). To accomplish that, I am currently using a script that looks like this: var the_script_name = the_script_name || (function () { 'use strict';
const splitArgs = (a) => { return a.split(":") }; const joinVals = (a) => { return [a.slice(0)[0], a.slice(1).join(":").trim()]; }; const lookFor = (a) => { return a[0] === "qud"; }; const handleInput = (msg_orig) => { if (msg_orig.type !== 'api' || !msg_orig.content.toLowerCase().startsWith('!the_script_name ')) { return; } let args = msg_orig.content.split(/\s--/) // split at argument delimiter .slice(1) // drop the api tag .map(splitArgs) // split each arg (foo:bar becomes [foo, bar]) .map(joinVals); // if the value included a colon (the delimiter), join the parts that were inadvertently separated log(args);
processQud(args); // look for qud argument first log(args); // qud argument, if present, has been removed
// process other arguments here };
const processQud = (args) => { let qud = args.filter(lookFor)[0] || []; if (qud.length) { // ...stuff happens here... args[args.findIndex(lookFor)] = args.pop(); // pop the last item and copy it over the qud argument } return; };
const registerEventHandlers = () => { on('chat:message', handleInput); };
return { RegisterEventHandlers: registerEventHandlers }; })();
on('ready', () => { the_script_name.RegisterEventHandlers(); }); As far as the best way to prioritize an argument, I have seen it done a few different ways. I've seen a switch{} block (looking for the arg, breaking when found). I've seen assigning all the argument pairs (foo:bar) to an object (as key:value), and then using hasOwnProperty. I think I've seen some of Aaron's code using the underscore _.has function, but I'm not sure if that only works for an object. So, is one faster/better/more trusted/more versatile? Are there edge cases for the way arguments could be written that my code wouldn't catch? Also, a few questions about the syntax of my code, above, to help me learn... Question 1 How can I pass another variable into a fat arrow block for use with a map(), filter(), or reduce() function? The above code works fine if I am only looking to prioritize the argument qud , but if I need to prioritize a second argument after that, my lookFor function is currently hard coded to only filter on "qud". Relevant bits: const lookFor = (a) => { return a[0] === "qud"; };
//... and ... // let qud = args.filter(lookFor)[0] || []; Question 2 I am trying to explore and learn the fat arrow syntax, and have used it for the interior function declarations. I know that my script declaration can use it, too: var skilllister = skilllister || (() => { ...but before I use that, I'm trying to understand the closing of that function block. Specifically, why the () that follows the parentheses encompassing the function declaration: ?)(); Are those to turn the declaration into a executable function, so that when the sandbox starts up and concatenates all of the scripts, the script gets run its one and only time? (please say yes... please say yes... please say...) Question 3 --I think I answered this one, in typing this up--