node-red-contrib-dayjs
Advanced tools
Comparing version 0.1.0 to 0.1.1
49
day.js
@@ -13,2 +13,3 @@ module.exports = function(RED) { | ||
var relativeTime = require('dayjs/plugin/relativeTime') | ||
var customParseFormat = require('dayjs/plugin/customParseFormat') | ||
dayjs.extend(utc) | ||
@@ -20,3 +21,5 @@ dayjs.extend(timezone) | ||
dayjs.extend(relativeTime) | ||
dayjs.extend(customParseFormat) | ||
this.outputFormat = config.outputFormat || 'ISOString'; | ||
@@ -30,10 +33,25 @@ this.costumFormatOutput = config.costumFormatOutput || 'YYYY-MM-DDTHH:mm:ssZ'; | ||
this.manipulateAmount = config.manipulateAmount | ||
this.inputFormat = config.inputFormat | ||
this.inputTimezone = config.inputTimezone | ||
var node = this; | ||
function parsePayload (payload) { | ||
function parsePayload (payload,inputFormat,tz) { | ||
// parse the payload to a day.js instance | ||
// if payload is not parsable, the current timestamp is used | ||
if (inputFormat != '') { | ||
day = dayjs.utc(payload,inputFormat) | ||
} else { | ||
day = dayjs.utc(payload); | ||
if (!day.isValid()) { | ||
day = dayjs.utc() | ||
} | ||
} | ||
if (!day.isValid()) { | ||
day = dayjs.utc() | ||
} | ||
if (tz != '') { | ||
return day.tz(tz,true) | ||
} else { | ||
return day | ||
} | ||
}; | ||
@@ -43,2 +61,4 @@ | ||
function alterTimezone (input,tz,local_time) { | ||
// alter timezone | ||
// TODO: keep time | ||
return input.tz(tz) | ||
@@ -49,3 +69,3 @@ | ||
function formatOutput(input, Format,costumFormatOutput) { | ||
// alter ihe input (day.js instance) to the final result | ||
switch (Format) { | ||
@@ -89,2 +109,3 @@ case 'ISOString': | ||
function manipulateOutput(input,operation,amount,unit) { | ||
// change the input day.js instance | ||
switch (operation) { | ||
@@ -105,6 +126,7 @@ case 'add': | ||
node.on('input', function(msg) { | ||
let msg_input_property = msg.input || node.inputProperty | ||
// get input - output props eg. 'payload' | ||
let msg_inputProperty = msg.input || node.inputProperty | ||
let msg_output_property = msg.output || node.outputProperty | ||
// get manipulation options | ||
let manipulate_operation = msg.operation || node.manipulateOperation | ||
@@ -114,8 +136,16 @@ let manipulate_unit = msg.unit || node.manipulateUnit | ||
let input = msg[msg_input_property] | ||
// get real payload | ||
let input = msg[msg_inputProperty] | ||
let day = parsePayload(input) | ||
let inputFormat = msg.inputFormat || node.inputFormat || '' | ||
let inputTimezone = msg.inputTimezone || node.inputTimezone || '' | ||
// parse the input | ||
let day = parsePayload(input,inputFormat,inputTimezone) | ||
// alter Timezone for the output | ||
let day_tz = alterTimezone(day,node.outputTimezone,false) | ||
let day_output = dayjs() | ||
// if operation and unit is given, we can manipulate the date | ||
if(manipulate_operation != "" && manipulate_unit != "") { | ||
@@ -127,2 +157,3 @@ day_output = manipulateOutput(day,manipulate_operation,manipulate_Amount,manipulate_unit) | ||
// final output format | ||
msg[msg_output_property] = formatOutput(day_output,node.outputFormat,node.costumFormatOutput) | ||
@@ -129,0 +160,0 @@ node.send(msg); |
{ | ||
"name": "node-red-contrib-dayjs", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Wrapper of dayjs for Node-RED", | ||
@@ -5,0 +5,0 @@ "main": "day.js", |
@@ -5,55 +5,55 @@ # node-red-contrib-dayjs | ||
A simple [Node-RED](https://nodered.org/) node which wrapps the JavaScript library [Day.js](https://day.js.org/en/), an alternative to Moment.js | ||
A simple [Node-RED](https://nodered.org/) node that integrates the versatile JavaScript library [Day.js](https://day.js.org/en/), offering an alternative to Moment.js. | ||
## Install | ||
To install - either use the manage palette option in the editor, or change to your Node-RED user directory. | ||
You can install this node via the manage palette option in the Node-RED editor or by navigating to your Node-RED user directory and running: | ||
cd ~/.node-red | ||
npm install node-red-contrib-dayjs | ||
```bash | ||
cd ~/.node-red | ||
npm install node-red-contrib-dayjs | ||
``` | ||
## Usage | ||
<h3>Input</h3> | ||
Expect a parsable String: | ||
<code> | ||
### Input | ||
This node expects a parsable string in the following formats: | ||
'2018-04-04T16:00:00.000Z' | ||
'2018-04-13 19:18:17.040+02:00' | ||
'2018-04-13 19:18' | ||
1318781876406 | ||
1318781876 | ||
</code> | ||
If the Input i not parsable, the current time us used | ||
<h3>Manipulate</h3> | ||
<h4>Operations</h4> | ||
<p>Message Property: <code>msg.operation</code></p> | ||
<ul> | ||
<li>add</li> | ||
<li>subtract</li> | ||
<li>startOf</li> | ||
<li>endOf</li> | ||
</ul> | ||
<h4>Units</h4> | ||
<p>Message Property: <code>msg.unit</code></p> | ||
<ul> | ||
<li>year</li> | ||
<li>quarter</li> | ||
<li>month</li> | ||
<li>week</li> | ||
<li>isoWeek</li> | ||
<li>day</li> | ||
<li>hour</li> | ||
<li>minute</li> | ||
<li>second</li> | ||
</ul> | ||
<h4>Amount</h4> | ||
<p>Message Property: <code>msg.amount</code></p> | ||
<h3>Output</h3> | ||
By default, the Output is an ISO String, but i can be changed.<br> | ||
``` | ||
'2018-04-04T16:00:00.000Z' | ||
'2018-04-13 19:18:17.040+02:00' | ||
'2018-04-13 19:18' | ||
1318781876406 | ||
1318781876 | ||
``` | ||
If the input is not parsable, the current time is used. | ||
You can also set a Costum Output Format (<a href="https://day.js.org/docs/en/display/format">Format Options</a>) | ||
### Manipulate | ||
You can also change the Timezone (e.g. 'utc', 'Europe/Paris') | ||
#### Operations | ||
The operation to perform is specified via the `msg.operation` property, which supports the following options: | ||
- add | ||
- subtract | ||
- startOf | ||
- endOf | ||
#### Units | ||
Specify the unit of time using the `msg.unit` property, which can take the values: | ||
- year | ||
- quarter | ||
- month | ||
- week | ||
- isoWeek | ||
- day | ||
- hour | ||
- minute | ||
- second | ||
#### Amount | ||
The amount of time to manipulate is provided via the msg.amount property. | ||
### Output | ||
By default, the output is provided as an ISO String, but it can be customized. You can also specify a custom output format using the ([Format Options](https://day.js.org/docs/en/display/format)). | ||
Additionally, you can adjust the timezone (e.g., 'utc', 'Europe/Paris')). | ||
## Example | ||
@@ -63,2 +63,2 @@ | ||
## Acknowledgments | ||
## Acknowledgments |
@@ -34,2 +34,3 @@ var helper = require("node-red-node-test-helper"); | ||
outputFormat: "ISOString", | ||
inputFormat: "", | ||
costumFormatOutput: "YYYY-MM-DDTHH:mm:ssZ", | ||
@@ -60,2 +61,31 @@ outputTimezone: "utc", | ||
it('parse costum input format to ISO 8601', function (done) { | ||
var flow = [{ id: "n_day", type: "day.js", name: "dayjs test" , | ||
outputFormat: "ISOString", | ||
inputFormat: "YYYY/MM/DD", | ||
costumFormatOutput: "YYYY-MM-DDTHH:mm:ssZ", | ||
outputTimezone: "utc", | ||
inputProperty: "payload", | ||
outputProperty: "payload", | ||
manipulateOperation: "", | ||
manipulateUnit: "", | ||
manipulateAmount: "", | ||
wires: [["n_helper"]] }, | ||
{ id: "n_helper", type: "helper" }]; | ||
helper.load(day, flow, function () { | ||
var n_helper = helper.getNode("n_helper"); | ||
var n_day = helper.getNode("n_day"); | ||
n_helper.on("input", function (msg) { | ||
try { | ||
msg.should.have.property('payload', '1970-01-01T00:00:00.000Z'); | ||
done(); | ||
} catch(err) { | ||
done(err); | ||
} | ||
}); | ||
n_day.receive({ payload: '1970/01/01' }); | ||
}); | ||
}); | ||
it('add one hour to a timestamp', function (done) { | ||
@@ -89,3 +119,3 @@ var flow = [{ id: "n_day", type: "day.js", name: "dayjs test" , | ||
it('output Costum Format', function (done) { | ||
it('output costum format', function (done) { | ||
var flow = [{ id: "n_day", type: "day.js", name: "dayjs test" , | ||
@@ -147,2 +177,32 @@ outputFormat: "Costum", | ||
it('parse costum input format with Timezone to ISO 8601', function (done) { | ||
var flow = [{ id: "n_day", type: "day.js", name: "dayjs test" , | ||
outputFormat: "ISOString", | ||
inputFormat: "YYYY/MM/DD", | ||
inputTimezone: "America/Toronto", | ||
costumFormatOutput: "YYYY-MM-DDTHH:mm:ssZ", | ||
outputTimezone: "Europe/Paris", | ||
inputProperty: "payload", | ||
outputProperty: "payload", | ||
manipulateOperation: "", | ||
manipulateUnit: "", | ||
manipulateAmount: "", | ||
wires: [["n_helper"]] }, | ||
{ id: "n_helper", type: "helper" }]; | ||
helper.load(day, flow, function () { | ||
var n_helper = helper.getNode("n_helper"); | ||
var n_day = helper.getNode("n_day"); | ||
n_helper.on("input", function (msg) { | ||
try { | ||
msg.should.have.property('payload', '1970-01-01T05:00:00.000Z'); | ||
done(); | ||
} catch(err) { | ||
done(err); | ||
} | ||
}); | ||
n_day.receive({ payload: '1970/01/01' }); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31403
8
760
63
1