From f0139f9808136fbe4edb5883d0c885a848d5158a Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Wed, 18 Mar 2015 16:20:21 +0000 Subject: [PATCH] Add multi-rule support to Change node --- nodes/core/logic/15-change.html | 288 ++++++++++++++++-------- nodes/core/logic/15-change.js | 146 +++++++----- public/red/ui/editor.js | 2 + test/nodes/core/logic/15-change_spec.js | 70 +++++- 4 files changed, 343 insertions(+), 163 deletions(-) diff --git a/nodes/core/logic/15-change.html b/nodes/core/logic/15-change.html index b3eae64b3..892641d08 100644 --- a/nodes/core/logic/15-change.html +++ b/nodes/core/logic/15-change.html @@ -1,5 +1,5 @@ diff --git a/nodes/core/logic/15-change.js b/nodes/core/logic/15-change.js index 7324c31f8..f0ebee24c 100644 --- a/nodes/core/logic/15-change.js +++ b/nodes/core/logic/15-change.js @@ -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. @@ -19,71 +19,99 @@ module.exports = function(RED) { function ChangeNode(n) { RED.nodes.createNode(this, n); - this.action = n.action; - this.property = n.property || ""; - this.from = n.from || ""; - this.to = n.to || ""; - this.reg = (n.reg === null || n.reg); - if (this.reg === false) { - this.from = this.from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); + + this.rules = n.rules; + + if (!this.rules) { + var rule = { + t:(n.action=="replace"?"set":n.action), + p:n.property||"" + } + + if (rule.t === "set") { + rule.to = n.to||""; + } else if (rule.t === "change") { + rule.from = n.from||""; + rule.to = n.to||""; + rule.re = (n.reg===null||n.reg); + } + this.rules = [rule]; } + + this.actions = []; + var valid = true; - if (this.action === "change") { - try { - this.re = new RegExp(this.from, "g"); - } catch (e) { - valid = false; - this.error("Invalid 'from' property: "+e.message); + + for (var i=0;i