@intlayer/config
Version:
Retrieve Intlayer configurations and manage environment variables for both server-side and client-side environments.
25 lines (23 loc) • 1.64 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
//#region src/utils/parseFilePathPattern.ts
/**
* Parses a FilePathPattern configuration string or function to a final string path.
* Resolves context variables like `{{locale}}`, `{{key}}`, `{{fileName}}`, `{{extension}}`.
*/
const parseFilePathPattern = async (pattern, context) => {
if (typeof pattern === "function") return await pattern(context);
if (typeof pattern === "object" && pattern !== null && context.locale) {
const localePattern = pattern[context.locale];
if (localePattern) {
if (typeof localePattern === "function") return await localePattern(context);
return parseStringPattern(localePattern, context);
}
}
if (typeof pattern === "string") return parseStringPattern(pattern, context);
return "";
};
const parseStringPattern = (pattern, context) => pattern.replaceAll(/\{\{\s*locale\s*\}\}/g, context.locale ?? "").replaceAll(/\{\{\s*key\s*\}\}/g, context.key ?? "").replaceAll(/\{\{\s*fileName\s*\}\}/g, context.fileName ?? "").replaceAll(/\{\{\s*extension\s*\}\}/g, context.extension ?? "").replaceAll(/\{\{\s*componentFileName\s*\}\}/g, context.componentFileName ?? "").replaceAll(/\{\{\s*componentExtension\s*\}\}/g, context.componentExtension ?? "").replaceAll(/\{\{\s*format\s*\}\}/g, context.format ?? "").replaceAll(/\{\{\s*componentFormat\s*\}\}/g, context.componentFormat ?? "").replaceAll(/\{\{\s*componentDirPath\s*\}\}/g, context.componentDirPath ?? "");
//#endregion
exports.parseFilePathPattern = parseFilePathPattern;
exports.parseStringPattern = parseStringPattern;
//# sourceMappingURL=parseFilePathPattern.cjs.map