fix(dotenv): handle single-quotes in values in stringify() (#5846)

* refactor(dotenv): add parse function and test

* refactor(dotenv): escape single quotes in stringify function

* refactor(dotenv): fix stringify function to correctly escape single quotes

* refactor(dotenv): fix stringify function to correctly escape single quotes

* tweak

---------

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
This commit is contained in:
Emerson Laurentino 2024-08-28 21:32:39 -03:00 committed by GitHub
parent 2c4287178a
commit 8b9a139472
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 1 deletions

View File

@ -27,7 +27,7 @@ export function stringify(object: Record<string, string>): string {
`key starts with a '#' indicates a comment and is ignored: '${key}'`, `key starts with a '#' indicates a comment and is ignored: '${key}'`,
); );
continue; continue;
} else if (escapedValue.includes("\n")) { } else if (escapedValue.includes("\n") || escapedValue.includes("'")) {
// escape inner new lines // escape inner new lines
escapedValue = escapedValue.replaceAll("\n", "\\n"); escapedValue = escapedValue.replaceAll("\n", "\\n");
quote = `"`; quote = `"`;

View File

@ -78,4 +78,9 @@ Deno.test("stringify()", async (t) => {
`NULL=`, `NULL=`,
), ),
); );
await t.step("handles single-quote characters", () =>
assertEquals(
stringify({ PARSE: "par'se" }),
`PARSE="par'se"`,
));
}); });