UNPKG

@microsoft/botbuilder-m365

Version:

M365 extensions for Microsoft BotBuilder, Alpha release.

112 lines 4.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VarBlock = void 0; const yaml_1 = require("yaml"); const Block_1 = require("./Block"); /** * @private */ class VarBlock extends Block_1.Block { constructor(content) { super(); this.name = this.varName(); this.content = content; } get type() { return Block_1.BlockTypes.Variable; } isValid() { let valid = true; let error; if (this.content[0] !== VarBlock.Prefix) { error = `A variable must start with the symbol ${VarBlock.Prefix}`; valid = false; } else if (this.content.length < 2) { error = 'The variable name is empty'; valid = false; } else { const varName = this.varName(); if (!VarBlock.isValidVarName(varName)) { error = `The variable name '${varName}' contains invalid characters. Only alphanumeric chars, underscore, and dot are allowed.`; valid = false; } else if (varName.split('.').length > 2) { error = `The variable name '${varName}' isn't currently supported. Variables must be of the form '$<property>' or '$<scope>.<property>' and sub properties aren't currently supported.`; valid = false; } } return { valid, error }; } render(context, state) { var _a, _b; const name = this.varName(); if (!name) { throw new Error('Variable rendering failed, the variable name is empty.'); } // Split variable name into parts and validate // TODO: Add support for longer dotted path variable names const parts = name.trim().split('.'); if (parts.length == 1) { // Assume the var is coming from the 'temp' scope parts.unshift('temp'); } else if (parts.length > 2) { throw new Error(`PromptParser: invalid variable name of '${name}' specified.`); } // Check for special cased variables first let value; switch (parts[0]) { case 'activity': // Return activity field value = (_a = this.caseInsensitiveFindValue(context.activity, parts[1])) !== null && _a !== void 0 ? _a : ''; break; default: { // Find referenced state entry const entry = this.caseInsensitiveFindValue(state, parts[0]); if (!entry) { throw new Error(`PromptParser: invalid variable name of '${name}' specified. Couldn't find a state named '${parts[0]}'.`); } // Return state field value = (_b = this.caseInsensitiveFindValue(entry.value, parts[1])) !== null && _b !== void 0 ? _b : ''; break; } } // Return value return VarBlock.formatValue(value); } static formatValue(value) { return typeof value == 'object' || Array.isArray(value) ? (0, yaml_1.stringify)(value) : value != undefined ? value.toString() : ''; } static hasVarPrefix(text) { return !!text && text.length > 0 && text[0] === VarBlock.Prefix; } static isValidVarName(text) { return /^[a-zA-Z0-9_.]*$/.test(text); } varName() { return this.content.length < 2 ? '' : this.content.slice(1); } caseInsensitiveFindValue(obj, property) { if (Object.prototype.hasOwnProperty.call(obj, property)) { return obj[property]; } else { const propKey = property.toLowerCase(); for (const key in obj) { if (key.toLowerCase() == propKey) { return obj[key]; } } return undefined; } } } exports.VarBlock = VarBlock; VarBlock.Prefix = '$'; //# sourceMappingURL=VarBlock.js.map