react-cookie
Advanced tools
Comparing version 0.4.2 to 0.4.3
@@ -8,10 +8,5 @@ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
function load(name, doNotParse) { | ||
var cookies = {}; | ||
var cookies = (typeof document === 'undefined') ? _rawCookie : cookie.parse(document.cookie); | ||
var cookieVal = cookies && cookies[name]; | ||
if (typeof document !== 'undefined') { | ||
cookies = cookie.parse(document.cookie); | ||
} | ||
var cookieVal = (cookies && cookies[name]) || _rawCookie[name]; | ||
if (!doNotParse) { | ||
@@ -18,0 +13,0 @@ try { |
@@ -1,1 +0,1 @@ | ||
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){var cookie=require("cookie");var _rawCookie={};var _res=undefined;function load(name,doNotParse){var cookies={};if(typeof document!=="undefined"){cookies=cookie.parse(document.cookie)}var cookieVal=cookies&&cookies[name]||_rawCookie[name];if(!doNotParse){try{cookieVal=JSON.parse(cookieVal)}catch(e){}}return cookieVal}function save(name,val,opt){_rawCookie[name]=val;if(typeof val==="object"){_rawCookie[name]=JSON.stringify(val)}if(typeof document!=="undefined"){document.cookie=cookie.serialize(name,_rawCookie[name],opt)}if(_res&&_res.cookie){_res.cookie(name,val,opt)}}function remove(name,opt){delete _rawCookie[name];if(typeof opt==="undefined"){opt={}}else if(typeof opt==="string"){opt={path:opt}}if(typeof document!=="undefined"){opt.expires=new Date(1970,1,1,0,0,1);document.cookie=cookie.serialize(name,"",opt)}if(_res&&_res.clearCookie){_res.clearCookie(name,opt)}}function setRawCookie(rawCookie){if(rawCookie){_rawCookie=cookie.parse(rawCookie)}else{_rawCookie={}}}function plugToRequest(req,res){if(req.cookie){_rawCookie=req.cookie}else if(req.headers&&req.headers.cookie){setRawCookie(req.headers.cookie)}else{_rawCookie={}}_res=res}var reactCookie={load:load,save:save,remove:remove,setRawCookie:setRawCookie,plugToRequest:plugToRequest};if(typeof window!=="undefined"){window["reactCookie"]=reactCookie}module.exports=reactCookie},{cookie:2}],2:[function(require,module,exports){exports.parse=parse;exports.serialize=serialize;var decode=decodeURIComponent;var encode=encodeURIComponent;var fieldContentRegExp=/^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;function parse(str,options){if(typeof str!=="string"){throw new TypeError("argument str must be a string")}var obj={};var opt=options||{};var pairs=str.split(/; */);var dec=opt.decode||decode;pairs.forEach(function(pair){var eq_idx=pair.indexOf("=");if(eq_idx<0){return}var key=pair.substr(0,eq_idx).trim();var val=pair.substr(++eq_idx,pair.length).trim();if('"'==val[0]){val=val.slice(1,-1)}if(undefined==obj[key]){obj[key]=tryDecode(val,dec)}});return obj}function serialize(name,val,options){var opt=options||{};var enc=opt.encode||encode;if(!fieldContentRegExp.test(name)){throw new TypeError("argument name is invalid")}var value=enc(val);if(value&&!fieldContentRegExp.test(value)){throw new TypeError("argument val is invalid")}var pairs=[name+"="+value];if(null!=opt.maxAge){var maxAge=opt.maxAge-0;if(isNaN(maxAge))throw new Error("maxAge should be a Number");pairs.push("Max-Age="+maxAge)}if(opt.domain){if(!fieldContentRegExp.test(opt.domain)){throw new TypeError("option domain is invalid")}pairs.push("Domain="+opt.domain)}if(opt.path){if(!fieldContentRegExp.test(opt.path)){throw new TypeError("option path is invalid")}pairs.push("Path="+opt.path)}if(opt.expires)pairs.push("Expires="+opt.expires.toUTCString());if(opt.httpOnly)pairs.push("HttpOnly");if(opt.secure)pairs.push("Secure");return pairs.join("; ")}function tryDecode(str,decode){try{return decode(str)}catch(e){return str}}},{}]},{},[1]); | ||
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){var cookie=require("cookie");var _rawCookie={};var _res=undefined;function load(name,doNotParse){var cookies=typeof document==="undefined"?_rawCookie:cookie.parse(document.cookie);var cookieVal=cookies&&cookies[name];if(!doNotParse){try{cookieVal=JSON.parse(cookieVal)}catch(e){}}return cookieVal}function save(name,val,opt){_rawCookie[name]=val;if(typeof val==="object"){_rawCookie[name]=JSON.stringify(val)}if(typeof document!=="undefined"){document.cookie=cookie.serialize(name,_rawCookie[name],opt)}if(_res&&_res.cookie){_res.cookie(name,val,opt)}}function remove(name,opt){delete _rawCookie[name];if(typeof opt==="undefined"){opt={}}else if(typeof opt==="string"){opt={path:opt}}if(typeof document!=="undefined"){opt.expires=new Date(1970,1,1,0,0,1);document.cookie=cookie.serialize(name,"",opt)}if(_res&&_res.clearCookie){_res.clearCookie(name,opt)}}function setRawCookie(rawCookie){if(rawCookie){_rawCookie=cookie.parse(rawCookie)}else{_rawCookie={}}}function plugToRequest(req,res){if(req.cookie){_rawCookie=req.cookie}else if(req.headers&&req.headers.cookie){setRawCookie(req.headers.cookie)}else{_rawCookie={}}_res=res}var reactCookie={load:load,save:save,remove:remove,setRawCookie:setRawCookie,plugToRequest:plugToRequest};if(typeof window!=="undefined"){window["reactCookie"]=reactCookie}module.exports=reactCookie},{cookie:2}],2:[function(require,module,exports){exports.parse=parse;exports.serialize=serialize;var decode=decodeURIComponent;var encode=encodeURIComponent;var fieldContentRegExp=/^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;function parse(str,options){if(typeof str!=="string"){throw new TypeError("argument str must be a string")}var obj={};var opt=options||{};var pairs=str.split(/; */);var dec=opt.decode||decode;pairs.forEach(function(pair){var eq_idx=pair.indexOf("=");if(eq_idx<0){return}var key=pair.substr(0,eq_idx).trim();var val=pair.substr(++eq_idx,pair.length).trim();if('"'==val[0]){val=val.slice(1,-1)}if(undefined==obj[key]){obj[key]=tryDecode(val,dec)}});return obj}function serialize(name,val,options){var opt=options||{};var enc=opt.encode||encode;if(!fieldContentRegExp.test(name)){throw new TypeError("argument name is invalid")}var value=enc(val);if(value&&!fieldContentRegExp.test(value)){throw new TypeError("argument val is invalid")}var pairs=[name+"="+value];if(null!=opt.maxAge){var maxAge=opt.maxAge-0;if(isNaN(maxAge))throw new Error("maxAge should be a Number");pairs.push("Max-Age="+maxAge)}if(opt.domain){if(!fieldContentRegExp.test(opt.domain)){throw new TypeError("option domain is invalid")}pairs.push("Domain="+opt.domain)}if(opt.path){if(!fieldContentRegExp.test(opt.path)){throw new TypeError("option path is invalid")}pairs.push("Path="+opt.path)}if(opt.expires)pairs.push("Expires="+opt.expires.toUTCString());if(opt.httpOnly)pairs.push("HttpOnly");if(opt.secure)pairs.push("Secure");return pairs.join("; ")}function tryDecode(str,decode){try{return decode(str)}catch(e){return str}}},{}]},{},[1]); |
@@ -7,10 +7,5 @@ var cookie = require('cookie'); | ||
function load(name, doNotParse) { | ||
var cookies = {}; | ||
var cookies = (typeof document === 'undefined') ? _rawCookie : cookie.parse(document.cookie); | ||
var cookieVal = cookies && cookies[name]; | ||
if (typeof document !== 'undefined') { | ||
cookies = cookie.parse(document.cookie); | ||
} | ||
var cookieVal = (cookies && cookies[name]) || _rawCookie[name]; | ||
if (!doNotParse) { | ||
@@ -17,0 +12,0 @@ try { |
{ | ||
"name": "react-cookie", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"description": "Load and save cookies within your React application", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -13,13 +13,11 @@ # react-cookie | ||
Bower: `bower install react-cookie`<br /> | ||
CDN: `https://cdnjs.cloudflare.com/ajax/libs/react-cookie/0.3.4/react-cookie.min.js` | ||
CDN: `https://cdnjs.cloudflare.com/ajax/libs/react-cookie/0.4.3/react-cookie.min.js` | ||
# Examples | ||
## ES6 | ||
```js | ||
import React from 'react'; | ||
import { Component } from 'react'; | ||
import cookie from 'react-cookie'; | ||
export default class MyApp extends React.Component { | ||
export default class MyApp extends Component { | ||
constructor(props) { | ||
@@ -31,3 +29,3 @@ super(props); | ||
onLogin(userId) { | ||
this.state.userId = userId; | ||
this.setState({ userId }); | ||
cookie.save('userId', userId); | ||
@@ -45,37 +43,5 @@ } | ||
} | ||
} | ||
``` | ||
## ES5 | ||
```js | ||
var React = require('react'); | ||
var cookie = require('react-cookie'); | ||
var MyApp = React.createClass({ | ||
getInitialState: function() { | ||
return { userId: cookie.load('userId') }; | ||
}, | ||
onLogin: function(userId) { | ||
this.state.userId = userId; | ||
cookie.save('userId', userId); | ||
}, | ||
onLogout: function() { | ||
cookie.remove('userId'); | ||
}, | ||
render: function() { | ||
return ( | ||
<LoginPanel onSuccess={this.onLogin} /> | ||
); | ||
} | ||
}); | ||
module.exports = MyApp; | ||
``` | ||
## Without CommonJS | ||
@@ -82,0 +48,0 @@ You can use react-cookie with anything by using the global variable `reactCookie`. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14958
276
80