-
Notifications
You must be signed in to change notification settings - Fork 0
/
todomvc-svc.js
60 lines (52 loc) · 1.26 KB
/
todomvc-svc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const { randomGuid } = require('./utils')
const getStore = () => {
let collection = []
const getIdx = (id) => collection.findIndex(t => t.id === id)
const init = (initialData) => {
collection = initialData
console.info(`SVC > Todos initilized, count: ${initialData.length}`)
}
const getAllItems = () => {
console.info(`SVC > All todos fetched, count: ${collection.length}`)
return collection
}
const addItem = (title) => {
const newItem = {
id: randomGuid(),
title,
completed: false
}
collection.unshift(newItem)
console.info(`SVC > Todo ID:${newItem.id} added`)
return newItem
}
const deleteItem = (id) => {
const idx = getIdx(id)
debugger
if (idx == -1) {
throw new Error('Invalid ID')
}
const item = collection.splice(idx, 1)
console.info(`SVC > Todo ID:${id} removed`)
return item[0]
}
const updateItem = (id, changes) => {
const idx = getIdx(id)
if (idx == -1) {
throw new Error('Invalid ID')
}
collection[idx] = { ...collection[idx], ...changes }
console.info(`SVC > Todo ID:${id} updated`)
return collection[idx]
}
return {
init,
getAllItems,
addItem,
deleteItem,
updateItem
}
}
module.exports = {
getStore
}