Skip to content

Commit a36fbf9

Browse files
committed
Now using tabs for indentation, following jQuery core style guidelines, closes carhartl#82.
1 parent bdf8abb commit a36fbf9

File tree

5 files changed

+141
-130
lines changed

5 files changed

+141
-130
lines changed

.tm_properties

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
softTabs = false
2+
tabSize = 2
3+
4+
[ text.plain ]
5+
softWrap = true
6+
wrapColumn = "Use Window Frame"
7+
softTabs = true
8+
tabSize = 4
9+
10+
[ "*.md" ]
11+
fileType = "text.plain"

jquery.cookie.js

+40-40
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,45 @@
99
* http://www.opensource.org/licenses/GPL-2.0
1010
*/
1111
(function($, document) {
12-
$.cookie = function(key, value, options) {
13-
14-
// key and at least value given, set cookie...
15-
if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value == null)) {
16-
options = $.extend({}, $.cookie.defaults, options);
17-
18-
if (value == null) {
19-
options.expires = -1;
20-
}
21-
22-
if (typeof options.expires === 'number') {
23-
var days = options.expires, t = options.expires = new Date();
24-
t.setDate(t.getDate() + days);
25-
}
26-
27-
value = String(value);
28-
29-
return (document.cookie = [
30-
encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
31-
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
32-
options.path ? '; path=' + options.path : '',
33-
options.domain ? '; domain=' + options.domain : '',
34-
options.secure ? '; secure' : ''
35-
].join(''));
36-
}
37-
38-
// key and possibly options given, get cookie...
39-
options = value || $.cookie.defaults || {};
40-
var decode = options.raw ? function(s) { return s; } : decodeURIComponent;
41-
42-
var cookies = document.cookie.split('; ');
43-
for (var i = 0, parts; (parts = cookies[i] && cookies[i].split('=')); i++) {
44-
if (decode(parts.shift()) === key) {
45-
return decode(parts.join('='));
46-
}
47-
}
48-
return null;
49-
};
50-
51-
$.cookie.defaults = {};
12+
$.cookie = function(key, value, options) {
13+
14+
// key and at least value given, set cookie...
15+
if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value == null)) {
16+
options = $.extend({}, $.cookie.defaults, options);
17+
18+
if (value == null) {
19+
options.expires = -1;
20+
}
21+
22+
if (typeof options.expires === 'number') {
23+
var days = options.expires, t = options.expires = new Date();
24+
t.setDate(t.getDate() + days);
25+
}
26+
27+
value = String(value);
28+
29+
return (document.cookie = [
30+
encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
31+
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
32+
options.path ? '; path=' + options.path : '',
33+
options.domain ? '; domain=' + options.domain : '',
34+
options.secure ? '; secure' : ''
35+
].join(''));
36+
}
37+
38+
// key and possibly options given, get cookie...
39+
options = value || $.cookie.defaults || {};
40+
var decode = options.raw ? function(s) { return s; } : decodeURIComponent;
41+
42+
var cookies = document.cookie.split('; ');
43+
for (var i = 0, parts; (parts = cookies[i] && cookies[i].split('=')); i++) {
44+
if (decode(parts.shift()) === key) {
45+
return decode(parts.join('='));
46+
}
47+
}
48+
return null;
49+
};
50+
51+
$.cookie.defaults = {};
5252

5353
})(jQuery, document);

server.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
var http = require('http'),
2-
url = require('url'),
3-
path = require('path'),
4-
fs = require('fs');
1+
var http = require('http');
2+
var url = require('url');
3+
var path = require('path');
4+
var fs = require('fs');
55

66
http.createServer(function(request, response) {
7-
var uri = url.parse(request.url).pathname,
8-
filename = path.join(process.cwd(), uri);
7+
var uri = url.parse(request.url).pathname;
8+
var filename = path.join(process.cwd(), uri);
99

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

18-
response.writeHead(200);
19-
response.write(file, 'utf-8');
20-
response.end();
21-
});
18+
response.writeHead(200);
19+
response.write(file, 'utf-8');
20+
response.end();
21+
});
2222
}).listen(8124, '0.0.0.0');
2323

2424
console.log('Test suite at http://0.0.0.0:8124/test.html');

test.html

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<!doctype html>
22
<html>
3-
<head>
4-
<meta charset="utf-8">
5-
<title>jquery.cookie Test Suite</title>
6-
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css">
7-
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
8-
<script src="http://code.jquery.com/qunit/qunit-git.js"></script>
9-
<script src="jquery.cookie.js"></script>
10-
<script src="test.js"></script>
11-
</head>
12-
<body>
13-
<h1 id="qunit-header">jquery.cookie Test Suite</h1>
14-
<h2 id="qunit-banner"></h2>
15-
<div id="qunit-testrunner-toolbar"></div>
16-
<h2 id="qunit-userAgent"></h2>
17-
<ol id="qunit-tests"></ol>
18-
</body>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>jquery.cookie Test Suite</title>
6+
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css">
7+
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
8+
<script src="http://code.jquery.com/qunit/qunit-git.js"></script>
9+
<script src="jquery.cookie.js"></script>
10+
<script src="test.js"></script>
11+
</head>
12+
<body>
13+
<h1 id="qunit-header">jquery.cookie Test Suite</h1>
14+
<h2 id="qunit-banner"></h2>
15+
<div id="qunit-testrunner-toolbar"></div>
16+
<h2 id="qunit-userAgent"></h2>
17+
<ol id="qunit-tests"></ol>
18+
</body>
1919
</html>

