std/dotenv/stringify.ts
Emerson Laurentino 8b9a139472
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>
2024-08-29 10:32:39 +10:00

48 lines
1.4 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
/**
* Stringify an object into a valid `.env` file format.
*
* @example Usage
* ```ts
* import { stringify } from "@std/dotenv/stringify";
* import { assertEquals } from "@std/assert";
*
* const object = { GREETING: "hello world" };
* assertEquals(stringify(object), "GREETING='hello world'");
* ```
*
* @param object object to be stringified
* @returns string of object
*/
export function stringify(object: Record<string, string>): string {
const lines: string[] = [];
for (const [key, value] of Object.entries(object)) {
let quote;
let escapedValue = value ?? "";
if (key.startsWith("#")) {
console.warn(
`key starts with a '#' indicates a comment and is ignored: '${key}'`,
);
continue;
} else if (escapedValue.includes("\n") || escapedValue.includes("'")) {
// escape inner new lines
escapedValue = escapedValue.replaceAll("\n", "\\n");
quote = `"`;
} else if (escapedValue.match(/\W/)) {
quote = "'";
}
if (quote) {
// escape inner quotes
escapedValue = escapedValue.replaceAll(quote, `\\${quote}`);
escapedValue = `${quote}${escapedValue}${quote}`;
}
const line = `${key}=${escapedValue}`;
lines.push(line);
}
return lines.join("\n");
}