MCPcopy Create free account
hub / github.com/Effect-TS/effect / parseDotEnvContents

Function parseDotEnvContents

packages/effect/src/ConfigProvider.ts:1096–1129  ·  view source on GitHub ↗
(lines: string)

Source from the content-addressed store, hash-verified

1094 /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg
1095
1096function parseDotEnvContents(lines: string): Record<string, string> {
1097 const obj: Record<string, string> = Object.create(null)
1098
1099 // Convert line breaks to same format
1100 lines = lines.replace(/\r\n?/gm, "\n")
1101
1102 let match: RegExpExecArray | null
1103 while ((match = DOT_ENV_LINE.exec(lines)) != null) {
1104 const key = match[1]
1105
1106 // Default undefined or null to empty string
1107 let value = match[2] || ""
1108
1109 // Remove whitespace
1110 value = value.trim()
1111
1112 // Check if double quoted
1113 const maybeQuote = value[0]
1114
1115 // Remove surrounding quotes
1116 value = value.replace(/^(['"`])([\s\S]*)\1$/gm, "$2")
1117
1118 // Expand newlines if double quoted
1119 if (maybeQuote === "\"") {
1120 value = value.replace(/\\n/g, "\n")
1121 value = value.replace(/\\r/g, "\r")
1122 }
1123
1124 // Add to object
1125 obj[key] = value
1126 }
1127
1128 return obj
1129}
1130
1131function dotEnvExpand(parsed: Record<string, string>): Record<string, string> {
1132 const newParsed: Record<string, string> = Object.create(null)

Callers 1

fromDotEnvContentsFunction · 0.85

Calls 3

trimMethod · 0.80
replaceMethod · 0.45
execMethod · 0.45

Tested by

no test coverage detected