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

Add timestamp as a default typedInput

and update Inject and change nodes to match,
and add some tests.
This commit is contained in:
Dave Conway-Jones 2016-04-18 14:38:32 +01:00
parent f55f85aa14
commit 59b34c2b3f
11 changed files with 53 additions and 26 deletions

View File

@ -22,7 +22,8 @@
num: {value:"num",label:"number",icon:"red/images/typedInput/09.png",validate:/^[+-]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)?$/},
bool: {value:"bool",label:"boolean",icon:"red/images/typedInput/bool.png",options:["true","false"]},
json: {value:"json",label:"JSON",icon:"red/images/typedInput/json.png", validate: function(v) { try{JSON.parse(v);return true;}catch(e){return false;}}},
re: {value:"re",label:"regular expression",icon:"red/images/typedInput/re.png"}
re: {value:"re",label:"regular expression",icon:"red/images/typedInput/re.png"},
date: {value:"date",label:"timestamp",hasValue:false}
};
var nlsd = false;

View File

@ -242,7 +242,7 @@
$("#node-input-payload").typedInput({
default: 'str',
typeField: $("#node-input-payloadType"),
types:['flow','global','str','num','bool','json',{value:"date",label:this._("inject.timestamp"),hasValue:false}]
types:['flow','global','str','num','bool','json','date']
});
$("#inject-time-type-select").change(function() {

View File

@ -149,7 +149,7 @@
.appendTo(row2);
var propertyValue = $('<input/>',{style:"width:250px",class:"node-input-rule-property-value",type:"text"})
.appendTo(row2)
.typedInput({default:'str',types:['msg','flow','global','str','num','bool','json']});
.typedInput({default:'str',types:['msg','flow','global','str','num','bool','json','date']});
var row3_1 = $('<div/>').appendTo(row3);
$('<div/>',{style:"display:inline-block;text-align:right; width:120px; padding-right:10px; box-sizing:border-box;"})

View File

@ -100,6 +100,8 @@ module.exports = function(RED) {
value = node.context().flow.get(rule.to);
} else if (rule.tot === 'global') {
value = node.context().global.get(rule.to);
} else if (rule.tot === 'date') {
value = Date.now();
}
if (rule.t === 'change') {
if (rule.fromt === 'msg' || rule.fromt === 'flow' || rule.fromt === 'global') {

View File

@ -222,7 +222,8 @@
"num": "number",
"re": "regular expression",
"bool": "boolean",
"json": "JSON"
"json": "JSON",
"date": "timestamp"
}
}
}

View File

@ -62,7 +62,6 @@ module.exports = {
}
themeSettings = null;
theme = settings.editorTheme;
},
app: function() {

View File

@ -182,6 +182,8 @@ function evaluateNodeProperty(value, type, node, msg) {
return JSON.parse(value);
} else if (type === 're') {
return new RegExp(value);
} else if (type === 'date') {
return Date.now();
} else if (type === 'msg' && msg) {
return getMessageProperty(msg,value);
} else if (type === 'flow' && node) {

View File

@ -242,7 +242,6 @@ describe('change Node', function() {
});
});
it('changes the value to a number', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":"123","tot":"num"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
@ -260,6 +259,7 @@ describe('change Node', function() {
changeNode1.receive({payload:""});
});
});
it('changes the value to a js object', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":'{"a":123}',"tot":"json"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
@ -278,6 +278,24 @@ describe('change Node', function() {
});
});
it('sets the value of the message property to the current timestamp', function(done) {
var flow = [{"id":"changeNode1","type":"change","rules":[{"t":"set","p":"ts","pt":"msg","to":"","tot":"date"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
var rule = helper.getNode("changeNode1").rules[0];
helperNode1.on("input", function(msg) {
try {
(Date.now() - msg.ts).should.be.approximately(0,50);
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:Date.now()});
});
});
});
describe('#change', function() {

View File

@ -210,6 +210,10 @@ describe("red/util", function() {
var result = util.evaluateNodeProperty('^abc$','re');
result.toString().should.eql("/^abc$/");
});
it('returns date',function() {
var result = util.evaluateNodeProperty('','date');
(Date.now() - result).should.be.approximately(0,50);
});
it('returns msg property',function() {
var result = util.evaluateNodeProperty('foo.bar','msg',{},{foo:{bar:"123"}});
result.should.eql("123");