-
Notifications
You must be signed in to change notification settings - Fork 1
/
cookie.js
146 lines (128 loc) · 3.77 KB
/
cookie.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
define([], function () {
// TODO: Put these utility functions somewhere else. dojo/string perhaps?
/**
* Escapes a string to be used within a regular expression.
* @returns {string} Escaped string.
*/
function escapeString(/**string*/ string) {
return string.replace(/[-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
}
/**
* Counts the number of instances of `needle` found inside `haystack`.
*
* @param haystack
* String to search.
*
* @param needle
* String to look for.
*
* @returns {number} Number of hits.
*/
function count(/**string*/ haystack, /**string*/ needle) {
var hits = 0,
lastIndex = haystack.indexOf(needle);
while (lastIndex > -1) {
++hits;
lastIndex = haystack.indexOf(needle, lastIndex + 1);
}
return hits;
}
/**
* Options that may be used to set a cookie.
*
* @name cookieOptions
*
* @property {(Date|string)} expires
* The date at which the cookie should expire. By default, the cookie will expire when the browser closes.
*
* @property {number} maxAge
* The number of seconds from now that the cookie should expire. By default, the cookie will expire when the
* browser closes.
*
* @property {string} path
* The path to use for the cookie.
*
* @property {string} domain
* The domain to use for the cookie.
*
* @property {boolean} secure
* Whether or not to only send the cookie over secure connections.
*/
/**
* Creates a well-formed cookie options string.
* @returns {string}
*/
function createCookieOptions(/**cookieOptions*/ options) {
var optionsString = '',
value;
for (var k in options) {
value = options[k];
if (k === 'maxAge') {
k = 'max-age';
}
else if (k === 'secure' && !value) {
continue;
}
optionsString += '; ' + encodeURIComponent(k);
if (k === 'secure') {
// secure is a boolean flag, so provide no value
}
else if (k === 'expires') {
// Expires will not work if its value is URI-encoded
optionsString += '=' + (value.toUTCString ? value.toUTCString() : value);
}
else {
optionsString += '=' + encodeURIComponent(value);
}
}
return optionsString;
}
if (!navigator.cookieEnabled) {
return null;
}
var longAgo = new Date(1970, 0, 1).toUTCString();
/**
* An interface for getting and setting cookies based on the DOM Storage API.
*/
return {
/**
* The number of cookies that are currently set.
*/
get length() {
return document.cookie.length ? count(document.cookie, '; ') + 1 : 0;
},
/**
* Gets the key of the cookie at the given index.
* Note that at least in Chrome 24, changing the value of an existing cookie puts that cookie at the end of the
* cookie string and so the sort order changes. Other browsers maintain key sort order.
* @returns {?string}
*/
key: function (/**number*/ index) {
var keyValuePair = document.cookie.split('; ', index + 1)[index];
return keyValuePair ? decodeURIComponent(/^([^=]+)/.exec(keyValuePair)[0]) : null;
},
/**
* Gets the value of a cookie.
* @returns {?string}
*/
getItem: function (/**string*/ key) {
var match = new RegExp('(?:^|; )' + escapeString(encodeURIComponent(key)) + '=([^;]*)').exec(document.cookie);
return match ? decodeURIComponent(match[1]) : null;
},
/**
* Sets the value of a cookie.
*/
setItem: function (/**string*/ key, /**string*/ data, /**cookieOptions=*/ options) {
options = options || {};
document.cookie = encodeURIComponent(key) + '=' + encodeURIComponent(data) + createCookieOptions(options);
},
/**
* Removes a cookie.
*/
removeItem: function (/**string*/ key, /**cookieOptions=*/ options) {
options = options ? Object.create(options) : {};
options.expires = longAgo;
document.cookie = encodeURIComponent(key) + '=' + createCookieOptions(options);
}
};
});