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

Commit

Permalink
Now using tabs for indentation, following jQuery core style guidelines,
Browse files Browse the repository at this point in the history
closes #82.
  • Loading branch information
carhartl committed Jul 9, 2012
1 parent bdf8abb commit a36fbf9
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 130 deletions.
11 changes: 11 additions & 0 deletions .tm_properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
softTabs = false
tabSize = 2

[ text.plain ]
softWrap = true
wrapColumn = "Use Window Frame"
softTabs = true
tabSize = 4

[ "*.md" ]
fileType = "text.plain"
80 changes: 40 additions & 40 deletions jquery.cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,45 @@
* http://www.opensource.org/licenses/GPL-2.0
*/
(function($, document) {
$.cookie = function(key, value, options) {

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

if (value == null) {
options.expires = -1;
}

if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}

value = String(value);

return (document.cookie = [
encodeURIComponent(key), '=', options.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 : '',
options.secure ? '; secure' : ''
].join(''));
}

// key and possibly options given, get cookie...
options = value || $.cookie.defaults || {};
var decode = options.raw ? function(s) { return s; } : decodeURIComponent;

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('='));
}
}
return null;
};

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

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

if (value == null) {
options.expires = -1;
}

if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}

value = String(value);

return (document.cookie = [
encodeURIComponent(key), '=', options.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 : '',
options.secure ? '; secure' : ''
].join(''));
}

// key and possibly options given, get cookie...
options = value || $.cookie.defaults || {};
var decode = options.raw ? function(s) { return s; } : decodeURIComponent;

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('='));
}
}
return null;
};

$.cookie.defaults = {};

})(jQuery, document);
34 changes: 17 additions & 17 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
var http = require('http'),
url = require('url'),
path = require('path'),
fs = require('fs');
var http = require('http');
var url = require('url');
var path = require('path');
var fs = require('fs');

http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname,
filename = path.join(process.cwd(), uri);
var uri = url.parse(request.url).pathname;
var filename = path.join(process.cwd(), uri);

fs.readFile(filename, 'binary', function(err, file) {
if (err) {
response.writeHead(500, { 'Content-Type': 'text/plain' });
response.write(err + '\n');
response.end();
return;
}
fs.readFile(filename, 'binary', function(err, file) {
if (err) {
response.writeHead(500, { 'Content-Type': 'text/plain' });
response.write(err + '\n');
response.end();
return;
}

response.writeHead(200);
response.write(file, 'utf-8');
response.end();
});
response.writeHead(200);
response.write(file, 'utf-8');
response.end();
});
}).listen(8124, '0.0.0.0');

console.log('Test suite at http://0.0.0.0:8124/test.html');
32 changes: 16 additions & 16 deletions test.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jquery.cookie Test Suite</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css">
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script src="http://code.jquery.com/qunit/qunit-git.js"></script>
<script src="jquery.cookie.js"></script>
<script src="test.js"></script>
</head>
<body>
<h1 id="qunit-header">jquery.cookie Test Suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
<head>
<meta charset="utf-8">
<title>jquery.cookie Test Suite</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css">
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script src="http://code.jquery.com/qunit/qunit-git.js"></script>
<script src="jquery.cookie.js"></script>
<script src="test.js"></script>
</head>
<body>
<h1 id="qunit-header">jquery.cookie Test Suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>
114 changes: 57 additions & 57 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,126 +1,126 @@
var before = {
setup: function () {
cookies = document.cookie.split('; ')
for (var i = 0, c; (c = (cookies)[i]) && (c = c.split('=')[0]); i++) {
document.cookie = c + '=; expires=' + new Date(0).toUTCString();
}

$.cookie.defaults = {};
}
setup: function () {
cookies = document.cookie.split('; ')
for (var i = 0, c; (c = (cookies)[i]) && (c = c.split('=')[0]); i++) {
document.cookie = c + '=; expires=' + new Date(0).toUTCString();
}

$.cookie.defaults = {};
}
};


module('read', before);

