Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize whitespace from config objects. #43

Merged
merged 3 commits into from
Jan 31, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,29 @@ const getConfigEnv = async options =>
options
)

const sanitizeConfig = config => {
// recurse configuration objects and arrays to remove whitespace
const sanitize = (chunk) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throughout, I opted to use lodash convenience methods since you had already included the package. If you'd prefer I use native methods (e.g. Array.isArray, native .trim()) let me know and I can swap them out!

return _.reduce(chunk, (result, v, k) => {
if (_.isObject(v) || _.isArray(v)) {
blimmer marked this conversation as resolved.
Show resolved Hide resolved
result[k] = sanitize(v);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where we recursively call this function to sanitize nested objects and arrays.

} else if (_.isString()) {
result[k] = _.trim(v);
} else {
result[k] = 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'
Expand Down