Sanitize values for CSV. #7697
1 changed files with 10 additions and 7 deletions
|
@ -1,6 +1,6 @@
|
||||||
// JSON parser
|
// JSON parser
|
||||||
const parseJson = (data, filters = []) => {
|
const parseJson = (data, filters = []) => {
|
||||||
const list = data.map(item => {
|
const list = data.map((item) => {
|
||||||
const temp = {};
|
const temp = {};
|
||||||
// Apply filters
|
// Apply filters
|
||||||
Object.entries(item).forEach(([key, value]) => {
|
Object.entries(item).forEach(([key, value]) => {
|
||||||
|
@ -17,7 +17,7 @@ const parseJson = (data, filters = []) => {
|
||||||
// https://gist.github.com/btzr-io/55c3450ea3d709fc57540e762899fb85
|
// https://gist.github.com/btzr-io/55c3450ea3d709fc57540e762899fb85
|
||||||
const parseCsv = (data, filters = []) => {
|
const parseCsv = (data, filters = []) => {
|
||||||
// Get items for header
|
// Get items for header
|
||||||
const getHeaders = item => {
|
const getHeaders = (item) => {
|
||||||
const list = [];
|
const list = [];
|
||||||
// Apply filters
|
// Apply filters
|
||||||
Object.entries(item).forEach(([key]) => {
|
Object.entries(item).forEach(([key]) => {
|
||||||
|
@ -28,13 +28,16 @@ const parseCsv = (data, filters = []) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get rows content
|
// Get rows content
|
||||||
const getData = list =>
|
const getData = (list) =>
|
||||||
list
|
list
|
||||||
.map(item => {
|
.map((item) => {
|
||||||
const row = [];
|
const row = [];
|
||||||
// Apply filters
|
// Apply filters
|
||||||
Object.entries(item).forEach(([key, value]) => {
|
Object.entries(item).forEach(([key, value]) => {
|
||||||
if (!filters.includes(key)) row.push(value);
|
if (!filters.includes(key)) {
|
||||||
|
const sanitizedValue = '"' + value + '"';
|
||||||
|
row.push(sanitizedValue);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
// return rows
|
// return rows
|
||||||
return row.join(',');
|
return row.join(',');
|
||||||
|
@ -50,8 +53,8 @@ const parseData = (data, format, filters = []) => {
|
||||||
const valid = data && data[0] && format;
|
const valid = data && data[0] && format;
|
||||||
// Pick a format
|
// Pick a format
|
||||||
const formats = {
|
const formats = {
|
||||||
csv: list => parseCsv(list, filters),
|
csv: (list) => parseCsv(list, filters),
|
||||||
json: list => parseJson(list, filters),
|
json: (list) => parseJson(list, filters),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return parsed data: JSON || CSV
|
// Return parsed data: JSON || CSV
|
||||||
|
|
Loading…
Reference in a new issue