test('simple value', 1, function () {
document.cookie = 'c=v';
equal($.cookie('c'), 'v', 'should return value');
document.cookie = 'c=v';
equal($.cookie('c'), 'v', 'should return value');
});

test('empty value', 1, function () {
// IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, which
// resulted in a bug while reading such a cookie.
$.cookie('c', '');
equal($.cookie('c'), '', 'should return value');
// IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, which
// resulted in a bug while reading such a cookie.
$.cookie('c', '');
equal($.cookie('c'), '', 'should return value');
});

test('not existing', 1, function () {
equal($.cookie('whatever'), null, 'should return null');
equal($.cookie('whatever'), null, 'should return null');
});

test('decode', 1, function () {
document.cookie = encodeURIComponent(' c') + '=' + encodeURIComponent(' v');
equal($.cookie(' c'), ' v', 'should decode key and value');
document.cookie = encodeURIComponent(' c') + '=' + encodeURIComponent(' v');
equal($.cookie(' c'), ' v', 'should decode key and value');
});

test('raw: true', 1, function () {
document.cookie = 'c=%20v';
equal($.cookie('c', { raw: true }), '%20v', 'should not decode value');
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');
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');
$.cookie('c', 'foo=bar', { raw: true });
equal($.cookie('c', { raw: true }), 'foo=bar', 'should include the entire value');
});

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


module('write', before);

test('String primitive', 1, function () {
$.cookie('c', 'v');
equal($.cookie('c'), 'v', 'should write value');
$.cookie('c', 'v');
equal($.cookie('c'), 'v', 'should write value');
});

test('String object', 1, function () {
$.cookie('c', new String('v'));
equal($.cookie('c'), 'v', 'should write value');
$.cookie('c', new String('v'));
equal($.cookie('c'), 'v', 'should write value');
});

test('value "[object Object]"', 1, function () {
$.cookie('c', '[object Object]');
equal($.cookie('c'), '[object Object]', 'should write value');
$.cookie('c', '[object Object]');
equal($.cookie('c'), '[object Object]', 'should write value');
});

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

test('expires option as days from now', 1, function() {
var sevenDaysFromNow = new Date();
sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7);
equal($.cookie('c', 'v', { expires: 7 }), 'c=v; expires=' + sevenDaysFromNow.toUTCString(),
'should write the cookie string with expires');
var sevenDaysFromNow = new Date();
sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7);
equal($.cookie('c', 'v', { expires: 7 }), 'c=v; expires=' + sevenDaysFromNow.toUTCString(),
'should write the cookie string with expires');
});

test('expires option as Date instance', 1, function() {
var sevenDaysFromNow = new Date();
sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7);
equal($.cookie('c', 'v', { expires: sevenDaysFromNow }), 'c=v; expires=' + sevenDaysFromNow.toUTCString(),
'should write the cookie string with expires');
var sevenDaysFromNow = new Date();
sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7);
equal($.cookie('c', 'v', { expires: sevenDaysFromNow }), 'c=v; expires=' + sevenDaysFromNow.toUTCString(),
'should write the cookie string with expires');
});

test('invalid expires option (in the past)', 1, function() {
var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
$.cookie('c', 'v', { expires: yesterday });
equal($.cookie('c'), null, 'should not save already expired cookie');
var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
$.cookie('c', 'v', { expires: yesterday });
equal($.cookie('c'), null, 'should not save already expired cookie');
});

test('return value', 1, function () {
equal($.cookie('c', 'v'), 'c=v', 'should return written cookie string');
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');
equal($.cookie('c', ' v', { raw: true }).split('=')[1],
' v', 'should not encode');
});

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


module('delete', before);

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

document.cookie = 'c=v';
$.cookie('c', undefined);
equal(document.cookie, '', 'should delete with undefined as value');
document.cookie = 'c=v';
$.cookie('c', undefined);
equal(document.cookie, '', 'should delete with undefined as value');
});

0 comments on commit a36fbf9

Please sign in to comment.