Plato on Github
Report Home
node_modules\atropa-CookieMonster\src\atropa-CookieMonster.js
Maintainability
69.01
Lines of code
333
Difficulty
39.83
Estimated Errors
0.94
Function weight
By Complexity
By SLOC
/// <reference path="../docs/vsdoc/OpenLayersAll.js"/> var atropa = require('atropa-header'); /*jslint indent: 4, maxerr: 50, white: true, browser: true, devel: true, plusplus: true, regexp: true */ /*global atropa */ // end header atropa.requires( 'CookieMonster', function () { "use strict"; var supported = true; [ document.cookie ].forEach(function (prerequisite) { if(prerequisite === undefined) { supported = false; } }); return supported; } ); /** * This is a cookie handler. * @example * // cookie object * var cookieObj = {"key" : "cookieName", "val" : "cookieVal"} * // cookie string * var cookieString = cookieName=cookieVal; * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130223 * @class This represents a cookie handler * @returns {CookieMonster} A cookie handler. * @requires atropa.data */ atropa.CookieMonster = function CookieMonster() { 'use strict'; var currentCookies, getCookieCallback; /** * This holds the current cookie object array. * @private * @type Array * @fieldOf atropa.CookieMonster- */ currentCookies = []; /** * Converts a cookie string into an object. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130223 * @methodOf atropa.CookieMonster# * @param {String} cookie A cookie represented as a string * <code>cookieName=cookieVal;</code> * @returns {cookieObj} Returns a cookie object. * @example * var cookieMonster = new atropa.CookieMonster(); * var cookieObj = cookieMonster.cookie2obj('atropa=hial atropa!!;'); * console.log(cookieObj); */ this.cookie2obj = function cookie2obj(cookie) { var cookieObj = {}; if (!cookie) { return false; } cookieObj.key = cookie.substr(0, cookie.indexOf("=")).trim(); cookieObj.val = cookie.substr(cookie.indexOf("=") + 1); if(cookieObj.val.substr(-1) === ';') { cookieObj.val = cookieObj.val.substr(0, cookieObj.val.length - 1); } return cookieObj; }; /** * Converts a cookie object to a cookie string. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.CookieMonster# * @param {Object} cookieObj A cookie object * @returns {String} Returns a cookie string. * @example * var cookieMonster = new atropa.CookieMonster(); * // creating a cookie * cookieMonster.setCookie('atropa', 'hial atropa!!'); * console.log(document.cookie); * * // getting a cookie object * var cookieObj = cookieMonster.getCookie('atropa'); * console.log(cookieObj); * * // convert the cookie object to a string * console.log(cookieMonster.bakeCookie(cookieObj)); */ this.bakeCookie = function bakeCookie(cookieObj) { var cookie = '', key, val; key = cookieObj.key; val = cookieObj.val; cookie = key + '=' + val + ';'; return cookie; }; /** * Filter cookies based on user specified callback. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130223 * @methodOf atropa.CookieMonster# * @param {function} callback The callback function will be passed * two arguments. The first is a cookie object from the current * document. The second argument is the value supplied for * <code>args</code> if the callback function returns <code>true</code> * then the cookie object will be included in the return results. * @param {Array} args Arguments to pass to the callback * function. * @returns {Array} An array of cookie objects. * @example * var cookieMonster = new atropa.CookieMonster(); * // creating a few cookies * cookieMonster.setCookie('atropa', 'hial atropa!!'); * cookieMonster.setCookie('katjii', 'munching'); * console.log(document.cookie); * * // filter cookies * function cookieFilter(cookieObj, cookieValue) { * if(cookieObj.val === cookieValue) { * return true; * } else { * return false; * } * } * var cookieObjArray = cookieMonster.inspectCookies( * cookieFilter, 'munching'); * console.log(cookieObjArray); */ this.inspectCookies = function inspectCookies(callback, args) { var testCookie, cookies, jar = []; cookies = this.getCookies(); while (cookies.length > 0) { testCookie = cookies.shift(); if (callback(testCookie, args) === true) { jar.push(testCookie); } } return jar; }; /** * Internal callback function used while getting the current * cookies. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130223 * @private * @methodOf atropa.CookieMonster- * @param {cookieObj} testCookie A cookie object * @param {String} args argument used in comparison function * @returns {Boolean} If cookie key is exactly equal to the argument * then the callback returns true. */ getCookieCallback = function getCookieCallback(testCookie, args) { var out; if (testCookie.key === args) { out = true; } else { out = false; } return out; }; /** * Gets a user requested cookie. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130223 * @methodOf atropa.CookieMonster# * @param {String} whichKey The cookies key (name) * @returns {cookieObj|false} Returns a cookie object if * a cookie with the specified key is found or false if * it is not found. * @example * var cookieMonster = new atropa.CookieMonster(); * // creating a cookie * cookieMonster.setCookie('atropa', 'hial atropa!!'); * console.log(document.cookie); * // get a specific cookie * var cookieObj = cookieMonster.getCookie('atropa'); * console.log(cookieObj.key); * console.log(cookieObj.val); */ this.getCookie = function getCookie(whichKey) { var result = this.inspectCookies(getCookieCallback, whichKey.trim()); return result[0] || false; }; /** * Get all cookies. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.CookieMonster# * @returns {Array} An array whose elements are cookie objects. * @example * var cookieMonster = new atropa.CookieMonster(); * // creating a cookie * cookieMonster.setCookie('atropa', 'hial atropa!!'); * // get all cookie objects in an array * console.log(cookieMonster.getCookies()); */ this.getCookies = function getCookies() { var n, l, cookieArray, cookieObj; currentCookies = []; cookieArray = document.cookie.split(";"); for (n = 0, l = cookieArray.length; n < l; n++) { cookieObj = false; if (cookieArray[n]) { cookieObj = this.cookie2obj(cookieArray[n]); if (cookieObj) { currentCookies.push(cookieObj); } } } return currentCookies; }; /** * Removes a specified cookie by user submitted string. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130223 * @methodOf atropa.CookieMonster# * @param {String} whichKey The cookies key (name) that * will be deleted. * @example * var cookieMonster = new atropa.CookieMonster(); * // creating the cookie to delete * cookieMonster.setCookie('atropa', 'hial atropa!!'); * console.log(document.cookie); * // delete a cookie * cookieMonster.deleteCookie('atropa'); * console.log(document.cookie); */ this.deleteCookie = function deleteCookie(whichKey) { var cookieObj = {}; cookieObj.key = whichKey; cookieObj.val = ';expires=Thu, 2 Aug 2001 20:47:11 UTC'; document.cookie = this.bakeCookie(cookieObj); }; /** * Removes a specified cookie by user submitted cookieObj. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.CookieMonster# * @param {cookieObj} cookieObj A cookie object. * @example * var cookieMonster = new atropa.CookieMonster(); * // creating the cookie to delete * cookieMonster.setCookie('atropa', 'hial atropa!!'); * console.log(document.cookie); * // delete a cookie * cookieMonster.deleteCookieObj( * {key : 'atropa', val : 'does not matter'}); * console.log(document.cookie); */ this.deleteCookieObj = function deleteCookieObj(cookieObj) { this.deleteCookie(cookieObj.key); }; /** * Sets a cookie per user specifications as strings. The cookie * will expire when the browser is closed. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.CookieMonster# * @param {String} whichKey The key (name) of the new cookie * @param {String} setTo The value of the new cookie. * @example * var cookieMonster = new atropa.CookieMonster(); * // set a cookie * cookieMonster.setCookie('atropa', 'hial'); * console.log(document.cookie); */ this.setCookie = function setCookie(whichKey, setTo) { var newCookie = {}; newCookie.key = whichKey; newCookie.val = setTo; document.cookie = this.bakeCookie(newCookie); }; /** * Sets a cookie per user specifications as an object. * The cookie will expire when the browser is closed. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.CookieMonster# * @param {cookieObj} cookieObj A cookie object. * @example * var cookieMonster = new atropa.CookieMonster(); * // set a cookie * cookieMonster.setCookieObj({key : 'atropa', val : 'hial atropa!!'}); * console.log(document.cookie); */ this.setCookieObj = function setCookieObj(cookieObj) { return this.setCookie(cookieObj.key, cookieObj.val); }; atropa.supportCheck('CookieMonster'); }; while(atropa.data.requirements.length > 0) { atropa.data.requirements.pop()(); } module.exports = atropa;