Instantly share code, notes, and snippets.
Created
January 25, 2014 03:51
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save adoc/8611566 to your computer and use it in GitHub Desktop.
cookies.js: Using cookie.js, adds some additional helpers to add/delete multiple cookies at one time. With JSON, also the ability to put objects in cookies.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
cookies.js | |
Using cookie.js, adds some additional helpers to add/delete multiple cookies | |
at one time. With JSON, also the ability to put objects in cookies. | |
Author: github.com/adoc | |
*/ | |
(function(window) { | |
var _Cookies = function () { | |
// ===================================================================== | |
// Let's start with some compatibility blarg that's probably "wrong". | |
// each_unify | |
// | |
// Replacement for _.each function that acts more like jquery's | |
// key/index: value vs. underscore's value: key/index. | |
var each_unify = function (list, iterator, context) { | |
_.each(list, function () { | |
iterator(arguments[1], arguments[0], arguments[2]); | |
}, | |
context); | |
} | |
var each; | |
// each_key | |
// Iterates over an array or object. Provides to `func` the item in the | |
// array or the keyname in the object. | |
// | |
// obj array or object | |
// func function | |
// | |
// callback: arg: obj key or array value of item. | |
var each_key = function (obj, func) { | |
each(obj, function (key) { | |
func(obj[key]); | |
}); | |
} | |
if (typeof Cookie !== "object" && !Cookie.get) { | |
throw "cookies.js requires cookie.js. (How cute.)"; | |
} | |
if (typeof JSON !== "object" && !JSON.stringify) { | |
throw "cookies.js requires JSON. (Kinda the point here.)"; | |
} | |
if (typeof _ === "function" && _.each) { | |
each = each_unify; | |
} | |
else if (typeof jQuery === "function" && jQuery.each) { | |
each = $.each | |
} | |
else { | |
throw "cookies.js requires either jQuery or Underscore.js."; | |
} | |
// http://andrewdupont.net/2009/08/28/deep-extending-objects-in-javascript/ | |
var deepExtend = function(destination, source) { | |
for (var property in source) { | |
if (source[property] && source[property].constructor && | |
source[property].constructor === Object) { | |
destination[property] = destination[property] || {}; | |
arguments.callee(destination[property], source[property]); | |
} else { | |
destination[property] = source[property]; | |
} | |
} | |
return destination; | |
}; | |
// ===================================================================== | |
// Cookies object. (The meat.) | |
var Cookies = { | |
// Get a JSON stringed cookie. | |
// key string Cookie name to get. | |
// opts object | |
// | |
// return: JSON parsed value of the cookie. | |
get_cookie: function(key, opts) { | |
opts = opts || {}; | |
opts.prefix = opts.prefix || ''; | |
if (!key) { | |
throw "_get_cookie: `key` is a required argument."; | |
} | |
var key = [opts.prefix, key].join('.'); | |
var cookie = Cookie.get(key); | |
if (cookie) { | |
return JSON.parse(cookie); | |
} else { | |
throw "_get_cookie: Returned cookie was empty ["+key+"]."; | |
} | |
}, | |
// Sets a cookie as a JSON string. | |
// key string Cookie name to set. | |
// val object JSON serializible cookie value to set. | |
// opts object | |
set_cookie: function(key, val, opts) { | |
opts = opts || {}; | |
opts.prefix = opts.prefix || ''; | |
if (!key) { | |
throw "_set_cookie: `key` is a required argument."; | |
} | |
if (val === undefined) { | |
throw "_set_cookie: `value` is a required argument."; | |
} | |
var key = [opts.prefix, key].join('.'); | |
var val = JSON.stringify(val); | |
Cookie.set(key, | |
val, | |
opts.ttl || 1, | |
opts.path || '/', | |
opts.domain || '', | |
opts.secure || false); | |
// Look in to these defaults. Might be too insecure to offer | |
// as defaults. | |
}, | |
// Unsets a cookie. | |
// key string Cookie name to unset. | |
// opts object | |
unset_cookie: function(key, opts) { | |
opts = opts || {}; | |
if (!key) { | |
throw "_unset_cookie: `key` is a required argument."; | |
} | |
var key = [opts.prefix, key].join('.'); | |
Cookie.unset(key); | |
if (opts.verify) { | |
try { | |
Cookie.get(key); | |
throw "_unset_cookie: Cookie '"+key+"' still exists."; | |
} catch(err) { | |
// Carry on. | |
} | |
} | |
}, | |
// Gets a list of cookies and returns them as an object. | |
// | |
// list array Array of cookie names to get. | |
// opts object Options passed around to create a psuedo-inheritence. | |
// | |
// return: Object of cookies. | |
get_cookies: function (list, opts) { | |
var that = this; | |
opts = opts || {}; | |
var cookies = {}; | |
each_key(list, function(key) { | |
cookies[key] = that.get_cookie(key, opts); | |
}); | |
return cookies; | |
}, | |
// Unsets a list of cookies. | |
// list array Array of cookie names to unset. | |
// opts object | |
unset_cookies: function(list, opts) { | |
var that = this; | |
opts = opts || {}; | |
each_key(list, function(key) { | |
that.unset_cookie(key, opts); | |
}); | |
}, | |
// Sets cookies. | |
// cookies object Object of cookies to be set. | |
// *key Name of cookie (not including opts.prefix.) | |
// *value String or object to be serialized in to cookie. | |
// opts object Options to be passed around. | |
// @ | |
set_cookies: function(cookies, opts) { | |
var that = this; | |
opts = opts || {}; | |
cookies = cookies || {}; | |
if (opts.deep) { | |
deepExtend(cookies, this.get_cookies(cookies, opts)); | |
} | |
each(cookies, function(key, val) { | |
that.set_cookie(key, val, opts); | |
}); | |
} | |
} | |
return Cookies; | |
} | |
// Require.js combatibility. | |
if (typeof define === "function" && define.amd) { | |
define( "cookies", ["cookie", "underscore"], function () { | |
return _Cookies(); // Init object. | |
}); | |
} | |
// Standard global. | |
else { | |
window.Cookies = _Cookies(); | |
} | |
}).call(window, this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment