-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
90 lines (68 loc) · 2.12 KB
/
index.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
interface OptionalProp {
expires?: Date;
maxAge?: number;
}
class LazyCookie {
/**
* Returns total number of key/value pair that exist.
* @readonly
* @returns {number}
*/
public readonly length: number;
public constructor() {
this.length = document.cookie.split("; ").length;
}
private cookieObject() {
const map = new Map();
for (let i = 0; i < this.length; i++) {
const cookieIndex = document.cookie.split("; ")[i].split("=");
map.set(cookieIndex[0], cookieIndex[1]);
}
const cookieObj = Object.fromEntries(map);
return cookieObj;
}
/**
* Returns the current value associated with the given key, or undefined if the given key does not exist.
* @method
* @param {string} key - key
* @returns {string}
*/
public getItem(key: string): string {
return this.cookieObject()[key];
}
/**
* Sets the value of the pair identified by key to value,
* creating a new key/value pair if none existed for key previously.
* It Overrides already existing key/value pair.
* @method
* @param {string} key
* @param {string} value
* @param {Date} expires - when cookie should expire in Date format.
* @param {number} maxAge - when cookie should expire in millisecond.
* @returns {void}
*/
public setItem(key: string, value: string, { expires, maxAge }: OptionalProp = {}): void {
document.cookie = `${key}=${value}; max-Age=${maxAge}; expires=${expires}`;
}
/**
* @method
* @param {string} name - key
* @returns {void}
*/
public deleteItem(name: string): void {
document.cookie = `${name}=; max-Age=-1`;
}
/**
* Removes all key/value pairs, if there are any.
* @method
* @returns {void}
*/
public clear(): void {
const arrayOfCookiesKeys = Object.keys(this.cookieObject());
for (let a of arrayOfCookiesKeys) {
document.cookie = `${a}=; max-Age=-1`;
}
}
}
const lazycookie = new LazyCookie();
export default lazycookie;