forked from ospreys-2014/PinPoint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
JSONparser.js
50 lines (46 loc) · 1.32 KB
/
JSONparser.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
// pushs a note object to the notes array
// getNotes returns and saves the records.
function addNote(url, note) {
var noteObjects = getNotes(url);
noteObjects.push(note);
saveNotes(url, noteObjects);
}
// returns an array if no records exist, otherwise
// it returns an array of note objects.
function getNotes(url){
if (localStorage[url] === undefined){
return [];
} else {
var retrievedObject = localStorage.getItem(url);
return JSON.parse(retrievedObject);
}
}
// removes a note by matching the seconds attr of a note
// object and excludes it from the notes array.
function removeNote(url, seconds){
var notes = getNotes(url);
var result = notes.map(function(note){
if (note.seconds != seconds){
return note;
}
})
// result array includes an undefined value after one
// of the objects are removed. Clean function removes
// all undefined values before it is saved to record.
result.clean(undefined);
saveNotes(url, result);
}
// saves notes array as string in localStorage.
function saveNotes(url, notes) {
localStorage[url] = JSON.stringify(notes);
}
// **Extension on the native Array Class**
Array.prototype.clean = function(deleteValue) {
for (var i = 0; i < this.length; i++) {
if (this[i] == deleteValue) {
this.splice(i, 1);
i--;
}
}
return this;
};