Skip to content

Commit

Permalink
Various fixes & bug fix from Jacques44
Browse files Browse the repository at this point in the history
  • Loading branch information
TotallyInformation committed Jun 12, 2016
1 parent 90a50b5 commit 5b8f607
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 27 deletions.
26 changes: 26 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"env": {
"node": true,
"browser": false,
"es6": true
},
"extends": "eslint:recommended",
"rules": {
"indent": [
"error",
"tab"
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"warn",
"single"
],
"semi": [
"warn",
"never"
]
}
}
44 changes: 44 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# compiled output
/dist
/tmp

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# Bower Modules
bower_components

# misc
/.sass-cache

#################
## Eclipse
#################
Expand Down
1 change: 0 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,3 @@ Apache License
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Run the following command in the root directory of your Node-RED install
npm install node-red-contrib-moment

While in development, install with:

npm install https://github.com/TotallyInformation/node-red-contrib-moment/tarball/master

#Updates
Expand Down Expand Up @@ -55,7 +55,7 @@ There are 5 parameters to the node.
Summary of things I'd like to do with the moment node (not necessarily immediately):

* [ ] Add a combo box to the Format field with common formats pre-populated
Combo boxes are fiddly in HTML.
Combo boxes are fiddly in HTML.
* [ ] Improve the error messages when Moment.JS fails to interpret the input (say why)
* [ ] Allow more input date/time formats - turns out Moment.JS doesn't really help here. At present, I see too many input failures from US/UK date formats, etc.
It would be great if I could parse "human" inputs like "tomorrow" and "2 minutes from now". We can output them now but not input them.
Expand All @@ -64,15 +64,15 @@ Summary of things I'd like to do with the moment node (not necessarily immediate

Maybe add a dropdown with a country code to give a hint.

#License
#License

This code is Open Source under an Apache 2 License. Please see the [apache2-license.txt file](https://github.com/TotallyInformation/node-red-contrib-moment/apache2-license.txt) for details.

You may not use this code except in compliance with the License. You may obtain an original copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Please see the
License for the specific language governing permissions and limitations under the License.

Expand Down
10 changes: 5 additions & 5 deletions moment/nrmoment.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<!-- First, the content of the edit dialog is defined. -->
<script type="text/x-red" data-template-name="moment">
<!-- data-template-name identifies the node type this is for -->

<!-- Each of the following divs creates a field in the edit dialog. -->
<!-- Generally, there should be an input for each property of the node. -->
<!-- The for and id attributes identify the corresponding property -->
Expand Down Expand Up @@ -61,7 +61,7 @@
<div class="form-row">
<label for="node-input-locale"><i class="fa fa-flag"></i> Locale</label>
<input type="text" id="node-input-locale" placeholder="en">
</div>
</div>
<div class="form-tips">
Default output format is ISO8601 if the input is a Date object and is a Date object if the input is a string.
See the info sidebar for formatting details.
Expand Down Expand Up @@ -109,7 +109,7 @@
<dt>calendar (aroundNow)</dt>
<dd>e.g. "Last Monday", "Tomorrow 2:30pm"</dd>
<dt>duration</dt>
<dd>e.g. "8 minutes"</dd>
<dd>e.g. "8 minutes"</dd>
</dl>
</script>

Expand All @@ -123,7 +123,7 @@
topic: {value:""}, //, required:true}
input: {value:"payload"},
format: {value:""},
locale: {value:""},
locale: {value:""},
output: {value:"payload"}
},
inputs:1, // set the number of inputs - only 0 or 1
Expand All @@ -137,4 +137,4 @@
return this.name?"node_label_italic":"";
}
});
</script>
</script>
32 changes: 16 additions & 16 deletions moment/nrmoment.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@

module.exports = function(RED) {
"use strict";

// require moment.js (must be installed from package.js as a dependency)
var moment = require("moment")
//parseFormat = require('moment-parseformat') // More input options // NOT WORKING
;

// The main node definition - most things happen in here
function FormatDateTime(n) {
// Create a RED node
RED.nodes.createNode(this,n);

// Store local copies of the node configuration (as defined in the .html)
this.topic = n.topic;
this.input = n.input;
Expand All @@ -39,29 +39,29 @@ module.exports = function(RED) {

// copy "this" object in case we need it in context of callbacks of other functions.
var node = this;

if (n.locale) {
moment.locale(n.locale);
}
}

// send out the message to the rest of the workspace.
// ... this message will get sent at startup so you may not see it in a debug node.
// Define OUTPUT msg...
// Define OUTPUT msg...
//var msg = {};
//msg.topic = this.topic;
//msg.payload = "Hello world !"
//node.send(msg);

// respond to inputs....
node.on('input', function (msg) {
'use strict';
// We will be using eval() so lets get a bit of safety using strict

// If the node's topic is set, copy to output msg
if ( node.topic !== '' ) {
msg.topic = node.topic;
} // If nodes topic is blank, the input msg.topic is already there

// make sure output property is set, if not, assume msg.payload
if ( node.output === '' ) {
node.output = 'payload';
Expand Down Expand Up @@ -89,7 +89,7 @@ module.exports = function(RED) {
}

// We are going to overwrite the output property without warning or permission!

// Get a Moment.JS date/time - NB: the result might not be
// valid since the input might not parse as a date/time
var mDT = moment(inp);
Expand All @@ -100,9 +100,9 @@ module.exports = function(RED) {
} else {
// Handle different format strings. We allow any fmt str that
// Moment.JS supports but also some special formats

// If format not set, assume ISO8601 string if input is a Date otherwise assume Date

if ( node.format === '' ) {
// Is the input a JS Date object? If so, output a string
// Is it a number (Inject outputs a TIMESTAMP which is a number), also output a string
Expand All @@ -123,17 +123,17 @@ module.exports = function(RED) {
// we also allow output as a Javascript Date object
eval('msg.' + node.output + ' = mDT.toDate(); '); // SEE REASONS ABOVE! msg[node.output] = mDT.toDate();
} else if ( node.format.toLowerCase() === 'duration') {
eval('msg.' + node.output + ' = moment.duration(inp).humanize(); ');
eval('msg.' + node.output + ' = moment.duration(inp).humanize(); ');
} else {
// or we assume it is a valid format definition ...
eval('msg.' + node.output + ' = mDT.format(node.format); '); // SEE REASONS ABOVE! msg[node.output] = mDT.format(node.format);
}
}

// in this example just send it straight on... should process it here really
node.send(msg);
});

// Tidy up if we need to
//node.on("close", function() {
// Called when the node is shutdown - eg on redeploy.
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-moment",
"version": "1.0.7",
"version": "1.0.8",
"description": "Node-Red Node that produces a nicely formatted Date/Time string using the Moment.JS library.",
"dependencies": {
"moment": "2.x"
Expand All @@ -9,6 +9,9 @@
"name" : "Julian Knight",
"url" : "https://github.com/totallyinformation"
},
"contributors": [
{"name":"Jacques W", "url":"https://github.com/Jacques44"}
],
"keywords": [
"node-red",
"moment",
Expand Down

0 comments on commit 5b8f607

Please sign in to comment.