Giter Site home page Giter Site logo

cookiejs's People

Contributors

bjarneo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

mrozbarry

cookiejs's Issues

Some extra features; ability to save cookies as JSON objects, unit tests

Are you okay with my possibly adding some features and unit testing? I ask because this was one of the top main feature-complete and lightweight cookie handling libraries, and it could definitely use some extra love.

I was mainly thinking of being able to save/retrieve data more like JSON, so you could do something like:

  // Backwards compatible
  CookieJS.set({name: 'some.type.of.namespaced.objec', value: 'foobar'});

  // Object notation
  CookieJS.setObject({object: {some:{type:{of:{namespaced:{object:'foobar'}}}}}});

  // Namespace notation
  CookieJS.set({name:['some', 'type', 'of', 'namespaced','object'], value: 'foobar'});

CookieJS.set could probably intelligently detect if the value is an object so the second case could just become:

CookieJS.set({value: {some:{type:{of:{namespaced:{object:'foobar'}}}}}});

Internally, there are two options for storaged:

  1. Use the root object member as the cookie name, save using JSON.stringify()

    CookieJS.setObject = function(params) {
     var keys = params.object.keys(); // Get a list of root keys
     for(var key in keys) {
       CookieJS.set({name: key, value: JSON.stringify(params.object[key])});
     }
    }
  2. Use one cookie per path as a larger JSON object

    // Attempt to use already implemented merge object functions if available, otherwise, roll our own
    var mergeObject = null;
    if (typeof Object.prototype.assign == 'function') {
      mergeObject = Object.prototype.assign;
    }  else if (_ && _.extend) {
      mergeObject = _.extend;
    } else {
      mergeObject = function(object, extension) {
        if (object == {} && extension ) return extension;
        var modifiedObject = object;
        for(var key in extension.keys()) {
          if (typeof extension[key] == 'object' && typeof modifiedObject[key] == 'object') {
            modifiedObject[key] = mergeObject(modifiedObject[key], extension[key]);
          } else {
            modifiedObject[key] = extension[key];
          }
        }
        return modifiedObject;
      };
    }
    
    CookieJS.setObject = function(params) {
      var key = params.path || '/';
      try {
        var obj = JSON.parse(CookieJS.get(key));
      } catch {
        var obj = {};
      }
      CookieJS.set({name: key, value: mergeObject(obj, params.value)});
    }

Also, it may be good if CookieJS.set returns the value it set so it would be possible to chain it with other functions.


For unit tests, I think QUnit may be a good choice here, as we're modifying document.cookies, and I don't trust things like PhantomJS to emulate cookie behaviour properly (not that it wouldn't work, but the real thing is always best). I think, however, using a combination of mocha with QUnit will use PhantomJS anyway, and mocha would be nice to run inside of gulp. Any preferences on this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.