From 877c269ee42f82d5021b88a6731ea96411bc5011 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Mon, 3 Dec 2018 14:38:03 +0100 Subject: [PATCH] Feat: Use moment js to manage dates --- src/libs/data_processing.js | 5 +++++ src/store/modules/datas.js | 27 +++++++++++---------------- src/views/home.vue | 7 ++----- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/libs/data_processing.js b/src/libs/data_processing.js index bd8c6d0..a1f586b 100644 --- a/src/libs/data_processing.js +++ b/src/libs/data_processing.js @@ -1,3 +1,4 @@ +import moment from 'moment' export function appendTag (row, keywords, field = 'Libellé') { // Append row.tag // if row.libellé contains one of words and not invert it gets tagged @@ -25,6 +26,10 @@ function strContains (string, words, invert) { } } +export function formatDate (row, field = 'Date') { + row[field] = moment(row[field], 'DD/MM/YYYY', true) +} + export function total (row, field = 'Montant') { var sum = row.map(x => parseFloat(x[field])) .reduce((sum, x) => sum + x, 0) diff --git a/src/store/modules/datas.js b/src/store/modules/datas.js index d1406bb..77a3e01 100644 --- a/src/store/modules/datas.js +++ b/src/store/modules/datas.js @@ -1,11 +1,8 @@ import { readdir, readFile } from 'fs' import path from 'path' import Papa from 'papaparse' -import { appendTag } from '../../libs/data_processing' - -var today = new Date() -var monthAgo = new Date() -monthAgo.setMonth(monthAgo.getMonth() - 1) +import moment from 'moment' +import { appendTag, formatDate } from '../../libs/data_processing' export default { namespaced: true, @@ -17,8 +14,8 @@ export default { fields: [] } }, - start: monthAgo.toISOString().split('T')[0], - end: today.toISOString().split('T')[0] + start: moment().subtract(1, 'months'), + end: moment() }, getters: { csvs: (state) => { @@ -34,16 +31,14 @@ export default { return getters.rows.filter(x => x.Montant < 0) }, start: (state) => { - return state.start + return state.start.format(moment.HTML5_FMT.DATE) }, end: (state) => { - return state.end + return state.end.format(moment.HTML5_FMT.DATE) }, date_filter_rows: (state, getters) => { - var start = new Date(state.start) - var end = new Date(state.end) return getters.spending_rows.filter(x => { - return (new Date(x.Date) >= start) & (new Date(x.Date) < end) + return (x.Date >= state.start) & (x.Date < state.end) }) }, tag_filter_rows: (state, getters) => (tags, invert) => { @@ -52,7 +47,7 @@ export default { if (invert) { return tags.some(t => { return row.tags.map(t => t.name.toLowerCase()) - .indexOf(t.toLowerCase()) < 0 + .ÿEindexOf(t.toLowerCase()) < 0 }) } else { return tags.every(t => { @@ -126,7 +121,6 @@ export default { } }, load_csv (context, csv) { - //readFile(path.join(context.rootGetters['config/data_dir'], csv), 'utf8', (err, content) => { readFile(path.join(context.rootGetters['config/data_dir'], csv), 'Latin1', (err, content) => { if (err) { console.log(err) @@ -144,15 +138,16 @@ export default { parsed.data = parsed.data.filter(x => x.Libellé !== undefined) parsed.data.forEach(row => { appendTag(row, tags, 'Libellé') + formatDate(row, 'Date') }) context.commit('SET_DATA', { data: parsed }) }, set_start (context, start) { - context.commit('SET_START', { start }) + context.commit('SET_START', { start: moment(start)}) }, set_end (context, end) { - context.commit('SET_END', { end }) + context.commit('SET_END', { end: moment(end)}) } } } diff --git a/src/views/home.vue b/src/views/home.vue index f041000..199f7f8 100644 --- a/src/views/home.vue +++ b/src/views/home.vue @@ -5,7 +5,7 @@ - + @@ -66,7 +66,7 @@ export default { { key: 'Date', sortable: true, - formatter: this.table_date_format + formatter: d => d.format('DD/MM/YYYY') }, { key: 'Montant', @@ -113,9 +113,6 @@ export default { ]), update_start (e) { }, - table_date_format (date) { - return date - }, set_tags_filter (tagnames) { this.tags_filter = tagnames }