Ran formatter

This commit is contained in:
RevanProdigalKnight 2022-05-04 08:32:55 -06:00
parent 8d5f80de26
commit 286ab64d67

@ -8,12 +8,12 @@ function parseArg(arg: string): string | number | boolean {
return asNumber; return asNumber;
} }
if (arg === 'true' || arg === 'false') { if (arg === "true" || arg === "false") {
return arg === 'true'; return arg === "true";
} }
// Strip quotation marks from strings that begin/end with the same mark // Strip quotation marks from strings that begin/end with the same mark
return arg.replace(/^"(.*?)"$/g, '$1').replace(/^'(.*?)'$/g, '$1'); return arg.replace(/^"(.*?)"$/g, "$1").replace(/^'(.*?)'$/g, "$1");
} }
export function ParseCommands(commands: string): string[] { export function ParseCommands(commands: string): string[] {
@ -48,12 +48,12 @@ export function ParseCommand(command: string): (string | number | boolean)[] {
// Track depth of quoted strings, e.g.: "the're 'going away' rather 'quickly \"and awkwardly\"'" should be parsed as a single string // Track depth of quoted strings, e.g.: "the're 'going away' rather 'quickly \"and awkwardly\"'" should be parsed as a single string
const quotes: string[] = []; const quotes: string[] = [];
let arg = ''; let arg = "";
while (idx < command.length) { while (idx < command.length) {
const c = command.charAt(idx); const c = command.charAt(idx);
// If the current character is a backslash, add the next character verbatim to the argument // If the current character is a backslash, add the next character verbatim to the argument
if (c === '\\') { if (c === "\\") {
arg += command.charAt(++idx); arg += command.charAt(++idx);
// If the current character is a single- or double-quote mark, add it to the current argument. // If the current character is a single- or double-quote mark, add it to the current argument.
} else if (c === KEY.DOUBLE_QUOTE || c === KEY.QUOTE) { } else if (c === KEY.DOUBLE_QUOTE || c === KEY.QUOTE) {
@ -65,23 +65,19 @@ export function ParseCommand(command: string): (string | number | boolean)[] {
// If we're already in a quoted string, push onto the stack of string starts to track depth. // If we're already in a quoted string, push onto the stack of string starts to track depth.
if ( if (
c !== quote && c !== quote &&
( (prev === KEY.SPACE ||
prev === KEY.SPACE ||
prev === KEY.EQUAL || prev === KEY.EQUAL ||
(c === KEY.DOUBLE_QUOTE && prev === KEY.QUOTE) || (c === KEY.DOUBLE_QUOTE && prev === KEY.QUOTE) ||
(c === KEY.QUOTE && prev === KEY.DOUBLE_QUOTE) (c === KEY.QUOTE && prev === KEY.DOUBLE_QUOTE))
)
) { ) {
quotes.push(c); quotes.push(c);
// If the next character is a space and the current character is the same as the previously used // If the next character is a space and the current character is the same as the previously used
// quotation mark, this is a valid end to a string. Pop off the depth tracker. // quotation mark, this is a valid end to a string. Pop off the depth tracker.
} else if ( } else if (
c === quote && c === quote &&
( (next === KEY.SPACE ||
next === KEY.SPACE ||
(c === KEY.DOUBLE_QUOTE && next === KEY.QUOTE) || (c === KEY.DOUBLE_QUOTE && next === KEY.QUOTE) ||
(c === KEY.QUOTE && next === KEY.DOUBLE_QUOTE) (c === KEY.QUOTE && next === KEY.DOUBLE_QUOTE))
)
) { ) {
quotes.pop(); quotes.pop();
} }
@ -90,7 +86,7 @@ export function ParseCommand(command: string): (string | number | boolean)[] {
} else if (c === KEY.SPACE && quotes.length === 0) { } else if (c === KEY.SPACE && quotes.length === 0) {
args.push(parseArg(arg)); args.push(parseArg(arg));
arg = ''; arg = "";
} else { } else {
// Add the current character to the current argument // Add the current character to the current argument
arg += c; arg += c;
@ -100,7 +96,7 @@ export function ParseCommand(command: string): (string | number | boolean)[] {
} }
// Add the last arg (if any) // Add the last arg (if any)
if (arg !== '') { if (arg !== "") {
args.push(parseArg(arg)); args.push(parseArg(arg));
} }