forked from kaelzhang/neuron.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookie.js
More file actions
72 lines (60 loc) · 1.76 KB
/
cookie.js
File metadata and controls
72 lines (60 loc) · 1.76 KB
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
function escapeRegExp(str){
return str.replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1")
}
var
default_options = {
path: '/',
domain: false,
duration: false,
secure: false,
document: document,
encode: true
},
Cookie = NR.Class({
Implements: 'attrs',
initialize: function (key, options) {
this.key = key;
this.set({ opt: options });
},
write: function (value) {
var o = this.get('opt');
if (o.encode) value = encodeURIComponent(value);
if (o.domain) value += '; domain=' + o.domain;
if (o.path) value += '; path=' + o.path;
if (o.duration) {
var date = new Date();
date.setTime(date.getTime() + o.duration * 24 * 60 * 60 * 1000);
value += '; expires=' + date.toGMTString();
}
if (o.secure) value += '; secure';
o.document.cookie = this.key + '=' + value;
this.set('opt',o);
return this;
},
read: function () {
var o = this.get('opt');
var value = o.document.cookie.match('(?:^|;)\\s*' + escapeRegExp(this.key ) + '=([^;]*)');
return (value) ? decodeURIComponent(value[1]) : null;
},
dispose: function () {
new Cookie(this.key, NR.mix({ duration: -1 },this.get('opt'), false)).write('');
return this;
}
}, {
opt: {
value: default_options,
setter: function (v) {
return NR.mix(v || {},this.get('opt'),false);
}
}
});
Cookie.write = function (key, value, options) {
return new Cookie(key, options).write(value);
};
Cookie.read = function (key) {
return new Cookie(key).read();
};
Cookie.dispose = function (key, options) {
return new Cookie(key, options).dispose();
};
module.exports = Cookie;