aws-cdk-lib
Version:
Version 2 of the AWS Cloud Development Kit library
2 lines (1 loc) • 1.5 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.TemplateString=void 0;var errors_1=()=>{var tmp=require("../errors");return errors_1=()=>tmp,tmp};class TemplateString{template;constructor(template){this.template=template}parse(input){const templateParts=this.template.split(/(\$\{[^{}]+})/),result={};let inputIndex=0;for(let i=0;i<templateParts.length;i++){const part=templateParts[i];if(part.startsWith("${")&&part.endsWith("}")){const varName=part.slice(2,-1),nextLiteral=templateParts[i+1]||"";let value="";if(nextLiteral){const endIndex=input.indexOf(nextLiteral,inputIndex);if(endIndex===-1)throw new(errors_1()).UnscopedValidationError(`Input ${input} does not match template ${this.template}`);value=input.slice(inputIndex,endIndex),inputIndex=endIndex}else value=input.slice(inputIndex),inputIndex=input.length;result[varName]=value}else{if(input.slice(inputIndex,inputIndex+part.length)!==part)throw new(errors_1()).UnscopedValidationError(`Input ${input} does not match template ${this.template}`);inputIndex+=part.length}}if(inputIndex!==input.length)throw new(errors_1()).UnscopedValidationError(`Input ${input} does not match template ${this.template}`);return result}interpolate(variables){return this.template.replace(/\${([^{}]+)}/g,(_,varName)=>{if(variables[varName]===void 0)throw new(errors_1()).UnscopedValidationError(`Variable ${varName} not provided for template interpolation`);return variables[varName]})}}exports.TemplateString=TemplateString;