@shons/next-configify
Version:
NestJS Config on Next Steroids
48 lines • 1.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Variables = void 0;
class Variables {
static expand(record) {
const expanded = {};
for (const key in record) {
const value = record[key];
const interpolated = this.interpolate(value, record);
expanded[key] =
typeof interpolated === 'string'
? this.escapeSequences(interpolated)
: interpolated;
}
return expanded;
}
static interpolate(value, record) {
if (typeof value !== 'string')
return value;
const lastUnescapedDollarSignIndex = this.lastIndexOf(value, this.UNESCAPED_PATTERN);
if (lastUnescapedDollarSignIndex === -1)
return value;
const rightMostGroup = value.slice(lastUnescapedDollarSignIndex);
const match = rightMostGroup.match(this.EXPANSION_GROUP_PATTERN);
if (match != null) {
let [, group, variableName, defaultValue] = match;
variableName =
variableName.indexOf('-') !== -1
? variableName.replaceAll('-', '_')
: variableName;
variableName = variableName.replaceAll('.', '_').toUpperCase();
return this.interpolate(value.replace(group, defaultValue || `${record[variableName]}` || ''), record);
}
return value;
}
static escapeSequences(value) {
return value.replace(this.ESCAPE_PATTERN, '$');
}
static lastIndexOf(value, regex) {
const matches = Array.from(value.matchAll(regex));
return matches.length > 0 ? matches.slice(-1)[0].index : -1;
}
}
exports.Variables = Variables;
Variables.ESCAPE_PATTERN = /\\\$/g;
Variables.UNESCAPED_PATTERN = /(?!(?<=\\))\$/g;
Variables.EXPANSION_GROUP_PATTERN = /((?!(?<=\\))\${?([\w-.]+)(?::-([^}\\]*))?}?)/;
//# sourceMappingURL=variables.js.map