|
|
@ -1,4 +1,4 @@ |
|
|
|
var Cookies = require('cookies-js'); |
|
|
|
var Cookies = require('js-cookie'); |
|
|
|
var extend = require('./extend'); |
|
|
|
|
|
|
|
module.exports = cookie; |
|
|
@ -11,7 +11,9 @@ function cookie(str){ |
|
|
|
|
|
|
|
this.config = { |
|
|
|
key: str, |
|
|
|
options: {} |
|
|
|
options: { |
|
|
|
expires: 365 |
|
|
|
} |
|
|
|
}; |
|
|
|
this.data = this.get(); |
|
|
|
return this; |
|
|
@ -21,10 +23,10 @@ cookie.prototype.get = function(str){ |
|
|
|
var data = {}; |
|
|
|
|
|
|
|
if (Cookies.get(this.config.key)) { |
|
|
|
data = JSON.parse( decodeURIComponent(Cookies.get(this.config.key)) ); |
|
|
|
data = JSON.parse(Cookies.get(this.config.key)); |
|
|
|
} |
|
|
|
if (str) { |
|
|
|
return ('undefined' !== typeof data[str]) ? data[str] : null; |
|
|
|
return (typeof data[str] !== 'undefined') ? data[str] : null; |
|
|
|
} |
|
|
|
else { |
|
|
|
return data; |
|
|
@ -33,19 +35,23 @@ cookie.prototype.get = function(str){ |
|
|
|
|
|
|
|
cookie.prototype.set = function(str, value){ |
|
|
|
if (!arguments.length || !this.enabled()) return this; |
|
|
|
if ('string' === typeof str && arguments.length === 2) { |
|
|
|
if (typeof str === 'string' && arguments.length === 2) { |
|
|
|
this.data[str] = value ? value : null; |
|
|
|
} |
|
|
|
else if ('object' === typeof str && arguments.length === 1) { |
|
|
|
else if (typeof str === 'object' && arguments.length === 1) { |
|
|
|
extend(this.data, str); |
|
|
|
} |
|
|
|
Cookies.set(this.config.key, encodeURIComponent( JSON.stringify(this.data) ), this.config.options); |
|
|
|
Cookies.set(this.config.key, this.data, this.config.options); |
|
|
|
return this; |
|
|
|
}; |
|
|
|
|
|
|
|
cookie.prototype.expire = function(){ |
|
|
|
Cookies.expire(this.config.key); |
|
|
|
this.data = {}; |
|
|
|
cookie.prototype.expire = function(daysUntilExpire){ |
|
|
|
if (daysUntilExpire) { |
|
|
|
Cookies.set(this.config.key, this.data, extend(this.config.options, { expires: daysUntilExpire })); |
|
|
|
} else { |
|
|
|
Cookies.remove(this.config.key); |
|
|
|
this.data = {}; |
|
|
|
} |
|
|
|
return this; |
|
|
|
}; |
|
|
|
|
|
|
@ -56,5 +62,5 @@ cookie.prototype.options = function(obj){ |
|
|
|
}; |
|
|
|
|
|
|
|
cookie.prototype.enabled = function(){ |
|
|
|
return Cookies.enabled; |
|
|
|
return navigator.cookieEnabled; |
|
|
|
}; |
|
|
|