Comparing version 1.0.0 to 1.0.1
{ | ||
"name" : "cookies-js", | ||
"version" : "1.0.0", | ||
"version" : "1.0.1", | ||
"main" : "src/cookies.js", | ||
@@ -5,0 +5,0 @@ "ignore" : [ |
# Change Log | ||
## 1.0.1 | ||
- Fixed being unable to retrieve cookie values for keys that were named the same as a built-in `Object` property. | ||
## 1.0.0 | ||
@@ -4,0 +7,0 @@ - Put Cookies.js into the public domain. |
@@ -1,1 +0,1 @@ | ||
(function(e){"use strict";var b=function(a,d,c){return 1===arguments.length?b.get(a):b.set(a,d,c)};b._document=document;b._navigator=navigator;b.defaults={path:"/",secure:false};b.get=function(a){b._cachedDocumentCookie!==b._document.cookie&&b._renewCache();return b._cache[a]};b.set=function(a,d,c){c=b._getExtendedOptions(c);c.expires=b._getExpiresDate(d===e?-1:c.expires);b._document.cookie=b._generateCookieString(a,d,c);return b};b.expire=function(a,d){return b.set(a,e,d)};b._getExtendedOptions=function(a){return{path:a&& a.path||b.defaults.path,domain:a&&a.domain||b.defaults.domain,expires:a&&a.expires||b.defaults.expires,secure:a&&a.secure!==e?a.secure:b.defaults.secure}};b._isValidDate=function(a){return"[object Date]"===Object.prototype.toString.call(a)&&!isNaN(a.getTime())};b._getExpiresDate=function(a,d){d=d||new Date;switch(typeof a){case "number":a=new Date(d.getTime()+1E3*a);break;case "string":a=new Date(a)}if(a&&!b._isValidDate(a))throw Error("`expires` parameter cannot be converted to a valid Date instance"); return a};b._generateCookieString=function(a,b,c){a=a.replace(/[^#$&+\^`|]/g,encodeURIComponent);a=a.replace(/\(/g,"%28").replace(/\)/g,"%29");b=(b+"").replace(/[^!#$&-+\--:<-\[\]-~]/g,encodeURIComponent);c=c||{};a=a+"="+b+(c.path?";path="+c.path:"");a+=c.domain?";domain="+c.domain:"";a+=c.expires?";expires="+c.expires.toUTCString():"";return a+=c.secure?";secure":""};b._getCookieObjectFromString=function(a){var d={};a=a?a.split("; "):[];for(var c=0;c<a.length;c++){var f=b._getKeyValuePairFromCookieString(a[c]); d[f.key]===e&&(d[f.key]=f.value)}return d};b._getKeyValuePairFromCookieString=function(a){var b=a.indexOf("="),b=0>b?a.length:b;return{key:decodeURIComponent(a.substr(0,b)),value:decodeURIComponent(a.substr(b+1))}};b._renewCache=function(){b._cache=b._getCookieObjectFromString(b._document.cookie);b._cachedDocumentCookie=b._document.cookie};b._areEnabled=function(){var a="1"===b.set("cookies.js",1).get("cookies.js");b.expire("cookies.js");return a};b.enabled=b._areEnabled();"function"===typeof define&& define.amd?define(function(){return b}):"undefined"!==typeof exports?("undefined"!==typeof module&&module.exports&&(exports=module.exports=b),exports.Cookies=b):window.Cookies=b})(); | ||
(function(e){"use strict";var a=function(b,d,c){return 1===arguments.length?a.get(b):a.set(b,d,c)};a._document=document;a._navigator=navigator;a._cacheKeyPrefix="cookey.";a.defaults={path:"/",secure:!1};a.get=function(b){a._cachedDocumentCookie!==a._document.cookie&&a._renewCache();return a._cache[a._cacheKeyPrefix+b]};a.set=function(b,d,c){c=a._getExtendedOptions(c);c.expires=a._getExpiresDate(d===e?-1:c.expires);a._document.cookie=a._generateCookieString(b,d,c);return a};a.expire=function(b,d){return a.set(b, e,d)};a._getExtendedOptions=function(b){return{path:b&&b.path||a.defaults.path,domain:b&&b.domain||a.defaults.domain,expires:b&&b.expires||a.defaults.expires,secure:b&&b.secure!==e?b.secure:a.defaults.secure}};a._isValidDate=function(b){return"[object Date]"===Object.prototype.toString.call(b)&&!isNaN(b.getTime())};a._getExpiresDate=function(b,d){d=d||new Date;switch(typeof b){case "number":b=new Date(d.getTime()+1E3*b);break;case "string":b=new Date(b)}if(b&&!a._isValidDate(b))throw Error("`expires` parameter cannot be converted to a valid Date instance"); return b};a._generateCookieString=function(b,a,c){b=b.replace(/[^#$&+\^`|]/g,encodeURIComponent);b=b.replace(/\(/g,"%28").replace(/\)/g,"%29");a=(a+"").replace(/[^!#$&-+\--:<-\[\]-~]/g,encodeURIComponent);c=c||{};b=b+"="+a+(c.path?";path="+c.path:"");b+=c.domain?";domain="+c.domain:"";b+=c.expires?";expires="+c.expires.toUTCString():"";return b+=c.secure?";secure":""};a._getCacheFromString=function(b){var d={};b=b?b.split("; "):[];for(var c=0;c<b.length;c++){var f=a._getKeyValuePairFromCookieString(b[c]); d[a._cacheKeyPrefix+f.key]===e&&(d[a._cacheKeyPrefix+f.key]=f.value)}return d};a._getKeyValuePairFromCookieString=function(b){var a=b.indexOf("="),a=0>a?b.length:a;return{key:decodeURIComponent(b.substr(0,a)),value:decodeURIComponent(b.substr(a+1))}};a._renewCache=function(){a._cache=a._getCacheFromString(a._document.cookie);a._cachedDocumentCookie=a._document.cookie};a._areEnabled=function(){var b="1"===a.set("cookies.js",1).get("cookies.js");a.expire("cookies.js");return b};a.enabled=a._areEnabled(); "function"===typeof define&&define.amd?define(function(){return a}):"undefined"!==typeof exports?("undefined"!==typeof module&&module.exports&&(exports=module.exports=a),exports.Cookies=a):window.Cookies=a})(); |
{ | ||
"name" : "cookies-js", | ||
"version" : "1.0.0", | ||
"version" : "1.0.1", | ||
"author" : "Scott Hamper", | ||
@@ -5,0 +5,0 @@ "description" : "Client-Side Cookie Manipulation API", |
/* | ||
* Cookies.js - 1.0.0 | ||
* Cookies.js - 1.0.1 | ||
* https://github.com/ScottHamper/Cookies | ||
@@ -19,2 +19,6 @@ * | ||
// Used to ensure cookie keys do not collide with | ||
// built-in `Object` properties | ||
Cookies._cacheKeyPrefix = 'cookey.'; // Hurr hurr, :) | ||
Cookies.defaults = { | ||
@@ -30,3 +34,3 @@ path: '/', | ||
return Cookies._cache[key]; | ||
return Cookies._cache[Cookies._cacheKeyPrefix + key]; | ||
}; | ||
@@ -89,4 +93,4 @@ | ||
Cookies._getCookieObjectFromString = function (documentCookie) { | ||
var cookieObject = {}; | ||
Cookies._getCacheFromString = function (documentCookie) { | ||
var cookieCache = {}; | ||
var cookiesArray = documentCookie ? documentCookie.split('; ') : []; | ||
@@ -97,8 +101,8 @@ | ||
if (cookieObject[cookieKvp.key] === undefined) { | ||
cookieObject[cookieKvp.key] = cookieKvp.value; | ||
if (cookieCache[Cookies._cacheKeyPrefix + cookieKvp.key] === undefined) { | ||
cookieCache[Cookies._cacheKeyPrefix + cookieKvp.key] = cookieKvp.value; | ||
} | ||
} | ||
return cookieObject; | ||
return cookieCache; | ||
}; | ||
@@ -120,3 +124,3 @@ | ||
Cookies._renewCache = function () { | ||
Cookies._cache = Cookies._getCookieObjectFromString(Cookies._document.cookie); | ||
Cookies._cache = Cookies._getCacheFromString(Cookies._document.cookie); | ||
Cookies._cachedDocumentCookie = Cookies._document.cookie; | ||
@@ -123,0 +127,0 @@ }; |
@@ -99,7 +99,11 @@ /* | ||
it('returns the value of `Cookies._cache[key]`', function () { | ||
Cookies._cachedDocumentCookie = mockDocument.cookie; // Prevents cache from renewing | ||
Cookies._cache = { key: 'value' }; | ||
it('returns the value of the cookie when it exists', function () { | ||
expect(Cookies.get(key)).toEqual('value'); | ||
}); | ||
it('returns the value of keys that are named the same as built-in `Object` properties', function () { | ||
key = 'constructor'; | ||
mockDocument.cookie = 'constructor=value'; | ||
expect(Cookies.get(key)).toEqual(Cookies._cache[key]); | ||
expect(Cookies.get(key)).toEqual('value'); | ||
}); | ||
@@ -413,9 +417,10 @@ }); | ||
describe('Cookies._getCookieObjectFromString(documentCookie)', function () { | ||
describe('Cookies._getCacheFromString(documentCookie)', function () { | ||
it('returns an object of cookie key/value pairs', function () { | ||
var documentCookie = 'key=value; scott=hamper'; | ||
expect(Cookies._getCookieObjectFromString(documentCookie)).toEqual({ | ||
key: 'value', | ||
scott: 'hamper' | ||
}); | ||
var expected = {}; | ||
expected[Cookies._cacheKeyPrefix + 'key'] = 'value'; | ||
expected[Cookies._cacheKeyPrefix + 'scott'] = 'hamper'; | ||
expect(Cookies._getCacheFromString(documentCookie)).toEqual(expected); | ||
}); | ||
@@ -425,3 +430,3 @@ | ||
var documentCookie = ''; | ||
expect(Cookies._getCookieObjectFromString(documentCookie)).toEqual({}); | ||
expect(Cookies._getCacheFromString(documentCookie)).toEqual({}); | ||
}); | ||
@@ -431,5 +436,6 @@ | ||
var documentCookie = 'key=value; key=scott'; | ||
expect(Cookies._getCookieObjectFromString(documentCookie)).toEqual({ | ||
key: 'value' | ||
}); | ||
var expected = {}; | ||
expected[Cookies._cacheKeyPrefix + 'key'] = 'value'; | ||
expect(Cookies._getCacheFromString(documentCookie)).toEqual(expected); | ||
}); | ||
@@ -475,11 +481,11 @@ }); | ||
describe('Cookies._renewCache()', function () { | ||
it('sets `Cookies._cache` to `Cookies._getCookieObjectFromString(document.cookie)`', function () { | ||
it('sets `Cookies._cache` to `Cookies._getCacheFromString(document.cookie)`', function () { | ||
mockDocument.cookie = 'key=value'; | ||
Cookies._cache = undefined; | ||
spyOn(Cookies, '_getCookieObjectFromString').andCallThrough(); | ||
spyOn(Cookies, '_getCacheFromString').andCallThrough(); | ||
Cookies._renewCache(); | ||
expect(Cookies._getCookieObjectFromString).toHaveBeenCalledWith(mockDocument.cookie); | ||
expect(Cookies._cache).toEqual(Cookies._getCookieObjectFromString(mockDocument.cookie)); | ||
expect(Cookies._getCacheFromString).toHaveBeenCalledWith(mockDocument.cookie); | ||
expect(Cookies._cache).toEqual(Cookies._getCacheFromString(mockDocument.cookie)); | ||
}); | ||
@@ -486,0 +492,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
148811
3489