var cookie = require('cookie');

var _cookies = cookie.parse((typeof document !== 'undefined') ? document.cookie : '');

for (var key in _cookies) {
  try {
    _cookies[key] = JSON.parse(_cookies[key]);
  } catch(e) {
    // Not serialized object
  }
}

function load(name) {
  return _cookies[name];
}

function save(name, val, opt) {
  _cookies[name] = val;
  
  // Cookies only work in the browser
  if (typeof document === 'undefined') return;
  
  document.cookie = cookie.serialize(name, val, opt);
}

var reactCookie = {
  load: load,
  save: save
};

if (typeof module !== 'undefined') {
  module.exports = reactCookie
}

if (typeof window !== 'undefined') {
  window['reactCookie'] = reactCookie;
}
var cookie = require('cookie');

var _cookies = cookie.parse((typeof document !== 'undefined') ? document.cookie : '');

for (var key in _cookies) {
  try {
    _cookies[key] = JSON.parse(_cookies[key]);
  } catch(e) {
    // Not serialized object
  }
}

function load(name) {
  return _cookies[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);
}

var reactCookie = {
  load: load,
  save: save
};

if (typeof module !== 'undefined') {
  module.exports = reactCookie
}

if (typeof window !== 'undefined') {
  window['reactCookie'] = reactCookie;
}

Fix cookie not being parsed on load.
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.
