-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookies.ts
70 lines (65 loc) · 2.38 KB
/
cookies.ts
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
62
63
64
65
66
67
68
69
70
module App {
export class Cookies {
private static _expirationDays: number = 365;
/**
* Reads the cookie and parses the completed course IDs from it.
* @function
*/
public static parseCompletedCookies(): number[] {
var completed = Cookies.get("completed").split(',');
var list: number[] = [];
if (completed && completed != [] && completed != ['']) {
completed.forEach(function (item) {
var num = parseInt(item);
if (!isNaN(num)) list.push(num);
});
}
return list;
}
/**
* Saves the passed completed course list IDs into the cookie (and erases the old value)
* @function
*/
public static updateCompletedCookies(list: number[]) {
Cookies.delete("completed");
if(list && list != []) Cookies.set("completed", list.toString());
}
/**
* Returns the value of the requested cookie
* @function
* @param {string} name the name of the cookie you are looking for
*/
private static get(name: string) {
let ca: Array<string> = document.cookie.split(/;\s/);
let cookieName = name + "=";
let c: string;
for (let i: number = 0; i < ca.length; i += 1) {
c = ca[i].replace(/^\s\+/g, "");
if (c.indexOf(cookieName) == 0) {
return c.substring(cookieName.length, c.length);
}
}
return "";
}
/**
* Deletes the requested cookie
* @function
* @param {string} name the name of the cookie to delete
*/
private static delete(name) {
Cookies.set(name, "");
}
/**
* Sets the value of the requested cookie
* @function
* @param {string} name the name of the cookie you are setting
* @param {string} value the value of the cookie
*/
private static set(name: string, value: string) {
let d: Date = new Date();
d.setTime(d.getTime() + Cookies._expirationDays * 24 * 60 * 60 * 1000);
let expires: string = "expires=" + d.toUTCString();
document.cookie = name + "=" + value + "; " + expires;
}
}
}