From acecb26fa82ea501cb9399e9bd9520fec4e840f6 Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Thu, 12 Dec 2024 00:13:41 +0000 Subject: [PATCH] new Template class --- public/js/varien/js.js | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/public/js/varien/js.js b/public/js/varien/js.js index ac20d0c1c..45b9ded57 100644 --- a/public/js/varien/js.js +++ b/public/js/varien/js.js @@ -664,7 +664,7 @@ Calendar.initialize = async function(event) { class Template { - static #DEFAULT_PATTERN = /(?:^|.|\r|\n)(#\{(.*?)\})/g; + static #DEFAULT_PATTERN = /#\{(.*?)\}/g; /** * Creates a Template object for string interpolation @@ -687,26 +687,24 @@ class Template throw new TypeError('Data object cannot be null or undefined'); } - return this.template.replaceAll(this.pattern, (match, prefix = '', expr) => { + return this.template.replace(this.pattern, (match, expr) => { try { - // Handle escape sequences - if (prefix === '\\') return match; - - // Parse nested properties using optional chaining - const value = expr.split('.') - .reduce((obj, prop) => { - // Handle array notation [index] - const arrayMatch = prop.match(/(\w+)\[(\d+)\]/); - if (arrayMatch) { - const [, propName, index] = arrayMatch; - return obj?.[propName]?.[index]; - } - return obj?.[prop]; - }, data); - - return `${prefix}${value ?? ''}`; + let value = data; + const parts = expr.trim().split('.'); + + for (const part of parts) { + if (part.includes('[')) { + const [name, index] = part.split('['); + const cleanIndex = parseInt(index.replace(']', '')); + value = value[name][cleanIndex]; + } else { + value = value[part]; + } + } + + return value ?? ''; } catch (error) { - return `${prefix}`; + return ''; } }); }