Skip to content
This repository has been archived by the owner on Nov 15, 2017. It is now read-only.

Commit

Permalink
Using data() to pull out original values
Browse files Browse the repository at this point in the history
  • Loading branch information
carhartl committed May 1, 2014
1 parent 120dc4c commit 65a9d24
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 54 deletions.
4 changes: 2 additions & 2 deletions jquery.cookie.js
Expand Up @@ -29,7 +29,7 @@
}

function stringifyCookieValue(value) {
return encode(config.json ? JSON.stringify(value) : String(value));
return encode(($.isArray(value) || $.isPlainObject(value)) ? JSON.stringify(value) : String(value));
}

function parseCookieValue(s) {
Expand All @@ -43,7 +43,7 @@
// If we can't decode the cookie, ignore it, it's unusable.
// If we can't parse the cookie, ignore it, it's unusable.
s = decodeURIComponent(s.replace(pluses, ' '));
return config.json ? JSON.parse(s) : s;
return $('<div>').attr('data-cookie', s).data('cookie');
} catch(e) {}
}

Expand Down
98 changes: 46 additions & 52 deletions test/tests.js
Expand Up @@ -59,52 +59,52 @@ test('raw = true', function () {
strictEqual($.cookie('c'), 'foo=bar', 'should include the entire value');
});

test('json = true', function () {
expect(1);

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

test('not existing with json = true', function () {
expect(1);

if ('JSON' in window) {
$.cookie.json = true;
strictEqual($.cookie('whatever'), undefined, "won't throw exception");
} else {
ok(true);
}
});

test('string with json = true', function () {
expect(1);

if ('JSON' in window) {
$.cookie.json = true;
$.cookie('c', 'v');
strictEqual($.cookie('c'), 'v', 'should return value');
} else {
ok(true);
}
});

test('invalid JSON string with json = true', function () {
expect(1);

if ('JSON' in window) {
$.cookie('c', 'v');
$.cookie.json = true;
strictEqual($.cookie('c'), undefined, "won't throw exception, returns undefined");
} else {
ok(true);
}
});
// test('json = true', function () {
// expect(1);
//
// if ('JSON' in window) {
// $.cookie.json = true;
// $.cookie('c', { foo: 'bar' });
// deepEqual($.cookie('c'), { foo: 'bar' }, 'should parse JSON');
// } else {
// ok(true);
// }
// });
//
// test('not existing with json = true', function () {
// expect(1);
//
// if ('JSON' in window) {
// $.cookie.json = true;
// strictEqual($.cookie('whatever'), undefined, "won't throw exception");
// } else {
// ok(true);
// }
// });
//
// test('string with json = true', function () {
// expect(1);
//
// if ('JSON' in window) {
// $.cookie.json = true;
// $.cookie('c', 'v');
// strictEqual($.cookie('c'), 'v', 'should return value');
// } else {
// ok(true);
// }
// });
//
// test('invalid JSON string with json = true', function () {
// expect(1);
//
// if ('JSON' in window) {
// $.cookie('c', 'v');
// $.cookie.json = true;
// strictEqual($.cookie('c'), undefined, "won't throw exception, returns undefined");
// } else {
// ok(true);
// }
// });

test('invalid URL encoding', function () {
expect(1);
Expand Down Expand Up @@ -180,12 +180,6 @@ test('value "[object Object]"', function () {
strictEqual($.cookie('c'), '[object Object]', 'should write value');
});

test('number', function () {
expect(1);
$.cookie('c', 1234);
strictEqual($.cookie('c'), '1234', 'should write value');
});

test('expires option as days from now', function () {
expect(1);
var sevenDaysFromNow = new Date();
Expand Down

0 comments on commit 65a9d24

Please sign in to comment.