Skip to content

Commit

Permalink
Substitute Parser: Handle custom date format from template (#2207)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenberttpingol authored Nov 8, 2023
1 parent 21abbc8 commit 80fec7f
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion modules/src/xibo-substitutes-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jQuery.fn.extend({
) {
const items = [];
const parser = new RegExp('\\[.*?\\]', 'g');
const pipeParser = new RegExp('\\|{1}', 'g');
this.each(function() {
// Parse the template for a list of things to substitute, and match those
// with content from items.
Expand All @@ -42,6 +43,17 @@ jQuery.fn.extend({
.replace(']', '');
variable = variable.charAt(0).toLowerCase() + variable.substring(1);

// Check if variable has its own formatting
// Then, parse it and use later as dateFormat
let formatFromTemplate = null;

if (variable.match(pipeParser) !== null &&
variable.match(pipeParser).length === 1) {
const variableWithFormat = variable.split('|');
formatFromTemplate = variableWithFormat[1];
variable = variableWithFormat[0];
}

if (mapping[variable]) {
variable = mapping[variable];
}
Expand All @@ -55,7 +67,8 @@ jQuery.fn.extend({
// Is it a date field?
dateFields.forEach((field) => {
if (field === variable) {
value = moment(value).format(dateFormat);
value = moment(value).format(formatFromTemplate !== null ?
formatFromTemplate : dateFormat);
}
});
}
Expand Down

0 comments on commit 80fec7f

Please sign in to comment.