Passed
Pull Request — master (#451)
by korelstar
07:41
created

src/store.js   A

Complexity

Total Complexity 39
Complexity/F 1.7

Size

Lines of Code 160
Function Count 23

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 39
eloc 100
mnd 16
bc 16
fnc 23
dl 0
loc 160
bpm 0.6956
cpm 1.6956
noi 1
c 0
b 0
f 0
rs 9.28

1 Function

Rating   Name   Duplication   Size   Complexity  
D store.js ➔ nthIndexOf 0 10 12
1
import Vue from 'vue'
2
import Vuex from 'vuex'
3
4
Vue.use(Vuex)
5
6
export default new Vuex.Store({
7
	state: {
8
		settings: {},
9
		notes: [],
10
		notesIds: {},
11
		unsaved: {},
12
		isSaving: false,
13
		isManualSave: false,
14
		documentTitle: null,
15
		sidebarOpen: false,
16
	},
17
18
	getters: {
19
		numNotes: (state) => () => {
20
			return state.notes.length
21
		},
22
23
		noteExists: (state) => (id) => {
24
			return state.notesIds[id] !== undefined
25
		},
26
27
		getNote: (state) => (id) => {
28
			if (state.notesIds[id] === undefined) {
29
				return null
30
			}
31
			return state.notesIds[id]
32
		},
33
34
		getCategories: (state) => (maxLevel, details) => {
35
			function nthIndexOf(str, pattern, n) {
0 ignored issues
show
Bug introduced by
The function nthIndexOf is declared conditionally. This is not supported by all runtimes. Consider moving it to root scope or using var nthIndexOf = function() { /* ... */ }; instead.
Loading history...
36
				let i = -1
37
				while (n-- && i++ < str.length) {
38
					i = str.indexOf(pattern, i)
39
					if (i < 0) {
40
						break
41
					}
42
				}
43
				return i
44
			}
45
46
			// get categories from notes
47
			const categories = {}
48
			for (const note of state.notes) {
49
				let cat = note.category
50
				if (maxLevel > 0) {
51
					const index = nthIndexOf(cat, '/', maxLevel)
52
					if (index > 0) {
53
						cat = cat.substring(0, index)
54
					}
55
				}
56
				if (categories[cat] === undefined) {
57
					categories[cat] = 1
58
				} else {
59
					categories[cat] += 1
60
				}
61
			}
62
			// get structured result from categories
63
			const result = []
64
			for (const category in categories) {
65
				if (details) {
66
					result.push({
67
						name: category,
68
						count: categories[category],
69
					})
70
				} else if (category) {
71
					result.push(category)
72
				}
73
			}
74
			if (details) {
75
				result.sort((a, b) => a.name.localeCompare(b.name))
76
			} else {
77
				result.sort()
78
			}
79
			return result
80
		},
81
	},
82
83
	mutations: {
84
		add(state, updated) {
85
			const note = state.notesIds[updated.id]
86
			if (note) {
87
				// don't update meta-data over full data
88
				if (updated.content !== null || note.content === null) {
89
					note.title = updated.title
90
					note.modified = updated.modified
91
					note.content = updated.content
92
					note.favorite = updated.favorite
93
					note.category = updated.category
94
					Vue.set(note, 'unsaved', updated.unsaved)
95
					Vue.set(note, 'error', updated.error)
96
					Vue.set(note, 'errorMessage', updated.errorMessage)
97
				}
98
			} else {
99
				state.notes.push(updated)
100
				Vue.set(state.notesIds, updated.id, updated)
101
			}
102
		},
103
104
		setNoteAttribute(state, params) {
105
			const note = state.notesIds[params.noteId]
106
			if (note) {
107
				Vue.set(note, params.attribute, params.value)
108
			}
109
		},
110
111
		remove(state, id) {
112
			const index = state.notes.findIndex(note => note.id === id)
113
			if (index !== -1) {
114
				state.notes.splice(index, 1)
115
				delete state.notesIds[id]
116
			}
117
		},
118
119
		removeAll(state) {
120
			state.notes = []
121
			state.notesIds = {}
122
		},
123
124
		addUnsaved(state, id) {
125
			Vue.set(state.unsaved, id, state.notesIds[id])
126
		},
127
128
		clearUnsaved(state) {
129
			state.unsaved = {}
130
		},
131
132
		setSettings(state, settings) {
133
			state.settings = settings
134
		},
135
136
		setSaving(state, isSaving) {
137
			state.isSaving = isSaving
138
		},
139
140
		setManualSave(state, isManualSave) {
141
			state.isManualSave = isManualSave
142
		},
143
144
		setDocumentTitle(state, title) {
145
			state.documentTitle = title
146
		},
147
148
		setSidebarOpen(state, open) {
149
			state.sidebarOpen = open
150
		},
151
	},
152
153
	actions: {
154
		addAll(context, notes) {
155
			for (const note of notes) {
156
				context.commit('add', note)
157
			}
158
		},
159
	},
160
})
161