From 877e9a521fe131978cd63b0af669c01746fe1d8c Mon Sep 17 00:00:00 2001 From: pixeleet Date: Sun, 19 Apr 2015 16:39:55 +0200 Subject: [PATCH] Fix cookie not being parsed on load. If you have to stringify your object before saving, and you add that, to the `_cookies` cache, then you'll return that on `load` after setting it with `save`. While if the value is already set, the script correctly parses the cookie values and what you load is returned as an object. This oneliner takes care of that for you. --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index df7745f..cbd51ac 100644 --- a/index.js +++ b/index.js @@ -16,10 +16,13 @@ function load(name) { function save(name, val, opt) { _cookies[name] = val; - + // Cookies only work in the browser if (typeof document === 'undefined') return; + // allow you to work with cookies as objects. + if (typeof val === 'object') val = JSON.stringify(val); + document.cookie = cookie.serialize(name, val, opt); } @@ -34,4 +37,4 @@ if (typeof module !== 'undefined') { if (typeof window !== 'undefined') { window['reactCookie'] = reactCookie; -} \ No newline at end of file +}