-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v0feat: simplified auto-quoting/escape in autocomplete
- Note that values that have had their quotes escaped but have no spaces are no longer quoted.
- Loading branch information
1 parent
baaab41
commit 252726e
Showing
3 changed files
with
44 additions
and
28 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { multisplice } from "@alanscodelog/utils/multisplice.js" | ||
|
||
export function escapeVariableOrPrefix(variable: string, preferredQuote: string): string { | ||
let doQuote = false | ||
for (const quoteType of ["\"", "'", "`"]) { | ||
if (!variable.includes(quoteType) && !variable.includes(" ")) continue | ||
if (variable.startsWith(quoteType) | ||
&& variable.endsWith(quoteType)) { | ||
break | ||
} | ||
const indexes: number[] = [] | ||
for (let i = 0; i < variable.length; i++) { | ||
const char = variable[i] | ||
if (char === undefined) break | ||
if (char === "\\") { | ||
i += 2 | ||
continue | ||
} | ||
if (char === " ") { | ||
doQuote = true | ||
} | ||
if (char === quoteType) indexes.push(i) | ||
} | ||
|
||
if (indexes.length === 0) break | ||
|
||
const newVal = multisplice(variable.split(""), indexes, 0, "\\").array.join("") | ||
variable = newVal | ||
|
||
break | ||
} | ||
if (doQuote) { | ||
variable = `${preferredQuote}${variable}${preferredQuote}` | ||
} | ||
return variable | ||
} |
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