From 9e8005960e261ede425584011bf93780af9ebf5c Mon Sep 17 00:00:00 2001 From: Julia Friesel <julia.friesel@gmail.com> Date: Mon, 6 Aug 2018 09:27:28 +0200 Subject: [PATCH] Handle special characters in strings --- components/settings.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/components/settings.js b/components/settings.js index bf420f72..cfce6dac 100644 --- a/components/settings.js +++ b/components/settings.js @@ -64,7 +64,8 @@ function transformToCsv(cycleDays) { const rows = cycleDays .map(day => { return columnNames.map(column => { - return objectPath.get(day, column, '') + const val = objectPath.get(day, column, '') + return typeof val === 'string' ? csvify(val) : val }) }) .map(row => row.join(',')) @@ -72,3 +73,20 @@ function transformToCsv(cycleDays) { rows.unshift(columnNames.join(',')) return rows.join('\n') } + +function csvify (val) { + // escape double quotes + val = val.replace(/"/g, '""') + + val = val.toLowerCase() + const hasSpecialChars = ( + val.includes('\n') || + val.includes('\t') || + val.includes(',') || + val.includes(';') || + val.includes('.') || + val.includes('\'') + ) + + return hasSpecialChars ? `"${val}"` : val +} \ No newline at end of file -- GitLab