From 697435de303c68cf2fa10fc6e69c9cdfa11ccd66 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Mon, 3 Dec 2018 09:49:36 +0100 Subject: [PATCH] Feat: Tag rows before storing them in vuex --- src/libs/data_processing.js | 27 +++++++++++++++++++++++++++ src/store/modules/datas.js | 13 +++++++++++-- src/views/home.vue | 15 ++++++++++++++- 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/libs/data_processing.js diff --git a/src/libs/data_processing.js b/src/libs/data_processing.js new file mode 100644 index 0000000..2946b70 --- /dev/null +++ b/src/libs/data_processing.js @@ -0,0 +1,27 @@ +export function appendTag (row, keywords, field='Libellé') { + // Append row.tag + // if row.libellé contains one of words and not invert it gets tagged + // if row.libellé contains no words and invert it gets tagged + // according to keywords [{name: string, words: [], invert: bool}] + // row['tags'] = + row.tags = keywords.filter(k => { + return strContains(row[field], k.words, k.invert) + }) +} + +function strContains (string, words, invert) { + // Does a string contain one of words or the opposite + if (!words) { + return true + } + if (invert) { + return words.every(v => { + return string.indexOf(v) < 0 + }) + } else { + return words.some(v => { + return string.indexOf(v) >= 0 + }) + } +} + diff --git a/src/store/modules/datas.js b/src/store/modules/datas.js index 9d18ad9..c5ddf2a 100644 --- a/src/store/modules/datas.js +++ b/src/store/modules/datas.js @@ -1,6 +1,7 @@ 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() @@ -66,7 +67,6 @@ export default { state.csv_files = csvs }, SET_DATA: (state, { data }) => { - data.data = data.data.filter(x => x.Libellé !== undefined) state.rows = data }, APPEND_DATA: (state, { content }) => { @@ -108,10 +108,19 @@ export default { header: true } var parsed = Papa.parse(content, parseConfig) - context.commit('SET_DATA', { data: parsed }) + context.dispatch('clean_store_data', parsed) } }) }, + clean_store_data (context, parsed) { + var postes = Object.values(context.rootGetters['config/postes']) + parsed.data = parsed.data.filter(x => x.Libellé !== undefined) + parsed.data.forEach(row => { + appendTag(row, postes, 'Libellé') + }) + + context.commit('SET_DATA', { data: parsed }) + }, set_start (context, start) { context.commit('SET_START', { start }) }, diff --git a/src/views/home.vue b/src/views/home.vue index 3f751eb..2e70986 100644 --- a/src/views/home.vue +++ b/src/views/home.vue @@ -23,7 +23,16 @@ - + + + + @@ -53,6 +62,10 @@ export default { { key: 'Libellé', sortable: true + }, + { + key: 'tags', + sortable: true } ], poste: {}