test.js

+57-57
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,126 @@
11
var before = {
2-
setup: function () {
3-
cookies = document.cookie.split('; ')
4-
for (var i = 0, c; (c = (cookies)[i]) && (c = c.split('=')[0]); i++) {
5-
document.cookie = c + '=; expires=' + new Date(0).toUTCString();
6-
}
7-
8-
$.cookie.defaults = {};
9-
}
2+
setup: function () {
3+
cookies = document.cookie.split('; ')
4+
for (var i = 0, c; (c = (cookies)[i]) && (c = c.split('=')[0]); i++) {
5+
document.cookie = c + '=; expires=' + new Date(0).toUTCString();
6+
}
7+
8+
$.cookie.defaults = {};
9+
}
1010
};
1111

1212

1313
module('read', before);
1414

1515
test('simple value', 1, function () {
16-
document.cookie = 'c=v';
17-
equal($.cookie('c'), 'v', 'should return value');
16+
document.cookie = 'c=v';
17+
equal($.cookie('c'), 'v', 'should return value');
1818
});
1919

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

2727
test('not existing', 1, function () {
28-
equal($.cookie('whatever'), null, 'should return null');
28+
equal($.cookie('whatever'), null, 'should return null');
2929
});
3030

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

3636
test('raw: true', 1, function () {
37-
document.cookie = 'c=%20v';
38-
equal($.cookie('c', { raw: true }), '%20v', 'should not decode value');
37+
document.cookie = 'c=%20v';
38+
equal($.cookie('c', { raw: true }), '%20v', 'should not decode value');
3939
});
4040

4141
test('[] used in name', 1, function () {
42-
document.cookie = 'c[999]=foo';
43-
equal($.cookie('c[999]'), 'foo', 'should return value');
42+
document.cookie = 'c[999]=foo';
43+
equal($.cookie('c[999]'), 'foo', 'should return value');
4444
});
4545

4646
test('embedded equals', 1, function () {
47-
$.cookie('c', 'foo=bar', { raw: true });
48-
equal($.cookie('c', { raw: true }), 'foo=bar', 'should include the entire value');
47+
$.cookie('c', 'foo=bar', { raw: true });
48+
equal($.cookie('c', { raw: true }), 'foo=bar', 'should include the entire value');
4949
});
5050

5151
test('defaults', 1, function () {
52-
document.cookie = 'c=%20v';
53-
$.cookie.defaults.raw = true;
54-
equal($.cookie('c'), '%20v', 'should use raw from defaults');
52+
document.cookie = 'c=%20v';
53+
$.cookie.defaults.raw = true;
54+
equal($.cookie('c'), '%20v', 'should use raw from defaults');
5555
});
5656

5757

5858
module('write', before);
5959

6060
test('String primitive', 1, function () {
61-
$.cookie('c', 'v');
62-
equal($.cookie('c'), 'v', 'should write value');
61+
$.cookie('c', 'v');
62+
equal($.cookie('c'), 'v', 'should write value');
6363
});
6464

6565
test('String object', 1, function () {
66-
$.cookie('c', new String('v'));
67-
equal($.cookie('c'), 'v', 'should write value');
66+
$.cookie('c', new String('v'));
67+
equal($.cookie('c'), 'v', 'should write value');
6868
});
6969

7070
test('value "[object Object]"', 1, function () {
71-
$.cookie('c', '[object Object]');
72-
equal($.cookie('c'), '[object Object]', 'should write value');
71+
$.cookie('c', '[object Object]');
72+
equal($.cookie('c'), '[object Object]', 'should write value');
7373
});
7474

7575
test('number', 1, function () {
76-
$.cookie('c', 1234);
77-
equal($.cookie('c'), '1234', 'should write value');
76+
$.cookie('c', 1234);
77+
equal($.cookie('c'), '1234', 'should write value');
7878
});
7979

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

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

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

101101
test('return value', 1, function () {
102-
equal($.cookie('c', 'v'), 'c=v', 'should return written cookie string');
102+
equal($.cookie('c', 'v'), 'c=v', 'should return written cookie string');
103103
});
104104

105105
test('raw option set to true', 1, function () {
106-
equal($.cookie('c', ' v', { raw: true }).split('=')[1],
107-
' v', 'should not encode');
106+
equal($.cookie('c', ' v', { raw: true }).split('=')[1],
107+
' v', 'should not encode');
108108
});
109109

110110
test('defaults', 1, function () {
111-
$.cookie.defaults.raw = true;
112-
equal($.cookie('c', ' v').split('=')[1], ' v', 'should use raw from defaults');
111+
$.cookie.defaults.raw = true;
112+
equal($.cookie('c', ' v').split('=')[1], ' v', 'should use raw from defaults');
113113
});
114114

115115

116116
module('delete', before);
117117

118118
test('delete', 2, function () {
119-
document.cookie = 'c=v';
120-
$.cookie('c', null);
121-
equal(document.cookie, '', 'should delete with null as value');
119+
document.cookie = 'c=v';
120+
$.cookie('c', null);
121+
equal(document.cookie, '', 'should delete with null as value');
122122

123-
document.cookie = 'c=v';
124-
$.cookie('c', undefined);
125-
equal(document.cookie, '', 'should delete with undefined as value');
123+
document.cookie = 'c=v';
124+
$.cookie('c', undefined);
125+
equal(document.cookie, '', 'should delete with undefined as value');
126126
});

0 commit comments

Comments
 (0)