Iniital Toon node

This commit is contained in:
Dave Conway-Jones
2025-11-22 12:36:24 +00:00
parent 01e050cc8c
commit c475d39674
6 changed files with 150 additions and 0 deletions

13
parsers/toon/LICENSE Normal file
View File

@@ -0,0 +1,13 @@
Copyright 2025 JS Foundation and other contributors, https://js.foundation/
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a 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 "AS IS" BASIS,
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.

21
parsers/toon/README.md Normal file
View File

@@ -0,0 +1,21 @@
node-red-node-toon
====================
A <a href="http://nodered.org" target="_new">Node-RED</a> node to encode and decode objects to TOON format strings.
Install
-------
Run the following command in your Node-RED user directory - typically `~/.node-red`
npm i node-red-node-toon
Usage
-----
A function that converts the `msg.payload` object to and from TOON format.
If the input is an object it converts it to a TOON encoded string.
If the input is a TOON encoded string it tries to convert it back to an object.

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

26
parsers/toon/package.json Normal file
View File

@@ -0,0 +1,26 @@
{
"name" : "node-red-node-toon",
"version" : "1.0.0",
"description" : "A Node-RED node to encode and decode objects to TOON format strings",
"dependencies" : {
"@toon-format/toon": "^1.3.0"
},
"bundledDependencies": [ "@toon-format/toon" ],
"repository" : {
"type":"git",
"url":"https://github.com/node-red/node-red-nodes.git",
"directory": "/tree/master/parsers/toon"
},
"license": "Apache-2.0",
"keywords": [ "node-red", "toon" ],
"node-red" : {
"nodes" : {
"toon": "toon.js"
}
},
"author": {
"name": "Dave Conway-Jones",
"email": "dceejay@gmail.com",
"url": "http://nodered.org"
}
}

43
parsers/toon/toon.html Normal file
View File

@@ -0,0 +1,43 @@
<script type="text/html" data-template-name="toon">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name">
</div>
<div class="form-row">
<label for="node-input-property"><i class="fa fa-ellipsis-h"></i> <span data-i18n="node-red:common.label.property"></span></label>
<input type="text" id="node-input-property" style="width:70%;"/>
</div>
</script>
<script type="text/javascript">
RED.nodes.registerType('toon',{
category: 'parser',
color:"#DEBD5C",
defaults: {
name: {value:""},
property: {value:"payload",required:true}
},
inputs:1,
outputs:1,
icon: "parser-toon.png",
label: function() {
return this.name||"toon";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function() {
if (this.property === undefined) {
$("#node-input-property").val("payload");
}
$("#node-input-property").typedInput({default:'msg',types:['msg']});
}
});
</script>
<script type="text/html" data-help-name="toon">
<p>A function that converts the chosen property (default <code>msg.payload</code>) to and from TOON format.</p>
<p>If the input is an object it converts it to a TOON encoded string.</p>
<p>If the input is a TOON encoded string it tries to convert it back to an object.</p>
</script>

47
parsers/toon/toon.js Normal file
View File

@@ -0,0 +1,47 @@
module.exports = function(RED) {
"use strict";
const toonlib = require("@toon-format/toon")
function ToonNode(n) {
RED.nodes.createNode(this,n);
this.property = n.property || "payload";
var node = this;
this.on("input", function(msg) {
var value = RED.util.getMessageProperty(msg,node.property);
if (value !== undefined) {
if (typeof(value) !== "string") {
// Take json object and make into a toon string
try {
value = toonlib.encode(value);
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
}
catch(err) {
node.error("Failed to encode: "+err.message,msg);
}
}
else if (typeof value === "string") {
// Take toon string and make into json object
try {
value = toonlib.decode(value)
RED.util.setMessageProperty(msg,node.property,value)
node.send(msg)
}
catch(err) {
node.error("Invalid TOON string: "+err.message,msg);
}
}
else {
node.warn("Cannot handle this type of input");
}
}
else {
node.warn("No property found to process");
}
});
}
RED.nodes.registerType("toon",ToonNode);
}