Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
btremblay committed Mar 14, 2015
0 parents commit a521e33
Show file tree
Hide file tree
Showing 15 changed files with 362 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# WebStorm non-sharable files
.idea/workspace.xml

# Log files
*.log

# NPM modules
node_modules
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/react-cookie.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/scopes/scope_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) <year> <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
90 changes: 90 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# react-cookie
Load and save cookies within your React application

## Download
NPM: `npm install react-cookie`<br />
Bower: `bower install react-cookie`

# Examples

## ES6
```js
import React from 'react';
import cookie from 'react-cookie';

export default class MyApp extends React.component {

constructor(props) {
super(props);
this.state = { userId: cookie.load('userId') };
}

onLogin(userId) {
this.state.userId = userId;
cookie.save('userId', nextState.userId);
}

render() {
return (
<LoginPanel onSuccess={this.onLogin.bind(this)} />
);
}

}
```

## 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', nextState.userId);
},

render: function() {
return (
<LoginPanel onSuccess={this.onLogin} />
);
}

});

module.exports = MyApp;
```

## Usage

### `reactCookie.get(name: string)`
### `reactCookie.save(name: string, val, [opt])`

## opt
Support all the cookie options from the RFC.

### path
> cookie path
### expires
> absolute expiration date for the cookie (Date object)
### maxAge
> relative max age of the cookie from when the client receives it (seconds)
### domain
> domain for the cookie
### secure
> true or false
### httpOnly
> true or false
## License
This project is under the MIT license. You are free to do whatever you want with it.
22 changes: 22 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "react-cookie",
"main": "dist/react-cookie.js",
"version": "0.1.0",
"homepage": "https://github.com/eXon/react-cookie",
"authors": [
"Benoit Tremblay <[email protected]>"
],
"description": "Load and save cookies within your React application",
"keywords": [
"cookie",
"cookies",
"react",
"reactjs",
"jsx"
],
"ignore": [
"**/.*",
"node_modules"
],
"license": "MIT"
}
115 changes: 115 additions & 0 deletions dist/react-cookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
(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 _cookies = cookie.parse((document && document.cookie) ? 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 only work in the browser
if (!document || !document.cookie) return;

_cookies[name] = 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;
}
},{"cookie":2}],2:[function(require,module,exports){

/// Serialize the a name value pair into a cookie string suitable for
/// http headers. An optional options object specified cookie parameters
///
/// serialize('foo', 'bar', { httpOnly: true })
/// => "foo=bar; httpOnly"
///
/// @param {String} name
/// @param {String} val
/// @param {Object} options
/// @return {String}
var serialize = function(name, val, opt){
opt = opt || {};
var enc = opt.encode || encode;
var pairs = [name + '=' + enc(val)];

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) pairs.push('Domain=' + opt.domain);
if (opt.path) 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('; ');
};

/// Parse the given cookie header string into an object
/// The object has the various cookies as keys(names) => values
/// @param {String} str
/// @return {Object}
var parse = function(str, opt) {
opt = opt || {};
var obj = {}
var pairs = str.split(/; */);
var dec = opt.decode || decode;

pairs.forEach(function(pair) {
var eq_idx = pair.indexOf('=')

// skip things that don't look like key=value
if (eq_idx < 0) {
return;
}

var key = pair.substr(0, eq_idx).trim()
var val = pair.substr(++eq_idx, pair.length).trim();

// quoted values
if ('"' == val[0]) {
val = val.slice(1, -1);
}

// only assign once
if (undefined == obj[key]) {
try {
obj[key] = dec(val);
} catch (e) {
obj[key] = val;
}
}
});

return obj;
};

var encode = encodeURIComponent;
var decode = decodeURIComponent;

module.exports.serialize = serialize;
module.exports.parse = parse;

},{}]},{},[1]);
1 change: 1 addition & 0 deletions dist/react-cookie.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var cookie = require('cookie');

var _cookies = cookie.parse((document && document.cookie) ? 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 only work in the browser
if (!document || !document.cookie) return;

_cookies[name] = 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;
}
33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "react-cookie",
"version": "0.1.0",
"description": "Load and save cookies within your React application",
"main": "index.js",
"files": [
"index.js"
],
"repository": {
"type": "git",
"url": "https://github.com/eXon/react-cookie.git"
},
"bugs": "https://github.com/eXon/react-cookie/issues",
"keywords": [
"cookie",
"cookies",
"react",
"reactjs",
"jsx"
],
"author": "Benoit Tremblay <[email protected]>",
"license": "MIT",
"scripts": {
"build": "browserify index.js > dist/react-cookie.js & uglifyjs dist/react-cookie.js -o dist/react-cookie.min.js"
},
"dependencies": {
"cookie": "^0.1.2"
},
"devDependencies": {
"browserify": "^9.0.3",
"uglify-js": "^2.4.17"
}
}

0 comments on commit a521e33

Please sign in to comment.