-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact-api.js
35 lines (30 loc) · 899 Bytes
/
contact-api.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
/* global localStorage */
var contactAPI = {}
contactAPI.load = function () {
return JSON.parse(localStorage.getItem('contacts') || '[]')
}
contactAPI.add = function (contact) {
var contacts = contactAPI.load()
contact.id = Math.random().toString(36).substr(2, 7)
contacts.push(contact)
localStorage.setItem('contacts', JSON.stringify(contacts))
return contact
}
contactAPI.update = function (id, properties) {
var contacts = contactAPI.load()
contacts = contacts.map(function (contact) {
if (id === contact.id) {
properties.id = id
return properties
}
return contact
})
localStorage.setItem('contacts', JSON.stringify(contacts))
}
contactAPI.delete = function (id) {
var contacts = contactAPI.load()
contacts = contacts.filter(function (contact) {
return id !== contact.id
})
localStorage.setItem('contacts', JSON.stringify(contacts))
}