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

Function interpolate

packages/platform/src/internal/platformConfigProvider.ts:110–146  ·  view source on GitHub ↗
(envValue: string, parsed: Record<string, string>)

Source from the content-addressed store, hash-verified

108
109/** @internal */
110const interpolate = (envValue: string, parsed: Record<string, string>) => {
111 // find the last unescaped dollar sign in the
112 // value so that we can evaluate it
113 const lastUnescapedDollarSignIndex = searchLast(envValue, /(?!(?<=\\))\$/g)
114
115 // If we couldn't match any unescaped dollar sign
116 // let's return the string as is
117 if (lastUnescapedDollarSignIndex === -1) return envValue
118
119 // This is the right-most group of variables in the string
120 const rightMostGroup = envValue.slice(lastUnescapedDollarSignIndex)
121
122 /**
123 * This finds the inner most variable/group divided
124 * by variable name and default value (if present)
125 * (
126 * (?!(?<=\\))\$ // only match dollar signs that are not escaped
127 * {? // optional opening curly brace
128 * ([\w]+) // match the variable name
129 * (?::-([^}\\]*))? // match an optional default value
130 * }? // optional closing curly brace
131 * )
132 */
133 const matchGroup = /((?!(?<=\\))\${?([\w]+)(?::-([^}\\]*))?}?)/
134 const match = rightMostGroup.match(matchGroup)
135
136 if (match !== null) {
137 const [_, group, variableName, defaultValue] = match
138
139 return interpolate(
140 envValue.replace(group, defaultValue || parsed[variableName] || ""),
141 parsed
142 )
143 }
144
145 return envValue
146}
147
148/** @internal */
149const searchLast = (str: string, rgx: RegExp) => {

Callers 1

expandFunction · 0.85

Calls 1

searchLastFunction · 0.85

Tested by

no test coverage detected