cookie-dough
Advanced tools
Comparing version 0.0.0 to 0.0.1
@@ -11,3 +11,3 @@ var gulp = require('gulp'), | ||
return gulp.src(['./src/*.js']) | ||
return gulp.src(['index.js','test/test.js']) | ||
.pipe(browserified) | ||
@@ -14,0 +14,0 @@ .pipe(gulp.dest('./public')); |
54
index.js
var cookie = require('cookie'), | ||
isServer = !!(typeof window === 'undefined'), | ||
parsedCookies; | ||
isServer = !!(typeof window === 'undefined'); | ||
function get(key) { | ||
return parsedCookies[key]; | ||
} | ||
function CookieDough(req) { | ||
var parsedCookies; | ||
function set(name, value, options) { | ||
parsedCookies[name] = value; | ||
if (isServer) { | ||
var res = options && options.res; | ||
if (!res) { | ||
throw new Error('Must specify `res` when setting cookie.'); | ||
if (!req) { | ||
throw new Error('Must specify `req` for cookie-dough'); | ||
} | ||
res.cookie(name, value, options); | ||
} else { | ||
document.cookie = cookie.serialize(name, value, options); | ||
} | ||
} | ||
module.exports = function (req) { | ||
if (isServer) { | ||
parsedCookies = req.cookies; | ||
@@ -32,9 +17,28 @@ } else { | ||
return { | ||
get: get, | ||
set: set, | ||
remove: function (key) { | ||
set(key, '', { expires: new Date(0) }) | ||
this.get = function get(name) { | ||
return parsedCookies[name]; | ||
}; | ||
this.set = function set(name, value, options) { | ||
parsedCookies[name] = value; | ||
if (isServer) { | ||
var res = options && options.res; | ||
if (!res) { | ||
throw new Error('Must specify `res` when setting cookie'); | ||
} | ||
res.cookie(name, value, options); | ||
} else { | ||
document.cookie = cookie.serialize(name, value, options); | ||
} | ||
}; | ||
this.remove = function remove(name) { | ||
this.set(key, '', { expires: new Date(0) }); | ||
delete parsedCookies[name]; | ||
}; | ||
} | ||
module.exports = CookieDough; |
{ | ||
"name": "cookie-dough", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "An isomorphic JavaScript cookie library", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
5308
46