forked from marcuswestin/std.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie.js
33 lines (28 loc) · 1.16 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
module.exports.get = function(name) {
var regex = new RegExp(
'(^|(; ))' + // beginning of document.cookie, or "; " which signifies the beginning of a new cookie
name +
'=([^;]*)') // the value of the cookie, matched up until a ";" or the end of the string
var match = document.cookie.match(regex),
value = match && match[3]
return value && decodeURIComponent(value)
}
module.exports.set = function(name, value, duration) {
if (duration === undefined) { duration = (365 * 24 * 60 * 60 * 1000) } // one year
var date = (duration instanceof Date ? duration : (duration < 0 ? null : new Date(new Date().getTime() + duration))),
expires = date ? "expires=" + date.toGMTString() + '; ' : '',
cookieName = name + '=' + encodeURIComponent(value) + '; ',
domain = 'domain='+document.domain+'; ',
path = 'path=/; '
document.cookie = cookieName + expires + domain + path
}
module.exports.isEnabled = function() {
var name = '__test__cookie' + new Date().getTime()
module.exports.set(name, 1)
var isEnabled = !!module.exports.get(name)
module.exports.remove(name)
return isEnabled
}
module.exports.remove = function(name) {
module.exports.set(name, "", new Date(1))
}