MCPcopy Create free account
hub / github.com/denoland/std / parse

Function parse

dotenv/parse.ts:77–113  ·  view source on GitHub ↗
(text: string)

Source from the content-addressed store, hash-verified

75 * @returns The parsed object.
76 */
77export function parse(text: string): Record<string, string> {
78 const env: Record<string, string> = Object.create(null);
79
80 const keysForExpandCheck = [];
81
82 for (const match of text.matchAll(KEY_VALUE_REGEXP)) {
83 const { key, interpolated, notInterpolated, unquoted } = match
84 ?.groups as LineParseResult;
85
86 if (!VALID_KEY_REGEXP.test(key)) {
87 // deno-lint-ignore no-console
88 console.warn(
89 `Ignored the key "${key}" as it is not a valid identifier: The key need to match the pattern /^[a-zA-Z_][a-zA-Z0-9_]*$/.`,
90 );
91 continue;
92 }
93
94 if (unquoted) {
95 keysForExpandCheck.push(key);
96 }
97
98 env[key] = typeof notInterpolated === "string"
99 ? notInterpolated
100 : typeof interpolated === "string"
101 ? expandCharacters(interpolated)
102 : unquoted.trim();
103 }
104
105 //https://github.com/motdotla/dotenv-expand/blob/ed5fea5bf517a09fd743ce2c63150e88c8a5f6d1/lib/main.js#L23
106 const variablesMap = { ...env };
107
108 for (const key of keysForExpandCheck) {
109 env[key] = expand(env[key]!, variablesMap);
110 }
111
112 return env;
113}

Callers 4

stringify_test.tsFile · 0.90
parse_test.tsFile · 0.90
parseFileSyncFunction · 0.90
parseFileFunction · 0.90

Calls 3

expandCharactersFunction · 0.85
expandFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected