mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Add ensure string helper for nodes.
This commit is contained in:
parent
e09ac859d3
commit
a9e72858df
@ -160,14 +160,7 @@ module.exports = function(RED) {
|
|||||||
delete msg._session;
|
delete msg._session;
|
||||||
payload = JSON.stringify(msg);
|
payload = JSON.stringify(msg);
|
||||||
} else {
|
} else {
|
||||||
payload = msg.payload;
|
payload = RED.utils.ensureString(msg.payload);
|
||||||
if (Buffer.isBuffer(payload)) {
|
|
||||||
payload = payload.toString();
|
|
||||||
} else if (typeof payload === "object") {
|
|
||||||
payload = JSON.stringify(payload);
|
|
||||||
} else if (typeof payload !== "string") {
|
|
||||||
payload = ""+payload;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (msg._session && msg._session.type == "websocket") {
|
if (msg._session && msg._session.type == "websocket") {
|
||||||
node.serverConfig.send(msg._session.id,payload);
|
node.serverConfig.send(msg._session.id,payload);
|
||||||
|
@ -73,14 +73,7 @@ module.exports = function(RED) {
|
|||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
if (smtpTransport) {
|
if (smtpTransport) {
|
||||||
node.status({fill:"blue",shape:"dot",text:"sending"});
|
node.status({fill:"blue",shape:"dot",text:"sending"});
|
||||||
var payload = msg.payload;
|
var payload = RED.utils.ensureString(msg.payload);
|
||||||
if (Buffer.isBuffer(payload)) {
|
|
||||||
payload = payload.toString();
|
|
||||||
} else if (typeof payload === "object") {
|
|
||||||
payload = JSON.stringify(payload);
|
|
||||||
} else if (typeof payload !== "string") {
|
|
||||||
payload = ""+payload;
|
|
||||||
}
|
|
||||||
|
|
||||||
smtpTransport.sendMail({
|
smtpTransport.sendMail({
|
||||||
from: node.userid, // sender address
|
from: node.userid, // sender address
|
||||||
|
@ -83,14 +83,7 @@ module.exports = function(RED) {
|
|||||||
var k = this.key || msg.topic;
|
var k = this.key || msg.topic;
|
||||||
if (k) {
|
if (k) {
|
||||||
if (this.structtype == "string") {
|
if (this.structtype == "string") {
|
||||||
if (Buffer.isBuffer(msg.payload)) {
|
this.client.set(k,RED.utils.ensureString(msg.payload));
|
||||||
msg.payload = msg.payload.toString();
|
|
||||||
} else if (typeof msg.payload === "object") {
|
|
||||||
msg.payload = JSON.stringify(msg.payload);
|
|
||||||
} else if (typeof msg.payload !== "string") {
|
|
||||||
msg.payload = ""+msg.payload;
|
|
||||||
}
|
|
||||||
this.client.set(k,msg.payload);
|
|
||||||
} else if (this.structtype == "hash") {
|
} else if (this.structtype == "hash") {
|
||||||
var r = hashFieldRE.exec(msg.payload);
|
var r = hashFieldRE.exec(msg.payload);
|
||||||
if (r) {
|
if (r) {
|
||||||
|
@ -19,6 +19,7 @@ var nodes = require("./nodes");
|
|||||||
var library = require("./library");
|
var library = require("./library");
|
||||||
var comms = require("./comms");
|
var comms = require("./comms");
|
||||||
var log = require("./log");
|
var log = require("./log");
|
||||||
|
var utils = require("./utils");
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var settings = null;
|
var settings = null;
|
||||||
var credentials = require("./nodes/credentials");
|
var credentials = require("./nodes/credentials");
|
||||||
@ -48,6 +49,7 @@ var RED = {
|
|||||||
events: events,
|
events: events,
|
||||||
log: log,
|
log: log,
|
||||||
comms: comms,
|
comms: comms,
|
||||||
|
utils: utils,
|
||||||
version: function () {
|
version: function () {
|
||||||
var p = require(path.join(process.env.NODE_RED_HOME,"package.json"));
|
var p = require(path.join(process.env.NODE_RED_HOME,"package.json"));
|
||||||
if (fs.existsSync(path.join(process.env.NODE_RED_HOME,".git"))) {
|
if (fs.existsSync(path.join(process.env.NODE_RED_HOME,".git"))) {
|
||||||
|
31
red/utils.js
Normal file
31
red/utils.js
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2014 IBM Corp.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
**/
|
||||||
|
|
||||||
|
function ensureString(o) {
|
||||||
|
if (Buffer.isBuffer(o)) {
|
||||||
|
return o.toString();
|
||||||
|
} else if (typeof o === "object") {
|
||||||
|
return JSON.stringify(o);
|
||||||
|
} else if (typeof o === "string") {
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
return ""+o;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
ensureString: ensureString,
|
||||||
|
};
|
||||||
|
|
41
test/red/utils_spec.js
Normal file
41
test/red/utils_spec.js
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2014 IBM Corp.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
**/
|
||||||
|
var should = require("should");
|
||||||
|
var utils = require("../../red/utils");
|
||||||
|
|
||||||
|
describe("red/utils", function() {
|
||||||
|
describe('ensureString', function() {
|
||||||
|
it('strings are preserved', function() {
|
||||||
|
utils.ensureString('string').should.equal('string');
|
||||||
|
});
|
||||||
|
it('Buffer is converted', function() {
|
||||||
|
var s = utils.ensureString(new Buffer('foo'));
|
||||||
|
s.should.equal('foo');
|
||||||
|
(typeof s).should.equal('string');
|
||||||
|
});
|
||||||
|
it('Object is converted to JSON', function() {
|
||||||
|
var s = utils.ensureString({foo: "bar"});
|
||||||
|
(typeof s).should.equal('string');
|
||||||
|
should.deepEqual(JSON.parse(s), {foo:"bar"});
|
||||||
|
});
|
||||||
|
it('stringifies other things', function() {
|
||||||
|
var s = utils.ensureString(123);
|
||||||
|
(typeof s).should.equal('string');
|
||||||
|
s.should.equal('123');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user