bjarneo / cookiejs Goto Github PK
View Code? Open in Web Editor NEWCookieJS framework. No jQuery needed.
CookieJS framework. No jQuery needed.
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:
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])});
}
}
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.