-
-
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
This file contains 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
// 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