pull/5387/merge
Dave Conway-Jones 2026-03-24 10:15:09 -04:00 committed by GitHub
commit 5a3fc97bfc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 7 deletions

View File

@ -119,7 +119,7 @@
function createValueField(row, defaultType){
return $('<input/>',{class:"node-input-rule-value",type:"text",style:"width: 100%;"}).appendTo(row)
.typedInput({default:defaultType||'str',types:['msg','flow','global','str','num','jsonata','env',previousValueType]});
.typedInput({default:defaultType||'str',types:['msg','flow','global','str','num','jsonata','env','re',previousValueType]});
}
function createNumValueField(row, defaultType){
@ -230,7 +230,6 @@
oneditprepare: function() {
var node = this;
$("#node-input-property").typedInput({default:this.propertyType||'msg',types:['msg','flow','global','jsonata','env']});
var outputCount = $("#node-input-outputs").val("{}");
@ -276,7 +275,7 @@
});
var inputRows = $('<div></div>',{style:"flex-grow:1"}).appendTo(container);
var row = $('<div></div>',{style:"display: flex;"}).appendTo(inputRows);
var row2 = $('<div/>',{style:"display: flex; padding-top: 5px; padding-left: 175px;"}).appendTo(inputRows);
var row2 = $('<div/>',{style:"display: flex; padding-top: 5px; padding-left: 155px;"}).appendTo(inputRows);
var row3 = $('<div/>',{style:"display: flex; padding-top: 5px; align-items: center"}).appendTo(inputRows);
var row4 = $('<div/>',{style:"visibility: hidden; height: 0px;"}).appendTo(inputRows);
@ -302,7 +301,6 @@
var rowInputCell = $('<div>',{style:"flex-grow:1; margin-left: 5px;"}).appendTo(row);
var valueField = null;
var numValueField = null;
var expValueField = null;
@ -362,6 +360,9 @@
if (type === "regex") {
row2.show();
row3.hide();
if ( valueField.typedInput('type') === "str") {
valueField.typedInput('type','re');
}
} else if ((type === "btwn") || (type === "index")) {
row2.hide();
row3.show();
@ -409,6 +410,9 @@
} else if (rule.t === "jsonata_exp") {
expValueField = createExpValueField(rowInputCell,rule.vt||'jsonata');
expValueField.typedInput('value',rule.v);
} else if (rule.t === "regex") {
valueField = createValueField(rowInputCell,rule.vt||'re');
valueField.typedInput('value',rule.v);
} else if (typeof rule.v != "undefined") {
valueField = createValueField(rowInputCell,rule.vt||'str');
valueField.typedInput('value',rule.v);

View File

@ -26,11 +26,11 @@ module.exports = function(RED) {
'gte': function(a, b) { return a >= b; },
'btwn': function(a, b, c) { return (a >= b && a <= c) || (a <= b && a >= c); },
'cont': function(a, b) { return (a + "").indexOf(b) != -1; },
'regex': function(a, b, c, d) { return (a + "").match(new RegExp(b,d?'i':'')); },
'true': function(a) { return a === true; },
'false': function(a) { return a === false; },
'null': function(a) { return (typeof a == "undefined" || a === null); },
'nnull': function(a) { return (typeof a != "undefined" && a !== null); },
'regex': function(a, b, c, d) { return (a + "").matchAll(new RegExp(b,d?'ig':'g')); },
'empty': function(a) {
if (typeof a === 'string' || Array.isArray(a) || Buffer.isBuffer(a)) {
return a.length === 0;
@ -197,7 +197,22 @@ module.exports = function(RED) {
state.elseflag = true;
}
try {
if (operators[rule.t](property,v1,v2,rule.case,msg.parts)) {
const r = operators[rule.t](property,v1,v2,rule.case,msg.parts)
if (r) {
if (rule.t === "regex") {
msg.matches = [];
for (const match of r) {
const le = match.length
const g = []
for (let i = 1; i<le; i++) {
g.push(match[i])
}
msg.matches.push({match: match[0], index: match.index, groups: g });
}
if (msg.matches.length === 0) {
state.onward.push(null);
}
}
state.onward.push(msg);
state.elseflag = false;
if (node.checkall == "false") {
@ -216,7 +231,7 @@ module.exports = function(RED) {
});
}
function applyRules(node, msg, property,state,done) {
function applyRules(node, msg, property, state, done) {
if (!state) {
if (node.rules.length === 0) {
done(undefined, []);

View File

@ -468,6 +468,19 @@ describe('switch Node', function() {
{id:"helperNode1", type:"helper", wires:[]}];
customFlowSwitchTest(flow, true, "oneTWOthree", done);
});
it('should match regex and return matches', function(done) {
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"regex","v":"(b[aio]..)","vt":"str"}],checkall:true,outputs:1,wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
customFlowMessageSwitchTest(flow, true, {payload:"foobishbashboshpoo",matches:[{"match":"bish","index":3,"groups":["bish"]},{"match":"bash","index":7,"groups":["bash"]},{"match":"bosh","index":11,"groups":["bosh"]}]}, done);
});
it('should match regex and return matches - 2', function(done) {
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"regex","v":"[b][iao]","vt":"str"}],checkall:true,outputs:1,wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
customFlowMessageSwitchTest(flow, true, {payload:"foobishbashboshpoo",matches:[{"match":"bi","index":3,"groups":[]},{"match":"ba","index":7,"groups":[]},{"match":"bo","index":11,"groups":[]}]}, done);
});
it('should not match regex with ignore-case flag unset', function(done) {
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"regex","v":"onetwothree"}],checkall:true,outputs:1,wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];