1
0
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:
Mark Hindess 2014-08-28 14:25:41 +01:00
parent e09ac859d3
commit a9e72858df
6 changed files with 77 additions and 24 deletions

View File

@ -160,14 +160,7 @@ module.exports = function(RED) {
delete msg._session;
payload = JSON.stringify(msg);
} else {
payload = 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;
}
payload = RED.utils.ensureString(msg.payload);
}
if (msg._session && msg._session.type == "websocket") {
node.serverConfig.send(msg._session.id,payload);

View File

@ -73,14 +73,7 @@ module.exports = function(RED) {
if (msg != null) {
if (smtpTransport) {
node.status({fill:"blue",shape:"dot",text:"sending"});
var payload = 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;
}
var payload = RED.utils.ensureString(msg.payload);
smtpTransport.sendMail({
from: node.userid, // sender address

View File

@ -83,14 +83,7 @@ module.exports = function(RED) {
var k = this.key || msg.topic;
if (k) {
if (this.structtype == "string") {
if (Buffer.isBuffer(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);
this.client.set(k,RED.utils.ensureString(msg.payload));
} else if (this.structtype == "hash") {
var r = hashFieldRE.exec(msg.payload);
if (r) {

View File

@ -19,6 +19,7 @@ var nodes = require("./nodes");
var library = require("./library");
var comms = require("./comms");
var log = require("./log");
var utils = require("./utils");
var fs = require("fs");
var settings = null;
var credentials = require("./nodes/credentials");
@ -48,6 +49,7 @@ var RED = {
events: events,
log: log,
comms: comms,
utils: utils,
version: function () {
var p = require(path.join(process.env.NODE_RED_HOME,"package.json"));
if (fs.existsSync(path.join(process.env.NODE_RED_HOME,".git"))) {

31
red/utils.js Normal file
View 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
View 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');
});
});
});