Skip to content

Commit

Permalink
Adding raw and json configuration (replacing former option), closes c…
Browse files Browse the repository at this point in the history
  • Loading branch information
carhartl committed Aug 29, 2012
1 parent 3e59f61 commit 796abd6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 30 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,10 @@
1.3 (wip)
---
- Configuration options: `raw`, `json`. Replaces raw option becomes config:
`$.cookie.raw = true` // bypass encoding/decoding the cookie value
`$.cookie.json = true` // automatically JSON stringify/parse value
Thus the default options now cleanly contain cookie attributes only.

1.2
---
- Adding `$.removeCookie('foo')` for deleting a cookie, using `$.cookie('foo', null)` is now deprecated.
Expand Down
16 changes: 11 additions & 5 deletions README.md
Expand Up @@ -36,7 +36,17 @@ Delete cookie:

*Note: when deleting a cookie, you must pass the exact same path, domain and secure options that were used to set the cookie, unless you're relying on the default options that is.*

## Options
## Configuration

raw: true

By default the cookie value is encoded/decoded when creating/reading, using `encodeURIComponent`/`decodeURIComponent`. Turn off by setting `raw: true`. Default: `false`.

json: true

Automatically store JSON objects passed as the cookie value. Assumes `JSON.stringify`and `JSON.parse`.

## Cookie Options

Options can be set globally by setting properties of the `$.cookie.defaults` object or individually for each call to `$.cookie()` by passing a plain object to the options argument. Per-call options override the ones set by `$.cookie.defaults`.

Expand All @@ -56,10 +66,6 @@ Define the domain where the cookie is valid. Default: domain of page where the c

If true, the cookie transmission requires a secure protocol (https). Default: `false`.

raw: true

By default the cookie value is encoded/decoded when creating/reading, using `encodeURIComponent`/`decodeURIComponent`. Turn off by setting `raw: true`. Default: `false`.

## Development

- Source hosted at [GitHub](https://github.com/carhartl/jquery-cookie)
Expand Down
14 changes: 7 additions & 7 deletions jquery.cookie.js
Expand Up @@ -20,10 +20,10 @@
return decodeURIComponent(s.replace(pluses, ' '));
}

$.cookie = function (key, value, options) {
var config = $.cookie = function (key, value, options) {

// key and at least value given, set cookie...
if (value !== undefined && !/Object/.test(Object.prototype.toString.call(value))) {
if (value !== undefined) {
options = $.extend({}, $.cookie.defaults, options);

if (value === null) {
Expand All @@ -35,10 +35,10 @@
t.setDate(t.getDate() + days);
}

value = String(value);
value = config.json ? JSON.stringify(value) : String(value);

return (document.cookie = [
encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
encodeURIComponent(key), '=', config.raw ? value : encodeURIComponent(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
Expand All @@ -47,12 +47,12 @@
}

// key and possibly options given, get cookie...
options = value || $.cookie.defaults || {};
var decode = options.raw ? raw : decoded;
var decode = config.raw ? raw : decoded;
var cookies = document.cookie.split('; ');
for (var i = 0, parts; (parts = cookies[i] && cookies[i].split('=')); i++) {
if (decode(parts.shift()) === key) {
return decode(parts.join('='));
var cookie = decode(parts.join('='));
return config.json ? JSON.parse(cookie) : cookie;
}
}

Expand Down
55 changes: 37 additions & 18 deletions test.js
Expand Up @@ -6,6 +6,8 @@ var before = {
}

$.cookie.defaults = {};
delete $.cookie.raw;
delete $.cookie.json;
}
};

Expand Down Expand Up @@ -38,25 +40,31 @@ test('decode pluses to space for server side written cookie', 1, function () {
equal($.cookie('c'), 'foo bar', 'should convert pluses back to space');
});

test('raw: true', 1, function () {
document.cookie = 'c=%20v';
equal($.cookie('c', { raw: true }), '%20v', 'should not decode value');
});

test('[] used in name', 1, function () {
document.cookie = 'c[999]=foo';
equal($.cookie('c[999]'), 'foo', 'should return value');
});

test('embedded equals', 1, function () {
$.cookie('c', 'foo=bar', { raw: true });
equal($.cookie('c', { raw: true }), 'foo=bar', 'should include the entire value');
});
test('raw: true', 2, function () {
$.cookie.raw = true;

test('defaults', 1, function () {
document.cookie = 'c=%20v';
$.cookie.defaults.raw = true;
equal($.cookie('c'), '%20v', 'should use raw from defaults');
equal($.cookie('c'), '%20v', 'should not decode value');

// see https://github.com/carhartl/jquery-cookie/issues/50
$.cookie('c', 'foo=bar');
equal($.cookie('c'), 'foo=bar', 'should include the entire value');
});

test('json: true', 1, function () {
$.cookie.json = true;

if ('JSON' in window) {
document.cookie = 'c=' + JSON.stringify({ foo: 'bar' });
deepEqual($.cookie('c'), { foo: 'bar'}, 'should parse JSON');
} else {
ok(true);
}
});


Expand Down Expand Up @@ -107,16 +115,27 @@ test('return value', 1, function () {
equal($.cookie('c', 'v'), 'c=v', 'should return written cookie string');
});

test('raw option set to true', 1, function () {
equal($.cookie('c', ' v', { raw: true }).split('=')[1],
' v', 'should not encode');
test('defaults', 2, function () {
$.cookie.defaults.path = '/';
ok($.cookie('c', 'v').match(/path=\//), 'should use options from defaults');
ok($.cookie('c', 'v', { path: '/foo' }).match(/path=\/foo/), 'options argument has precedence');
});

test('defaults', 1, function () {
$.cookie.defaults.raw = true;
equal($.cookie('c', ' v').split('=')[1], ' v', 'should use raw from defaults');
test('raw: true', 1, function () {
$.cookie.raw = true;
equal($.cookie('c', ' v').split('=')[1], ' v', 'should not encode');
});

test('json: true', 1, function () {
$.cookie.json = true;

if ('JSON' in window) {
$.cookie('c', { foo: 'bar' });
equal(document.cookie, 'c=' + encodeURIComponent(JSON.stringify({ foo: 'bar' })), 'should stringify JSON');
} else {
ok(true);
}
});

module('delete', before);

Expand Down

0 comments on commit 796abd6

Please sign in to comment.