From 054ea7931a2e71766463a5d70e7c31e903d806c6 Mon Sep 17 00:00:00 2001 From: Alexander Lichter Date: Sun, 23 Sep 2018 23:45:44 +0100 Subject: [PATCH] refactor: client store code style (#3683) --- lib/app/store.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/app/store.js b/lib/app/store.js index c8ec3c7392..08106ce50a 100644 --- a/lib/app/store.js +++ b/lib/app/store.js @@ -3,7 +3,6 @@ import Vuex from 'vuex' Vue.use(Vuex) -// Recursive find files in {srcDir}/{dir.store} const files = require.context('@/<%= dir.store %>', true, /^\.\/(?!<%= ignorePrefix %>)[^.]+\.(<%= extensions %>)$/) const filenames = files.keys() @@ -11,12 +10,8 @@ const filenames = files.keys() let storeData = {} // Check if {dir.store}/index.js exists -let indexFilename -filenames.forEach((filename) => { - if (filename.indexOf('./index.') !== -1) { - indexFilename = filename - } -}) +const indexFilename = filenames.find(name => name.includes('./index.')) + if (indexFilename) { storeData = getModule(indexFilename) } @@ -35,14 +30,13 @@ if (typeof storeData !== 'function') { const namePath = name.split(/\//) name = namePath[namePath.length - 1] - if (name === 'state' || name === 'getters' || name === 'actions' || name === 'mutations') { + if (['state', 'getters', 'actions', 'mutations'].includes(name)) { const module = getModuleNamespace(storeData, namePath, true) appendModule(module, filename, name) continue } - // if file is foo/index.js - // it should save as foo + // If file is foo/index.js, it should be saved as foo const isIndex = (name === 'index') if (isIndex) { namePath.pop() @@ -50,6 +44,7 @@ if (typeof storeData !== 'function') { const module = getModuleNamespace(storeData, namePath) const fileModule = getModule(filename) + name = namePath.pop() module[name] = module[name] || {} @@ -100,7 +95,9 @@ function getModule (filename) { function getModuleNamespace (storeData, namePath, forAppend = false) { if (namePath.length === 1) { - if (forAppend) { return storeData } + if (forAppend) { + return storeData + } return storeData.modules } const namespace = namePath.shift()