@karmaniverous/get-dotenv
    Preparing search index...

    Function dotenvExpand

    • Expands environment-variable references in a string. References may be written $VAR[:default] or ${VAR[:default]}; unknown variables without a default expand to the empty string.

      Parameters

      • value: string | undefined

        The string to expand.

      • ref: ProcessEnv = process.env

        The reference object to use for variable expansion.

      Returns string | undefined

      The expanded string, or undefined when it is empty.

      process.env.FOO = 'bar';
      dotenvExpand('Hello $FOO'); // "Hello bar"
      dotenvExpand('Hello $BAZ:world'); // "Hello world"
      dotenvExpand('Price \\$5'); // "Price $5"

      ref holds composed values: each has already been expanded once, so a referenced value is substituted verbatim and never expanded again (#94). With ref = { P: '$5' }, $P expands to $5. Only a default is expanded, by the same rules. The text is scanned once, left to right, and each substituted value is spliced in as-is (it is not re-scanned with the text around it):

      • \$ is a literal $.
      • A $ that starts no reference (cost $, $!) stays literal, and the other references in the value are still expanded.
      • The unbraced default runs to the next whitespace, so $HOST:8080 reads 8080 as HOST's default; write ${HOST}:8080. There is no :- form: ${A:-x} has the default -x.
      • A variable set to the empty string is used as is; only an unset variable takes its default.
      • A value never sees itself: a key that is being expanded is not yet in the reference map (dotenvExpandAll adds it only once it is expanded), so a reference to it resolves to the value composed before it, else counts as unset. With nothing earlier setting P, P=${P:/usr/bin}:/x expands to /usr/bin:/x.