Skip to content

Instantly share code, notes, and snippets.

@the-vampiire
Last active August 5, 2017 02:53
Show Gist options
  • Select an option

  • Save the-vampiire/a78953572ac42444fb07ae0c793dcd8e to your computer and use it in GitHub Desktop.

Select an option

Save the-vampiire/a78953572ac42444fb07ae0c793dcd8e to your computer and use it in GitHub Desktop.
argument parser
argumentParser = arguments => {
const acceptedUpdateItems = ['gitHub', 'blog', 'portfolio', 'story', 'projects', 'certifications'];
let error;
let output = argumentSplitter(arguments);
let item = output.item;
// initial check to ensure the update item is valid
if(!~acceptedUpdateItems.indexOf(item)) return `invalid update item [\`${item}\`]\n Use \`/update help\` for a list of available update items.`;
// handle the special case of the story item which only has the storyString as its data
let storyString = output.storyString ? output.storyString : null;
let pairsArray = output.pairsArray ? output.pairsArray : null;
if(pairsArray){
let flagDataPairs = {};
pairsArray.forEach( pairString => {
let splitIndex = pairString.indexOf(' ');
// fixes the, albeit odd, case where "/project [item] [-invalidFlag]" with no data is passed...
let flag = ~splitIndex ? pairString.slice(0, splitIndex) : pairString.slice(0);
let data = pairString.slice(splitIndex+1);
// if there is an error it is returned from errorScan as a string
let errorScanOutput = errorScanAndModify(item, flag, data);
if(typeof errorScanOutput === 'string'){
error = errorScanOutput;
}else{
// if there is no error errorScan returns an object containing the flag and data (in case they were modified)
let output = errorScanOutput;
flag = output.flag;
data = output.data;
}
flagDataPairs[flag] = data;
});
return error ? error : {item: item, updateData: flagDataPairs }
}
return {item: item, updateData: storyString };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment