forked from kahnvex/cookie-monster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (43 loc) · 1.21 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
'use strict';
exports = module.exports = function (doc) {
if (!doc) doc = {};
if (typeof doc === 'string') doc = { cookie: doc };
if (doc.cookie === undefined) doc.cookie = '';
var self = {};
self.get = function (key) {
var cookiesSplat = doc.cookie.split(/;\s*/);
for (var i = 0; i < cookiesSplat.length; i++) {
var ps = cookiesSplat[i].split('=');
var k = decodeURIComponent(ps[0]);
if (k === key) return decodeURIComponent(ps[1]);
}
};
self.set = function (key, value, opts) {
if (!opts) opts = {};
var newCookie = encodeURIComponent(key) + '=' + encodeURIComponent(value);
if (opts.expires){
newCookie += '; expires=' + opts.expires;
}
if (opts.path) {
newCookie += '; path=' + opts.path;
}
if (opts.domain) {
newCookie += '; domain=' + opts.domain;
}
if (opts.secure) {
newCookie += '; secure';
}
doc.cookie = newCookie;
return newCookie;
};
self.del = function (key) {
return self.set(key, '', { expires: Date(0) });
};
return self;
};
if (typeof document !== 'undefined') {
var cookie = exports(document);
exports.get = cookie.get;
exports.set = cookie.set;
exports.del = cookie.del;
}