std/dotenv/stringify.ts
Yoshiya Hinosawa 0ce9c2bf7e
experiment
2024-01-31 18:10:15 +09:00

46 lines
1.2 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
/**
* Stringify an object into a valid `.env` file format.
*
* @example
* ```ts
* import { stringify } from "@std/dotenv/stringify";
*
* const object = { GREETING: "hello world" };
* const string = 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")) {
// 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");
}