From fcf08090a3ef44985859be3a1b9af66de120e364 Mon Sep 17 00:00:00 2001 From: Yoann <41453809+YoannMourot@users.noreply.github.com> Date: Fri, 5 Nov 2021 17:48:44 +0100 Subject: [PATCH] Allow hyphen seperated template variables Allowing template variables like ^foo-bar=barbar:string. This was causing an error `Cannot initialize connector "rest": Cannot read property 'type' of undefined' at boot.` https://regex101.com/r/HoCufm/1 --- lib/template.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/template.js b/lib/template.js index 2635e4b..2527d77 100644 --- a/lib/template.js +++ b/lib/template.js @@ -47,12 +47,12 @@ JsonTemplate.prototype.compile = function() { function parse(item) { let matched = false; - const templates = /\{(([\!\^]?[a-zA-Z\$_][\w\$]*)\s*(=([^:]+))?(:(\w+))?)\}/g; + const templates = /\{(([\!\^]?[a-zA-Z\$_][\w\$-]*)\s*(=([^:]+))?(:(\w+))?)\}/g; /* eslint-disable no-cond-assign */ while (match = templates.exec(item)) { /* eslint-enable no-cond-assign */ matched = true; - const param = /([[\!\^]?[a-zA-Z\$_][\w\$]*)\s*(=([^:]+))?(:(\w+))?/.exec(match[1]); + const param = /([[\!\^]?[a-zA-Z\$_][\w\$-]*)\s*(=([^:]+))?(:(\w+))?/.exec(match[1]); if (!param || !param[1]) { throw new Error(g.f('Invalid parameter: %s', match[1])); } @@ -120,7 +120,7 @@ JsonTemplate.prototype.build = function(parameters) { function transform(obj) { return traverse(obj).map(function(item) { const ctx = this; - const templates = /\{(([\!\^]?[a-zA-Z\$_][\w\$]*)\s*(=([^:\{\}]+))?(:(\w+))?)\}/g; + const templates = /\{(([\!\^]?[a-zA-Z\$_][\w\$-]*)\s*(=([^:\{\}]+))?(:(\w+))?)\}/g; function build(item) { let match = null; const parsed = []; @@ -134,7 +134,7 @@ JsonTemplate.prototype.build = function(parameters) { parsed.push(item.substring(index, match.index)); } // The variable pattern is {name=defaultValue:type} - const param = /([\!\^]?[a-zA-Z\$_][\w\$]*)\s*(=([^:\{\}]+))?(:(\w+))?/.exec(match[1]); + const param = /([\!\^]?[a-zA-Z\$_][\w\$-]*)\s*(=([^:\{\}]+))?(:(\w+))?/.exec(match[1]); if (!param || !param[1]) { throw new Error(g.f('Invalid parameter: %s', match[1])); }