From 445f00d714e6da48cb919b6731dd58af9df4609e Mon Sep 17 00:00:00 2001 From: Joe Turgeon Date: Mon, 28 Dec 2020 12:30:21 -0600 Subject: [PATCH 1/2] Updating convertString handler to escape double quotation marks in values (configurable, by default to single quotes). --- handlers/convertString.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/handlers/convertString.js b/handlers/convertString.js index 397d1ea..a24bcff 100644 --- a/handlers/convertString.js +++ b/handlers/convertString.js @@ -7,9 +7,12 @@ exports.process = function(config) { } config.data = _.map(config.data, function(datum) { + var escapeChar = config.string && config.string.escapeChar ? + config.string.escapeChar : '\''; + var parts = _.map(datum, function(value, key) { key = String(key).replace('-', '_').replace(/\W/g, ''); - value = String(value).replace(/\n/g, ' '); + value = String(value).replace(/\n/g, ' ').replace(/"/g, escapeChar); return key + '="' + value + '"'; }); From 4b9ced983d5869d320adabe4cca582f53a65ad8b Mon Sep 17 00:00:00 2001 From: Joe Turgeon Date: Mon, 28 Dec 2020 14:02:17 -0600 Subject: [PATCH 2/2] Adding unit tests for convertString handler update. --- test/convertString.spec.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/convertString.spec.js b/test/convertString.spec.js index aae71c9..7d8074c 100644 --- a/test/convertString.spec.js +++ b/test/convertString.spec.js @@ -58,5 +58,42 @@ describe('handler/convertString.js', function() { done(); }); }); + + it('should escape double quote characters', + function(done) { + var config = { + data: [{test: 'this is "a" test.'}], + setting: true + }; + dataString = "test=\"this is 'a' test.\"\n"; + handler.process(config) + .then(function(result) { + assert.ok(result.hasOwnProperty('setting'), + 'process returns config object'); + assert.equal(result.data, dataString, + 'converted to string with prefix and suffix successfully'); + done(); + }); + }); + + it('should support a configurable escape character', + function(done) { + var config = { + data: [{test: 'this is "a" test.'}], + setting: true, + string: { + escapeChar: 'x' + } + }; + dataString = "test=\"this is xax test.\"\n"; + handler.process(config) + .then(function(result) { + assert.ok(result.hasOwnProperty('setting'), + 'process returns config object'); + assert.equal(result.data, dataString, + 'converted to string with prefix and suffix successfully'); + done(); + }); + }); }); });