Skip to content

Commit

Permalink
Allow typedInput timestamp to specify format
Browse files Browse the repository at this point in the history
  • Loading branch information
knolleary committed Mar 4, 2024
1 parent 6802539 commit 7115455
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,30 @@
}
},
re: {value:"re",label:"regular expression",icon:"red/images/typedInput/re.svg"},
date: {value:"date",label:"timestamp",icon:"fa fa-clock-o",hasValue:false},
date: {
value:"date",
label:"timestamp",
icon:"fa fa-clock-o",
options:[
{
label: 'timestamp',
value: ''
},
{
label: 'ISO 8601',
value: 'iso'
},
{
value: 'YYYY-MM-DD HH:mm:ss'
},
{
value: 'YYYY-MM-DD'
},
{
value: 'HH:mm:ss'
},
]
},
jsonata: {
value: "jsonata",
label: "expression",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ module.exports = function(RED) {
});
return
} else if (rule.tot === 'date') {
value = Date.now();
value = RED.util.evaluateNodeProperty(rule.to, rule.tot, node)
} else if (rule.tot === 'jsonata') {
RED.util.evaluateJSONataExpression(rule.to,msg, (err, value) => {
if (err) {
Expand Down
8 changes: 7 additions & 1 deletion packages/node_modules/@node-red/util/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,13 @@ function evaluateNodeProperty(value, type, node, msg, callback) {
} else if (type === 're') {
result = new RegExp(value);
} else if (type === 'date') {
result = Date.now();
if (!value) {
result = Date.now();
} else if (value === 'iso') {
result = (new Date()).toISOString()
} else {
result = moment().format(value)
}
} else if (type === 'bin') {
var data = JSON.parse(value);
if (Array.isArray(data) || (typeof(data) === "string")) {
Expand Down
9 changes: 8 additions & 1 deletion test/unit/@node-red/util/lib/util_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,17 @@ describe("@node-red/util/util", function() {
result = util.evaluateNodeProperty('','bool');
result.should.be.false();
});
it('returns date',function() {
it('returns date - default format',function() {
var result = util.evaluateNodeProperty('','date');
(Date.now() - result).should.be.approximately(0,50);
});

it('returns date - iso format',function() {
var result = util.evaluateNodeProperty('iso','date');
// 2023-12-04T16:51:04.429Z
/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d+Z$/.test(result).should.be.true()
});

it('returns bin', function () {
var result = util.evaluateNodeProperty('[1, 2]','bin');
result[0].should.eql(1);
Expand Down

0 comments on commit 7115455

Please sign in to comment.