From 901b3e35538b1c8162840dcc1fb96363bcb46c71 Mon Sep 17 00:00:00 2001 From: Ben Limmer Date: Wed, 29 Jan 2020 10:40:06 -0700 Subject: [PATCH 1/2] Sanitize whitespace from config objects. This commit adds logic to recursively sanitize the configuration object before it's written out to disk. It trims leading and trailing whitespace from string values in arrays or objects. Fixes #42 --- src/lib/common.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/lib/common.js b/src/lib/common.js index 963c2c22..cff2047c 100644 --- a/src/lib/common.js +++ b/src/lib/common.js @@ -101,10 +101,27 @@ const getConfigEnv = async options => options ) +const sanitizeConfig = config => { + // recurse configuration objects and arrays to remove whitespace + const sanitize = (chunk) => { + return _.reduce(chunk, (result, v, k) => { + if (_.isObject(v) || _.isArray(v)) { + result[k] = sanitize(v); + } else { + result[k] = _.trim(v); + } + + return result; + }, _.isArray(chunk) ? [] : {}) + } + + return sanitize(config); +} + const writeConfig = async newConfig => wrapPromise( new Promise(async (resolve, reject) => { - fs.writeFileSync(CONFIG_FILE, JSON.stringify(newConfig, null, 2)) + fs.writeFileSync(CONFIG_FILE, JSON.stringify(sanitizeConfig(newConfig), null, 2)) resolve(getConfigEnv()) }), 'Writing config' From f4f665ab89dc8094436c201441f83a814506ae9b Mon Sep 17 00:00:00 2001 From: Ben Limmer Date: Wed, 29 Jan 2020 15:35:17 -0700 Subject: [PATCH 2/2] Add branch for non-string types. --- src/lib/common.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib/common.js b/src/lib/common.js index cff2047c..f459dfe4 100644 --- a/src/lib/common.js +++ b/src/lib/common.js @@ -107,8 +107,10 @@ const sanitizeConfig = config => { return _.reduce(chunk, (result, v, k) => { if (_.isObject(v) || _.isArray(v)) { result[k] = sanitize(v); - } else { + } else if (_.isString()) { result[k] = _.trim(v); + } else { + result[k] = v; } return result;