-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.js
More file actions
50 lines (41 loc) · 1.28 KB
/
helper.js
File metadata and controls
50 lines (41 loc) · 1.28 KB
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
window.helper = (function () {
function TypeException(message) {
this.message = message;
this.name = "TypeException";
}
function isNumber(val){
return typeof val === "number";
}
db = (function () {
function save(id, array) {
if (!isNumber(id)) {
throw new TypeException("Primeiro parâmetro precisa ser um número");
}
if (!Array.isArray(array)) {
throw new TypeException("Segundo parâmetro precisa ser um Array");
}
localStorage.setItem(id, JSON.stringify(array));
}
function get(id) {
if (!isNumber(id)) {
throw new TypeException("Primeiro parâmetro precisa ser um número");
}
return JSON.parse(localStorage[id]);
}
return {
"save": save,
"get": get
};
}());
return {
"db": db,
random: function(min, max) {
if (!isNumber(min) || !isNumber(max)) {
throw new TypeException("Os parametros precisam ser números.");
}
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
}
}());