@swrve/smarttv-sdk
Version:
Swrve marketing engagement platform SDK for SmartTV OTT devices
101 lines (100 loc) • 4.29 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TextTemplating = void 0;
const SwrveLogger_1 = __importDefault(require("./SwrveLogger"));
class TextTemplating {
static applyTextTemplatingToString(text, properties) {
let templatedText = text;
if (!templatedText || templatedText.length === 0) {
return templatedText;
}
while (this.hasPatternMatch(templatedText)) {
const match = templatedText.match(this.patternMatch);
if (match == null) {
throw new Error("Could not resolve personalization");
}
const templateFullValue = match[0];
const fallback = this.getFallBack(templateFullValue);
let property = match[1];
if (fallback != null) {
property = property.substring(0, property.indexOf('|fallback="')); /* remove fallback text */
}
if (properties != null &&
!(properties[property] === undefined) &&
properties[property].length > 0) {
templatedText = templatedText.replace(templateFullValue, properties[property]);
}
else if (fallback != null) {
templatedText = templatedText.replace(templateFullValue, fallback);
}
else {
throw new Error("TextTemplating: Missing property value for key " + property);
}
}
return templatedText;
}
static applyTextTemplatingToJSON(json, properties) {
let templatedText = json;
if (!templatedText || templatedText.length === 0) {
return templatedText;
}
while (this.hasPatternMatch(templatedText)) {
const match = templatedText.match(this.patternMatch);
if (match == null) {
throw new Error("Could not resolve personalization");
}
const templateFullValue = match[0];
const fallback = this.getFallBackJSON(templateFullValue);
let property = match[1];
if (fallback != null) {
property = property.substring(0, property.indexOf('|fallback=\\"')); /* remove fallback text */
}
if (properties != null &&
!(properties[property] === undefined) &&
properties[property].length > 0) {
templatedText = templatedText.replace(templateFullValue, properties[property]);
}
else if (fallback != null) {
templatedText = templatedText.replace(templateFullValue, fallback);
}
else {
throw new Error("TextTemplating: Missing property value for key " + property);
}
}
return templatedText;
}
/* Checks if the pattern exists within a given piece of text */
static hasPatternMatch(text) {
if (text == null) {
return false;
}
return this.patternMatch.test(text);
}
/* Example of expected template syntax: ${item.property|fallback="fallback text"} */
static getFallBack(templateFullValue) {
const fallback = null;
const match = templateFullValue.match(TextTemplating.patternFallbackMatch);
if (match != null) {
return match[1];
}
return fallback;
}
/* Example of expected template syntax:
{\"key\":"${item.property|fallback=\"fallback text\"}"} */
static getFallBackJSON(templateFullValue) {
const fallback = null;
const match = templateFullValue.match(TextTemplating.patternJSONFallbackMatch);
if (match != null) {
SwrveLogger_1.default.debug("getFallbackJSON Got a Match: " + match[1]);
return match[1];
}
return fallback;
}
}
exports.TextTemplating = TextTemplating;
TextTemplating.patternMatch = /\$\{([^\}]*)\}/i; /* match any content beginning with ${ and ending in } */
TextTemplating.patternFallbackMatch = /\|fallback=\"([^\}]*)"\}/i;
TextTemplating.patternJSONFallbackMatch = /\|fallback=\\"([^\}]*)\\"\}/i;