Permalink
Browse files

Internalize type checks.

  • Loading branch information...
1 parent 6d2ddb9 commit 83c987ae7f99dccf312bf482345926fe1c9493a3 @jonlabelle committed Apr 9, 2013
Showing with 84 additions and 111 deletions.
  1. +83 −95 Cookie.js
  2. +1 −16 Cookie.min.js
View
@@ -1,17 +1,17 @@
/*
- * Cookie.js v1.2
+ * Cookie.js v1.3
*
* Manage all your cookies and sub-cookies with Cookie.js. Cookies.js is a
* stand-alone script and does NOT require additional libraries (jQuery, YUI,
* etc.) to operate.
*
- * Copyright (c) 2012, Jon LaBelle
+ * Copyright (c) 2013, Jon LaBelle
* http://jonlabelle.com
*
* Licensed under the MIT license:
* http://creativecommons.org/licenses/MIT/
*
- * Date: Wed Sep 19 10:41:55 2012 -0500
+ * Date: Tue Apr 09 04:44:13 2013 -0500
*/
(function (window, undefined) {
@@ -20,81 +20,68 @@
var encode = encodeURIComponent,
decode = decodeURIComponent;
- var Cookie = {
-
- /**
- * Iterate over an object.
- *
- * @access internal
- * @param {obj} obj The object to iterate.
- * @param {Function} callback The callback function.
- */
- each: function (obj, callback) {
- for (var k in obj) {
- if (obj.hasOwnProperty(k)) {
- callback(k, obj[k]);
- }
+ /**
+ * Is Array
+ *
+ * @param {mixed} obj Object to test.
+ * @return {bool}
+ */
+ function isArray(obj) {
+ return (typeof obj.length === "undefined") ? false : true;
+ }
+
+ /**
+ * Is Number check.
+ *
+ * @param {mixed} val The value to check.
+ * @return {bool}
+ */
+ function isNumeric (val) {
+ return !isNaN(parseFloat(val)) && isFinite(val);
+ }
+
+ /**
+ * Merges two objects.
+ *
+ * @param {obj} original
+ * @param {obj} newObject
+ * @return {obj} The combined merged objects.
+ */
+ function merge (original, newObject) {
+
+ for (var key in newObject) {
+ if (newObject.hasOwnProperty(key)) {
+ var newPropertyValue = newObject[key];
+ var originalPropertyValue = original[key];
}
- },
- /**
- * Merges two objects.
- *
- * @access internal
- * @param {obj} original
- * @param {obj} newObject
- * @return {obj} The combined merged objects.
- */
- merge: function (original, newObject) {
- for (var key in newObject) {
- if (newObject.hasOwnProperty(key)) {
- var newPropertyValue = newObject[key];
- var originalPropertyValue = original[key];
- }
- original[key] = (originalPropertyValue && typeof newPropertyValue === 'object' && typeof originalPropertyValue === 'object') ? this.merge(originalPropertyValue, newPropertyValue) : newPropertyValue;
- }
- return original;
- },
+ original[key] = (originalPropertyValue && typeof newPropertyValue === "object" && typeof originalPropertyValue === "object") ? merge(originalPropertyValue, newPropertyValue) : newPropertyValue;
+ }
- /**
- * Checks if an object has any properties.
- *
- * @param {obj} obj
- * @return {bool}
- */
- isEmptyObject: function(obj) {
- var name;
- for (name in obj) {
- return false;
+ return original;
+ }
+
+ /**
+ * Iterate over an object.
+ *
+ * @param {obj} obj The object to iterate.
+ * @param {Function} callback The callback function.
+ */
+ function each (obj, callback) {
+
+ for (var k in obj) {
+ if (obj.hasOwnProperty(k)) {
+ callback(k, obj[k]);
}
- return true;
- },
+ }
- /**
- * Is Array
- *
- * @access internal
- * @param {mixed} obj Object to test.
- * @return {bool}
- */
- isArray: Array.isArray || function (obj) {
- return (typeof (obj.length) === "undefined") ? false : true;
- },
+ }
- /**
- * Is Number check.
- *
- * @param {mixed} val The value to check.
- * @return {bool}
- */
- isNumeric: function (val) {
- return !isNaN(parseFloat(val)) && isFinite(val);
- },
+ var Cookie = {
/**
* Creates the cookie string.
*
- * @access internal
* @param {string} name
* @param {string} value
* @param {string} encodeValue Whether or not to encode the cookie values.
@@ -114,13 +101,14 @@
if (expires instanceof Date) {
text += "; expires=" + expires.toUTCString();
- } else if (this.isNumeric(expires)) {
+ } else if (isNumeric(expires)) {
// EXPIRATION AS DAYS (int)
var days = expires;
var t = expires = new Date();
t.setDate(t.getDate() + days);
+
text += "; expires=" + expires.toUTCString();
}
@@ -143,19 +131,18 @@
/**
* Creates a string from a hash/object.
*
- * @access internal
* @param {obj} hash
* @return {string}
*/
createCookieHashString: function (hash) {
- if (hash === null || typeof (hash) !== "object") {
+ if (hash === null || typeof hash !== "object") {
return "";
}
var text = [];
- this.each(hash, function (key, value) {
+ each(hash, function (key, value) {
if (typeof value !== "function" && typeof value !== "undefined") {
text.push(encode(key) + "=" + encode(String(value)));
}
@@ -190,7 +177,6 @@
/**
* Parses a cookie into an object representation.
*
- * @access internal
* @param {string} text
* @param {bool} shouldDecode Whether or not to decode the cookie values.
* @return {obj}
@@ -199,7 +185,7 @@
var cookies = {};
- if (typeof (text) === "string" && text.length > 0) {
+ if (typeof text === "string" && text.length > 0) {
var decodeValue = (shouldDecode === false ? function (s) {
return s;
@@ -214,9 +200,10 @@
// check for normally-formatted cookie (name-value)
cookieNameValue = cookieParts[i].match(/([^=]+)=/i);
- if (this.isArray(cookieNameValue)) {
+ if (isArray(cookieNameValue)) {
try {
+
cookieName = decode(cookieNameValue[1]);
cookieValue = decodeValue(cookieParts[i].substring(cookieNameValue[1].length + 1));
@@ -225,7 +212,8 @@
}
} else {
- // means the cookie does not have an "=", so treat it as a boolean flag
+ // means the cookie does not have an "=", so treat it as a
+ // boolean flag
cookieName = decode(cookieParts[i]);
cookieValue = "";
}
@@ -262,11 +250,11 @@
converter;
// if options is a function, then it's the converter
- if (typeof (options) === "function") {
+ if (typeof options === "function") {
converter = options;
options = {};
- } else if (typeof (options) === "object") {
+ } else if (typeof options === "object") {
converter = options.converter;
} else {
@@ -277,11 +265,11 @@
cookie = cookies[name];
// should return null, not undefined if the cookie doesn't exist
- if (cookie === null || typeof (cookie) === "undefined") {
+ if (cookie === null || typeof cookie === "undefined") {
return null;
}
- if (typeof (converter) === "function") {
+ if (typeof converter === "function") {
return converter(cookie);
}
@@ -303,15 +291,15 @@
return null;
}
- if (typeof (subName) === "undefined" || subName === null) {
+ if (typeof subName === "undefined" || subName === null) {
return null;
}
- if (hash[subName] === null || typeof (hash[subName]) === "undefined") {
+ if (hash[subName] === null || typeof hash[subName] === "undefined") {
return null;
}
- if (typeof (converter) === "function") {
+ if (typeof converter === "function") {
return converter(hash[subName]);
}
@@ -328,7 +316,7 @@
var cookies = this.parseCookieString(document.cookie, false);
- if (typeof (cookies[name]) === "string") {
+ if (typeof cookies[name] === "string") {
return this.parseCookieHash(cookies[name]);
}
@@ -345,7 +333,7 @@
remove: function (name, options) {
// set options
- options = this.merge(options || {}, {
+ options = merge(options || {}, {
expires: new Date(0)
});
@@ -364,11 +352,11 @@
* @return {string}
*/
removeSub: function (name, subName, options) {
- if (typeof (name) === "undefined" || name === null) {
+ if (typeof name === "undefined" || name === null) {
return "";
}
- if (typeof (subName) === "undefined" || subName === null) {
+ if (typeof subName === "undefined" || subName === null) {
return "";
}
@@ -378,7 +366,7 @@
var subs = this.getSubs(name);
// delete the indicated subcookie
- if (typeof (subs) === "object" && subs.hasOwnProperty(subName)) {
+ if (typeof subs === "object" && subs.hasOwnProperty(subName)) {
delete subs[subName];
if (!options.removeIfEmpty) {
@@ -390,7 +378,7 @@
// reset the cookie if there are subcookies left, else remove
for (var key in subs) {
- if (subs.hasOwnProperty(key) && !typeof subs[key] === "function" && !typeof subs[key] === "undefined") {
+ if (subs.hasOwnProperty(key) && typeof subs[key] !== "function" && typeof subs[key] !== "undefined") {
return this.setSubs(name, subs, options);
}
}
@@ -433,18 +421,18 @@
*/
setSub: function (name, subName, value, options) {
- if (typeof (name) === "undefined" || typeof (subName) === "undefined") {
+ if (typeof name === "undefined" || typeof subName === "undefined") {
return "";
}
- if (typeof (value) === "undefined") {
+ if (typeof value === "undefined") {
//error("Cookie.setSub(): Subcookie value cannot be undefined.");
value = "";
}
var hash = this.getSubs(name);
- if (hash === null || typeof (hash) !== "object") {
+ if (hash === null || typeof hash !== "object") {
hash = {};
}
@@ -463,11 +451,11 @@
*/
setSubs: function (name, value, options) {
- if (name === null || typeof (name) === "undefined") {
+ if (name === null || typeof name === "undefined") {
return "";
}
- if (value === null || typeof (value) === "undefined" || typeof (value) !== "object") {
+ if (value === null || typeof value === "undefined" || typeof value !== "object") {
return "";
}
@@ -505,4 +493,4 @@
window.Cookie = Cookie;
-}(window));
+}(window));
View
@@ -1,16 +1 @@
-/*
- * Cookie.js v1.2
- *
- * Manage all your cookies and sub-cookies with Cookie.js. Cookies.js is a
- * stand-alone script and does NOT require additional libraries (jQuery, YUI,
- * etc.) to operate.
- *
- * Copyright (c) 2012, Jon LaBelle
- * http://jonlabelle.com
- *
- * Licensed under the MIT license:
- * http://creativecommons.org/licenses/MIT/
- *
- * Date: Wed Sep 19 10:41:55 2012 -0500
- */
-(function(e,t){"use strict";var n=encodeURIComponent,r=decodeURIComponent,i={each:function(e,t){for(var n in e)e.hasOwnProperty(n)&&t(n,e[n])},merge:function(e,t){for(var n in t){if(t.hasOwnProperty(n))var r=t[n],i=e[n];e[n]=i&&typeof r=="object"&&typeof i=="object"?this.merge(i,r):r}return e},isEmptyObject:function(e){var t;for(t in e)return!1;return!0},isArray:Array.isArray||function(e){return typeof e.length=="undefined"?!1:!0},isNumeric:function(e){return!isNaN(parseFloat(e))&&isFinite(e)},createCookieString:function(e,t,r,i){i=i||{};var s=n(e)+"="+(r?n(t):t),o=i.expires,u=i.path||"/",a=i.domain;if(o instanceof Date)s+="; expires="+o.toUTCString();else if(this.isNumeric(o)){var f=o,l=o=new Date;l.setDate(l.getDate()+f),s+="; expires="+o.toUTCString()}return s+="; path="+u,a&&(s+="; domain="+a),i.secure===!0&&(s+="; secure"),s},createCookieHashString:function(e){if(e===null||typeof e!="object")return"";var t=[];return this.each(e,function(e,r){typeof r!="function"&&typeof r!="undefined"&&t.push(n(e)+"="+n(String(r)))}),t.join("&")},parseCookieHash:function(e){var t=e.split("&"),n=null,i={};if(e.length)for(var s=0,o=t.length;s<o;s++)n=t[s].split("="),i[r(n[0])]=r(n[1]);return i},parseCookieString:function(e,t){var n={};if(typeof e=="string"&&e.length>0){var i=t===!1?function(e){return e}:r,s=e.split(/;\s/g),o=null,u=null,a=null;for(var f=0,l=s.length;f<l;f++){a=s[f].match(/([^=]+)=/i);if(this.isArray(a))try{o=r(a[1]),u=i(s[f].substring(a[1].length+1))}catch(c){}else o=r(s[f]),u="";n[o]=u}}return n},exists:function(e){var t=this.parseCookieString(document.cookie,!0);return t.hasOwnProperty(e)},get:function(e,t){var n,r,i;return typeof t=="function"?(i=t,t={}):typeof t=="object"?i=t.converter:t={},n=this.parseCookieString(document.cookie,!t.raw),r=n[e],r===null||typeof r=="undefined"?null:typeof i=="function"?i(r):r},getSub:function(e,t,n){var r=this.getSubs(e);return r===null?null:typeof t=="undefined"||t===null?null:r[t]===null||typeof r[t]=="undefined"?null:typeof n=="function"?n(r[t]):r[t]},getSubs:function(e){var t=this.parseCookieString(document.cookie,!1);return typeof t[e]=="string"?this.parseCookieHash(t[e]):null},remove:function(e,t){return t=this.merge(t||{},{expires:new Date(0)}),this.set(e,"",t),this},removeSub:function(e,t,n){if(typeof e=="undefined"||e===null)return"";if(typeof t=="undefined"||t===null)return"";n=n||{};var r=this.getSubs(e);if(typeof r=="object"&&r.hasOwnProperty(t)){delete r[t];if(!n.removeIfEmpty)return this.setSubs(e,r,n);for(var i in r)if(r.hasOwnProperty(i)&&!typeof r[i]==="function"&&!typeof r[i]==="undefined")return this.setSubs(e,r,n);return this.remove(e,n)}return""},set:function(e,t,n){typeof t=="undefined"&&(t=""),n=n||{};var r=this.createCookieString(e,t,!n.raw,n);return document.cookie=r,this},setSub:function(e,t,n,r){if(typeof e=="undefined"||typeof t=="undefined")return"";typeof n=="undefined"&&(n="");var i=this.getSubs(e);if(i===null||typeof i!="object")i={};return i[t]=n,this.setSubs(e,i,r)},setSubs:function(e,t,n){if(e===null||typeof e=="undefined")return"";if(t===null||typeof t=="undefined"||typeof t!="object")return"";var r=this.createCookieString(e,this.createCookieHashString(t),!1,n);return document.cookie=r,r},enabled:function(){var e="jUQFUy5j3vHnA14R",t="cookieMonster",n=navigator.cookieEnabled?!0:!1;return typeof navigator.cookieEnabled=="undefined"&&!n&&(this.set(e,t),n=this.exists(e)),n&&this.remove(e),n}};e.Cookie=i})(window);;
+(function(window,undefined){"use strict";var encode=encodeURIComponent,decode=decodeURIComponent;function isArray(obj){return typeof obj.length==="undefined"?false:true}function isNumeric(val){return!isNaN(parseFloat(val))&&isFinite(val)}function merge(original,newObject){for(var key in newObject){if(newObject.hasOwnProperty(key)){var newPropertyValue=newObject[key];var originalPropertyValue=original[key]}original[key]=originalPropertyValue&&typeof newPropertyValue==="object"&&typeof originalPropertyValue==="object"?merge(originalPropertyValue,newPropertyValue):newPropertyValue}return original}function each(obj,callback){for(var k in obj){if(obj.hasOwnProperty(k)){callback(k,obj[k])}}}var Cookie={createCookieString:function(name,value,encodeValue,options){options=options||{};var text=encode(name)+"="+(encodeValue?encode(value):value),expires=options.expires,path=options.path||"/",domain=options.domain;if(expires instanceof Date){text+="; expires="+expires.toUTCString()}else if(isNumeric(expires)){var days=expires;var t=expires=new Date;t.setDate(t.getDate()+days);text+="; expires="+expires.toUTCString()}text+="; path="+path;if(domain){text+="; domain="+domain}if(options.secure===true){text+="; secure"}return text},createCookieHashString:function(hash){if(hash===null||typeof hash!=="object"){return""}var text=[];each(hash,function(key,value){if(typeof value!=="function"&&typeof value!=="undefined"){text.push(encode(key)+"="+encode(String(value)))}});return text.join("&")},parseCookieHash:function(text){var hashParts=text.split("&"),hashPart=null,hash={};if(text.length){for(var i=0,len=hashParts.length;i<len;i++){hashPart=hashParts[i].split("=");hash[decode(hashPart[0])]=decode(hashPart[1])}}return hash},parseCookieString:function(text,shouldDecode){var cookies={};if(typeof text==="string"&&text.length>0){var decodeValue=shouldDecode===false?function(s){return s}:decode,cookieParts=text.split(/;\s/g),cookieName=null,cookieValue=null,cookieNameValue=null;for(var i=0,len=cookieParts.length;i<len;i++){cookieNameValue=cookieParts[i].match(/([^=]+)=/i);if(isArray(cookieNameValue)){try{cookieName=decode(cookieNameValue[1]);cookieValue=decodeValue(cookieParts[i].substring(cookieNameValue[1].length+1))}catch(ex){}}else{cookieName=decode(cookieParts[i]);cookieValue=""}cookies[cookieName]=cookieValue}}return cookies},exists:function(name){var cookies=this.parseCookieString(document.cookie,true);return cookies.hasOwnProperty(name)},get:function(name,options){var cookies,cookie,converter;if(typeof options==="function"){converter=options;options={}}else if(typeof options==="object"){converter=options.converter}else{options={}}cookies=this.parseCookieString(document.cookie,!options.raw);cookie=cookies[name];if(cookie===null||typeof cookie==="undefined"){return null}if(typeof converter==="function"){return converter(cookie)}return cookie},getSub:function(name,subName,converter){var hash=this.getSubs(name);if(hash===null){return null}if(typeof subName==="undefined"||subName===null){return null}if(hash[subName]===null||typeof hash[subName]==="undefined"){return null}if(typeof converter==="function"){return converter(hash[subName])}return hash[subName]},getSubs:function(name){var cookies=this.parseCookieString(document.cookie,false);if(typeof cookies[name]==="string"){return this.parseCookieHash(cookies[name])}return null},remove:function(name,options){options=merge(options||{},{expires:new Date(0)});this.set(name,"",options);return this},removeSub:function(name,subName,options){if(typeof name==="undefined"||name===null){return""}if(typeof subName==="undefined"||subName===null){return""}options=options||{};var subs=this.getSubs(name);if(typeof subs==="object"&&subs.hasOwnProperty(subName)){delete subs[subName];if(!options.removeIfEmpty){return this.setSubs(name,subs,options)}else{for(var key in subs){if(subs.hasOwnProperty(key)&&typeof subs[key]!=="function"&&typeof subs[key]!=="undefined"){return this.setSubs(name,subs,options)}}return this.remove(name,options)}}else{return""}},set:function(name,value,options){if(typeof value==="undefined"){value=""}options=options||{};var text=this.createCookieString(name,value,!options.raw,options);document.cookie=text;return this},setSub:function(name,subName,value,options){if(typeof name==="undefined"||typeof subName==="undefined"){return""}if(typeof value==="undefined"){value=""}var hash=this.getSubs(name);if(hash===null||typeof hash!=="object"){hash={}}hash[subName]=value;return this.setSubs(name,hash,options)},setSubs:function(name,value,options){if(name===null||typeof name==="undefined"){return""}if(value===null||typeof value==="undefined"||typeof value!=="object"){return""}var text=this.createCookieString(name,this.createCookieHashString(value),false,options);document.cookie=text;return text},enabled:function(){var key="jUQFUy5j3vHnA14R",val="cookieMonster";var cookieEnabled=navigator.cookieEnabled?true:false;if(typeof navigator.cookieEnabled==="undefined"&&!cookieEnabled){this.set(key,val);cookieEnabled=this.exists(key)}if(cookieEnabled){this.remove(key)}return cookieEnabled}};window.Cookie=Cookie})(window);;

0 comments on commit 83c987a

Please sign in to comment.