diff --git a/parsers/toon/LICENSE b/parsers/toon/LICENSE new file mode 100644 index 00000000..e025d780 --- /dev/null +++ b/parsers/toon/LICENSE @@ -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. diff --git a/parsers/toon/README.md b/parsers/toon/README.md new file mode 100644 index 00000000..c0cf4315 --- /dev/null +++ b/parsers/toon/README.md @@ -0,0 +1,21 @@ +node-red-node-toon +==================== + +A Node-RED 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. diff --git a/parsers/toon/icons/parser-toon.png b/parsers/toon/icons/parser-toon.png new file mode 100644 index 00000000..d32f668b Binary files /dev/null and b/parsers/toon/icons/parser-toon.png differ diff --git a/parsers/toon/package.json b/parsers/toon/package.json new file mode 100644 index 00000000..8b0558a5 --- /dev/null +++ b/parsers/toon/package.json @@ -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" + } +} diff --git a/parsers/toon/toon.html b/parsers/toon/toon.html new file mode 100644 index 00000000..d5b81459 --- /dev/null +++ b/parsers/toon/toon.html @@ -0,0 +1,43 @@ + + + + + + diff --git a/parsers/toon/toon.js b/parsers/toon/toon.js new file mode 100644 index 00000000..24208f12 --- /dev/null +++ b/parsers/toon/toon.js @@ -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); +}