Last active
August 5, 2017 02:53
-
-
Save the-vampiire/a78953572ac42444fb07ae0c793dcd8e to your computer and use it in GitHub Desktop.
argument parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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