Skip to content

Instantly share code, notes, and snippets.

@scryptonite
Last active May 12, 2020 20:08
Show Gist options
  • Save scryptonite/b7e591c41a02e4cdbbd9996233681d03 to your computer and use it in GitHub Desktop.
Save scryptonite/b7e591c41a02e4cdbbd9996233681d03 to your computer and use it in GitHub Desktop.
for giving TTsBeastie's command parameter parser the superpower of understanding long strings that contain spaces
// turns `first second "third fourth fifth"` into ["first", "second", "third fourth fifth"].length === 3
const getParameters = message => Array.from((function*(string){
console.log(string);
const encapsulator = ["\"", "'", "`"];
const escape = "\\";
for(let index = 0; index < string.length; index++){
const start = index;
const part = string[index];
if(encapsulator.includes(part)) {
while(index < string.length && string[++index] !== part){
if(string[index] === escape) { index += 1; }
};
yield string.slice(start + 1, index++);
} else {
while(index < string.length && string[++index] !== " "){};
yield string.slice(start, index);
}
}
})(message));
console.log(getParamters(`"one" "one's two's threes" 123_456 Hello world`));
const [trigger, name, response] = getParameters(`!alias helloworld "Hello World!"`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment