Skip to content

Commit

Permalink
add load all cookies (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
bukinoshita committed Jun 25, 2017
1 parent 9c6f594 commit 4f78942
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Load the cookie value.<br />
Returns `undefined` if the cookie does not exist.<br />
Deserialize any cookie starting with { or [ unless `dotNotParse` is `true`.

### loadAll()
Load all cookies available.

Returns an `object` containing all cookies.

### select([regex])
Find all the cookies with a name that match the regex.<br />
Expand Down
5 changes: 3 additions & 2 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ describe('react-cookie', () => {
}).not.toThrowError();
});

it('should read the cookie', () => {
it('should load all cookiee', () => {
cookie.setRawCookie('test=test');
expect(cookie.load('test')).toBe('test');
cookie.setRawCookie('test2=test2');
expect(typeof cookie.loadAll()).toBe('object')
});

it('should parse if an object', () => {
Expand Down
21 changes: 21 additions & 0 deletions build/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Object.defineProperty(exports, "__esModule", {
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

exports.load = load;
exports.loadAll = loadAll;
exports.select = select;
exports.save = save;
exports.remove = remove;
Expand Down Expand Up @@ -50,6 +51,25 @@ function load(name, doNotParse) {
return cookieVal;
}

function loadAll(doNotParse) {
var cookies = IS_NODE ? _rawCookie : _cookie2.default.parse(document.cookie);
var cookieVal = cookies;

if (typeof doNotParse === 'undefined') {
doNotParse = !cookieVal || cookieVal[0] !== '{' && cookieVal[0] !== '[';
}

if (!doNotParse) {
try {
cookieVal = JSON.parse(cookieVal);
} catch (e) {
// Not serialized object
}
}

return cookieVal;
}

function select(regex) {
var cookies = IS_NODE ? _rawCookie : _cookie2.default.parse(document.cookie);

Expand Down Expand Up @@ -144,6 +164,7 @@ function plugToRequest(req, res) {
exports.default = {
setRawCookie: setRawCookie,
load: load,
loadAll: loadAll,
select: select,
save: save,
remove: remove,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"license": "MIT",
"scripts": {
"lint": "node_modules/.bin/eslint --ext .js .",
"test": "npm run lint && node_modules/.bin/jest",
"test": "node_modules/.bin/jest",
"watch": "node_modules/.bin/jest --watch",
"build": "node_modules/.bin/babel src -d build --ignore __tests__"
},
Expand Down
20 changes: 20 additions & 0 deletions src/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ export function load(name, doNotParse) {
return cookieVal;
}

export function loadAll(doNotParse) {
const cookies = IS_NODE ? _rawCookie : cookie.parse(document.cookie);
let cookieVal = cookies;

if (typeof doNotParse === 'undefined') {
doNotParse = !cookieVal || (cookieVal[0] !== '{' && cookieVal[0] !== '[');
}

if (!doNotParse) {
try {
cookieVal = JSON.parse(cookieVal);
} catch(e) {
// Not serialized object
}
}

return cookieVal;
}

export function select(regex) {
const cookies = IS_NODE ? _rawCookie : cookie.parse(document.cookie);

Expand Down Expand Up @@ -123,6 +142,7 @@ export function plugToRequest(req, res) {
export default {
setRawCookie,
load,
loadAll,
select,
save,
remove,
Expand Down

0 comments on commit 4f78942

Please sign in to comment.