Skip to content

Commit

Permalink
Some formatting fixes and cleanup, adding to changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
carhartl committed Aug 9, 2012
1 parent 808704f commit e39cf51
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.2
---
- Adding `$.removeCookie('foo')` for deleting a cookie, using `$.cookie('foo', null)` is now deprecated.

1.1
---
- Default options.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ Read cookie:
$.cookie('the_cookie', { raw: true }); // => "the_value" not URL decoded
$.cookie('not_existing'); // => null

Delete cookie by calling $.removeCookie:
Delete cookie:

//returns false => No cookie found
//returns true => A cookie was found
$.removeCookie('the_cookie'[, options ]);
// returns false => No cookie found
// returns true => A cookie was found
$.removeCookie('the_cookie'[, options]);

*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.*

Expand Down Expand Up @@ -60,8 +60,6 @@ If true, the cookie transmission requires a secure protocol (https). Default: `f

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

## Changelog

## Development

- Source hosted at [GitHub](https://github.com/carhartl/jquery-cookie)
Expand Down
13 changes: 8 additions & 5 deletions jquery.cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/GPL-2.0
*/
(function($, document, undefined) {
(function ($, document, undefined) {

var pluses = /\+/g;

function raw(s) {
return s;
}

function decoded(s) {
return decodeURIComponent(s.replace(pluses, ' '));
}

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

// key and at least value given, set cookie...
if (value !== undefined && !/Object/.test(Object.prototype.toString.call(value))) {
Expand Down Expand Up @@ -53,17 +55,18 @@
return decode(parts.join('='));
}
}

return null;
};

$.cookie.defaults = {};

$.removeCookie = function(key, options) {
if( $.cookie(key, options) !== null ) {
$.removeCookie = function (key, options) {
if ($.cookie(key, options) !== null) {
$.cookie(key, null, options);
return true;
}
return false;
};

})(jQuery, document);
})(jQuery, document);
21 changes: 11 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,13 @@ test('defaults', 1, function () {

module('delete', before);

test('delete', 1, function () {
test('delete (deprecated)', 1, function () {
document.cookie = 'c=v';
$.cookie('c', null);
equal(document.cookie, '', 'should delete with null as value');
equal(document.cookie, '', 'should delete the cookie');
});


module('removeCookie', before);

test('delete', 1, function() {
Expand All @@ -136,24 +137,24 @@ test('delete', 1, function() {

test('return', 2, function() {
equal($.removeCookie('c'), false, "should return false if a cookie wasn't found");

document.cookie = 'c=v';
equal($.removeCookie('c'), true, "should return true if the cookie was found");
equal($.removeCookie('c'), true, 'should return true if the cookie was found');
});

test('passing options', 2, function() {
var oldCookie = $.cookie;
$.cookie = function( arg0, arg1, arg2 ) {
if( arg1 === null ) {

$.cookie = function(arg0, arg1, arg2) {
if (arg1 === null) {
equal(arg2.test, 'options', 'The options should be passed');
} else {
equal(arg1.test, 'options', 'The options should be passed');
}
};

document.cookie = 'c=v';
$.removeCookie('c', { test: 'options' });

$.cookie = oldCookie;
});
});

0 comments on commit e39cf51

Please sign in to comment.