1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add ignore-case flag to Switch regex rule

closes #366
This commit is contained in:
Nick O'Leary 2015-10-04 22:14:49 +01:00
parent c1e8370916
commit 8b2b1669b5
4 changed files with 96 additions and 59 deletions

View File

@ -425,8 +425,10 @@
"property": "Property",
"rule": "rule"
},
"and": "and",
"checkall": "checking all rules",
"stopfirst": "stopping after first match",
"ignorecase": "ignore case",
"rules": {
"btwn":"is between",
"cont":"contains",

View File

@ -82,6 +82,9 @@
{v:"else",t:this._("switch.rules.else")}
];
var andLabel = this._("switch.and");
var caseLabel = this._("switch.ignorecase");
function resizeRule(rule,width) {
var selectField = rule.find("select");
var type = selectField.children("option:selected").val();
@ -99,8 +102,9 @@
selectField.width(selectWidth);
if (type === "btwn") {
btwnField1.width((width-288)/2);
btwnField2.width((width-288)/2);
var labelWidth = rule.find(".node-input-tule-btwn-label").width();
btwnField1.width((width-256-labelWidth)/2);
btwnField2.width((width-256-labelWidth)/2);
} else {
if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
// valueField.hide();
@ -113,7 +117,7 @@
function generateRule(i,rule) {
var container = $('<li/>',{style:"background: #fff; margin:0; padding:8px 0px; border-bottom: 1px solid #ccc;"});
var row = $('<div/>').appendTo(container);
var row2 = $('<div/>',{style:"padding-top: 5px; text-align: right;"}).appendTo(container);
var row2 = $('<div/>',{style:"padding-top: 5px; padding-left: 175px;"}).appendTo(container);
$('<i style="color: #eee; cursor: move;" class="node-input-rule-handle fa fa-bars"></i>').appendTo(row);
var selectField = $('<select/>',{style:"width:120px; margin-left: 5px; text-align: center;"}).appendTo(row);
@ -124,7 +128,7 @@
var valueField = $('<input/>',{class:"node-input-rule-value",type:"text",style:"margin-left: 5px; width: 145px;"}).appendTo(row);
var btwnField = $('<span/>').appendTo(row);
var btwnValueField = $('<input/>',{class:"node-input-rule-btwn-value",type:"text",style:"margin-left: 5px; width: 50px;"}).appendTo(btwnField);
btwnField.append(" and ");
var btwnAndLabel = $('<span/>',{class:"node-input-tule-btwn-label"}).text(" "+andLabel+" ").appendTo(btwnField);
var btwnValue2Field = $('<input/>',{class:"node-input-rule-btwn-value2",type:"text",style:"width: 50px;margin-left:2px;"}).appendTo(btwnField);
var finalspan = $('<span/>',{style:"float: right;margin-right: 10px;"}).appendTo(row);
@ -133,6 +137,8 @@
var deleteButton = $('<a/>',{href:"#",class:"editor-button editor-button-small", style:"margin-top: 7px; margin-left: 5px;"}).appendTo(finalspan);
$('<i/>',{class:"fa fa-remove"}).appendTo(deleteButton);
var caseSensitive = $('<input/>',{id:"node-input-rule-case-"+i,class:"node-input-rule-case",type:"checkbox",style:"width:auto;vertical-align:top"}).appendTo(row2);
$('<label/>',{for:"node-input-rule-case-"+i,style:"margin-left: 3px;"}).text(caseLabel).appendTo(row2);
selectField.change(function() {
var width = $("#node-input-rule-container").width();
resizeRule(container,width);
@ -149,6 +155,11 @@
valueField.show();
}
}
if (type === "regex") {
row2.show();
} else {
row2.hide();
}
});
deleteButton.click(function() {
@ -171,6 +182,11 @@
} else if (typeof rule.v != "undefined") {
valueField.val(rule.v);
}
if (rule.case) {
caseSensitive.prop('checked',true);
} else {
caseSensitive.prop('checked',false);
}
selectField.change();
}
@ -245,6 +261,9 @@
} else {
r.v = rule.find(".node-input-rule-value").val();
}
if (type === "regex") {
r.case = rule.find(".node-input-rule-case").prop("checked");
}
}
node.rules.push(r);
});

View File

@ -1,5 +1,5 @@
/**
* Copyright 2013 IBM Corp.
* Copyright 2013, 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -25,7 +25,7 @@ module.exports = function(RED) {
'gte': function(a, b) { return a >= b; },
'btwn': function(a, b, c) { return a >= b && a <= c; },
'cont': function(a, b) { return (a + "").indexOf(b) != -1; },
'regex': function(a, b) { return (a + "").match(new RegExp(b)); },
'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); },
@ -60,7 +60,7 @@ module.exports = function(RED) {
var rule = node.rules[i];
var test = prop;
if (rule.t == "else") { test = elseflag; elseflag = true; }
if (operators[rule.t](test,rule.v, rule.v2)) {
if (operators[rule.t](test,rule.v, rule.v2, rule.case)) {
onward.push(msg);
elseflag = false;
if (node.checkall == "false") { break; }

View File

@ -203,6 +203,22 @@ describe('switch Node', function() {
genericSwitchTest("regex", "[abc]+", true, true, "abbabac", done);
});
it('should match regex with ignore-case flag set true', function(done) {
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"regex","v":"onetwothree","case":true}],checkall:true,outputs:1,wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
customFlowSwitchTest(flow, true, "oneTWOthree", 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:[]}];
customFlowSwitchTest(flow, false, "oneTWOthree", done);
});
it('should not match regex with ignore-case flag set false', function(done) {
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"regex","v":"onetwothree",case:false}],checkall:true,outputs:1,wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
customFlowSwitchTest(flow, false, "oneTWOthree", done);
});
it('should return nothing when the payload doesn\'t match regex', function(done) {
genericSwitchTest("regex", "\\d+", true, false, "This is not a digit", done);
});