Skip to content

Commit

Permalink
Merge pull request #1249 from NicoPennec/fix/csv
Browse files Browse the repository at this point in the history
Improve CSV export
  • Loading branch information
EvanBldy authored Nov 30, 2023
2 parents 8be7fc5 + e3a9001 commit e866a13
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions src/lib/csv.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Papa from 'papaparse'

import { getPercentage } from '@/lib/stats'
import stringHelpers from '@/lib/string'
import {
getDayRange,
getMonthRange,
getWeekRange,
hoursToDays
} from '@/lib/time'
import { getPercentage } from '@/lib/stats'

const csv = {
generateTimesheet(
Expand Down Expand Up @@ -108,17 +110,14 @@ const csv = {
return entries
},

turnEntriesToCsvString(entries) {
const lineArray = []
entries.forEach(infoArray => {
const sanitizedCells = infoArray.map(cell => {
const cellString = `${cell || ''}`
return `"${cellString.replace(/"/g, '""')}"`
})
const line = sanitizedCells.join(';')
if (line.length > 2) lineArray.push(line)
turnEntriesToCsvString(entries, config = {}) {
return Papa.unparse(entries, {
delimiter: ';',
newline: '\n',
quotes: true,
skipEmptyLines: true,
...config
})
return lineArray.join('\n')
},

buildCsvFile(name, entries) {
Expand Down Expand Up @@ -402,30 +401,19 @@ const csv = {
return entries
},

processCSV: (data, config) => {
processCSV: (data, config = {}) => {
return new Promise((resolve, reject) => {
Papa.parse(data, {
config: config,
encoding: 'UTF-8',
error: reject,
transform: value => value.trim(),
complete: results => {
const parsedData = csv.cleanUpCsv(results.data)
resolve(parsedData)
}
results.data[0].forEach(stringHelpers.capitalize)
resolve(results.data)
},
...config
})
})
},

cleanUpCsv: data => {
data.forEach(item => {
item.forEach((item, index, data) => {
data[index] = item.trim()
})
})
data[0].forEach((item, index, data) => {
data[index] = (item[0] || '').toUpperCase() + item.slice(1)
})
return data
}
}

Expand Down

0 comments on commit e866a13

Please sign in to comment.