-
Notifications
You must be signed in to change notification settings - Fork 0
/
localData.js
61 lines (50 loc) · 1.29 KB
/
localData.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
61
function getParamFromUrl(
key,
defaultVal,
search = location.search,
ignoreCase = true
) {
const regExp = ignoreCase
? new RegExp(`[?|&]${key}=([^&]+)`, 'i')
: new RegExp(`[?|&]${key}=([^&]+)`);
const match = regExp.exec(search);
return match ? match[1] : defaultVal;
}
function getParam(key, search = location.search) {
const value = getParamFromUrl(key, '', search);
return typeof value !== 'undefined'
? decodeURIComponent(value)
: '';
}
function LocalData() {
this.storage = window.localStorage;
this.init();
}
LocalData.prototype.init = function() {
const puzzle = getParam('puzzle');
this.set('fling_puzzle', puzzle);
}
LocalData.prototype.get = function(key) {
var item = parseInt(this.storage.getItem(key));
if (!item) {
item = 0;
this.set(key, item);
}
return item;
};
LocalData.prototype.set = function(key, item) {
this.storage.setItem(key, item);
};
LocalData.prototype.getHighScores = function() {
var highScores = this.storage.getItem('fling_highScores');
if (!highScores) {
highScores = [];
for (var i = 0; i < 7; i++) {
highScores.push({name: 'someone', score: 0});
}
this.set('fling_highScores', JSON.stringify(highScores));
} else {
highScores = JSON.parse(highScores);
}
return highScores;
};