-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
83 lines (81 loc) · 3.6 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
var VueCookies = {
// install of Vue
install: function(Vue) {
if (!Vue.prototype.$cookie) {
Object.defineProperties(Vue.prototype, {
$cookie: {
get: function() {
return VueCookies;
},
},
});
}
},
get: function(key) {
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(key).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
},
set: function(key, value, expireValue, path, domain, secure) {
if (!key) {
throw new Error("cookie name is not find in first argument")
}else if(/^(?:expires|max\-age|path|domain|secure)$/i.test(key)){
throw new Error("cookie key name illegality ,Cannot be set to ['expires','max-age','path','domain','secure']\t","current key name: "+key);
}
var _expires = "; max-age="; // default expire time for 1 day
var expireTimes = expireValue || '1d';
if (expireTimes) {
switch (expireTimes.constructor) {
case Number:
if(expireTimes === Infinity || expireTimes === -1) _expires = "; expires=Fri, 31 Dec 9999 23:59:59 GMT"
else _expires = "; max-age=" + expireTimes;
break;
case String:
if (/^(?:\d{1,}(y|m|d|h|min|s))$/i.test(expireTimes)) {
// get capture number group
var _expireTime = expireTimes.replace(/^(\d{1,})(?:y|m|d|h|min|s)$/i, "$1");
// get capture type group , to lower case
switch (expireTimes.replace(/^(?:\d{1,})(y|m|d|h|min|s)$/i, "$1").toLowerCase()) {
// Frequency sorting
case 'm': _expires = "; max-age=" + +_expireTime * 259200; break; // 60 * 60 * 24 * 30
case 'd': _expires = "; max-age=" + +_expireTime * 86400; break; // 60 * 60 * 24
case 'h': _expires = "; max-age=" + +_expireTime * 3600; break; // 60 * 60
case 'min': _expires = "; max-age=" + +_expireTime * 60; break; // 60
case 's': _expires = "; max-age=" + _expireTime; break;
case 'y': _expires = "; max-age=" + +_expireTime * 31104000; break; // 60 * 60 * 24 * 30 * 12
default: new Error("unknown exception of 'set operation'");
}
} else {
_expires = "; expires=" + expireTimes;
}
break;
case Date:
_expires = "; expires=" + expireTimes.toUTCString();
break;
}
} else {
_expires = "; max-age=" + + 86400;
}
document.cookie = encodeURIComponent(key) + "=" + encodeURIComponent(value) + _expires + (domain ? "; domain=" + domain : "") + (path ? "; path=" + path : "; path=/") + (secure ? "; secure" : "");
return this;
},
remove: function(key, path, domain) {
if (!key || !this.isKey(key)) {
return false;
}
document.cookie = encodeURIComponent(key) + "=; expires="+ new Date() +"" + "; domain=" + (domain ? domain : document.domain) + (path ? "; path=" + path : "; path=/");
return true;
},
isKey: function(key) {
return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(key).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
},
keys: /* optional method: you can safely remove it! */ function() {
var _keys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
for (var _index = 0; _index < _keys.length; _index++) {
_keys[_index] = decodeURIComponent(_keys[_index]);
}
return _keys;
}
}
export default VueCookies;
if(typeof window!=='undefined' && !window.$cookie){
window.$cookie = VueCookies;
}