-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.js
184 lines (150 loc) · 4.85 KB
/
index.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Cookie
// -------------
// Thanks to:
// - http://www.nczonline.net/blog/2009/05/05/http-cookies-explained/
// - http://developer.yahoo.com/yui/3/cookie/
var Cookie = exports;
var decode = decodeURIComponent;
var encode = encodeURIComponent;
/**
* Returns the cookie value for the given name.
*
* @param {String} name The name of the cookie to retrieve.
*
* @param {Function|Object} options (Optional) An object containing one or
* more cookie options: raw (true/false) and converter (a function).
* The converter function is run on the value before returning it. The
* function is not used if the cookie doesn't exist. The function can be
* passed instead of the options object for conveniently. When raw is
* set to true, the cookie value is not URI decoded.
*
* @return {*} If no converter is specified, returns a string or undefined
* if the cookie doesn't exist. If the converter is specified, returns
* the value returned from the converter.
*/
Cookie.get = function(name, options) {
validateCookieName(name);
if (typeof options === 'function') {
options = { converter: options };
}
else {
options = options || {};
}
var cookies = parseCookieString(document.cookie, !options['raw']);
return (options.converter || same)(cookies[name]);
};
/**
* Sets a cookie with a given name and value.
*
* @param {string} name The name of the cookie to set.
*
* @param {*} value The value to set for the cookie.
*
* @param {Object} options (Optional) An object containing one or more
* cookie options: path (a string), domain (a string),
* expires (number or a Date object), secure (true/false),
* and raw (true/false). Setting raw to true indicates that the cookie
* should not be URI encoded before being set.
*
* @return {string} The created cookie string.
*/
Cookie.set = function(name, value, options) {
validateCookieName(name);
options = options || {};
var expires = options['expires'];
var domain = options['domain'];
var path = options['path'];
if (!options['raw']) {
value = encode(String(value));
}
var text = name + '=' + value;
// expires
var date = expires;
if (typeof date === 'number') {
date = new Date();
date.setDate(date.getDate() + expires);
}
if (date instanceof Date) {
text += '; expires=' + date.toUTCString();
}
// domain
if (isNonEmptyString(domain)) {
text += '; domain=' + domain;
}
// path
if (isNonEmptyString(path)) {
text += '; path=' + path;
}
// secure
if (options['secure']) {
text += '; secure';
}
document.cookie = text;
return text;
};
/**
* Removes a cookie from the machine by setting its expiration date to
* sometime in the past.
*
* @param {string} name The name of the cookie to remove.
*
* @param {Object} options (Optional) An object containing one or more
* cookie options: path (a string), domain (a string),
* and secure (true/false). The expires option will be overwritten
* by the method.
*
* @return {string} The created cookie string.
*/
Cookie.remove = function(name, options) {
options = options || {};
options['expires'] = new Date(0);
return this.set(name, '', options);
};
function parseCookieString(text, shouldDecode) {
var cookies = {};
if (isString(text) && text.length > 0) {
var decodeValue = shouldDecode ? decode : same;
var cookieParts = text.split(/;\s/g);
var cookieName;
var cookieValue;
var cookieNameValue;
for (var i = 0, len = cookieParts.length; i < len; i++) {
// Check for normally-formatted cookie (name-value)
cookieNameValue = cookieParts[i].match(/([^=]+)=/i);
if (cookieNameValue instanceof Array) {
try {
cookieName = decode(cookieNameValue[1]);
cookieValue = decodeValue(cookieParts[i]
.substring(cookieNameValue[1].length + 1));
} catch (ex) {
// Intentionally ignore the cookie -
// the encoding is wrong
}
} else {
// Means the cookie does not have an "=", so treat it as
// a boolean flag
cookieName = decode(cookieParts[i]);
cookieValue = '';
}
if (cookieName) {
cookies[cookieName] = cookieValue;
}
}
}
return cookies;
}
// Helpers
function isString(o) {
return typeof o === 'string';
}
function isNonEmptyString(s) {
return isString(s) && s !== '';
}
function validateCookieName(name) {
if (!isNonEmptyString(name)) {
throw new TypeError('Cookie name must be a non-empty string');
}
}
function same(s) {
return s;
}