Skip to content

Commit 6d7b290

Browse files
Merge pull request #27 from js-cookie/byte-saving
Cut more bytes
2 parents e1f7ea2 + 38fb895 commit 6d7b290

5 files changed

Lines changed: 61 additions & 46 deletions

File tree

‎Gruntfile.js‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*jshint node:true, quotmark:single */
1+
/*jshint node:true */
22
'use strict';
33

44
module.exports = function (grunt) {
@@ -24,6 +24,27 @@ module.exports = function (grunt) {
2424
source: 'src/**/*.js',
2525
tests: ['test/**/*.js', '!test/polyfill.js']
2626
},
27+
jscs: {
28+
options: {
29+
requireCommaBeforeLineBreak: true,
30+
requireLineFeedAtFileEnd: true,
31+
requireSemicolons: true,
32+
requireSpaceBeforeKeywords: ['else', 'while', 'catch'],
33+
requireSpaceAfterKeywords: true,
34+
requireSpaceAfterLineComment: true,
35+
requireSpaceBeforeBlockStatements: true,
36+
requireSpaceBeforeObjectValues: true,
37+
validateIndentation: '\t',
38+
validateLineBreaks: 'LF',
39+
validateQuoteMarks: true,
40+
disallowSpacesInsideArrayBrackets: 'all',
41+
disallowSpacesInsideParentheses: true,
42+
disallowTrailingWhitespace: true
43+
},
44+
grunt: 'Gruntfile.js',
45+
source: 'src/**/*.js',
46+
tests: ['test/**/*.js', '!test/polyfill.js']
47+
},
2748
uglify: {
2849
options: {
2950
banner: '/*! <%= pkg.name %> v<%= pkg.version %> | <%= pkg.license %> */\n'

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"grunt-contrib-qunit": "0.7.0",
3636
"grunt-contrib-uglify": "0.9.1",
3737
"grunt-contrib-watch": "0.6.1",
38+
"grunt-jscs": "1.6.0",
3839
"grunt-saucelabs": "8.6.0",
3940
"gzip-js": "0.3.2",
4041
"qunitjs": "1.18.0",

‎src/js.cookie.js‎

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@
1313
} else {
1414
var _OldCookies = window.Cookies;
1515
var api = window.Cookies = factory(window.jQuery);
16-
api.noConflict = function() {
16+
api.noConflict = function () {
1717
window.Cookies = _OldCookies;
1818
return api;
1919
};
2020
}
2121
}(function () {
22-
function decode (value) {
23-
return value.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
24-
}
25-
2622
function extend () {
2723
var i = 0;
2824
var result = {};
@@ -35,29 +31,13 @@
3531
return result;
3632
}
3733

38-
function init(converter) {
39-
var processRead = function (value, name, json) {
40-
if (value.charAt(0) === '"') {
41-
value = value.slice(1, -1);
42-
}
43-
44-
value = converter && converter(value, name) || decode(value);
45-
46-
if (json) {
47-
try {
48-
value = JSON.parse(value);
49-
} catch(e) {}
50-
}
51-
52-
return value;
53-
};
54-
var api = function (key, value, options) {
34+
function init (converter) {
35+
function api (key, value, options) {
5536
var result;
56-
var args = [].slice.call(arguments);
5737

5838
// Write
5939

60-
if (args.length > 1) {
40+
if (arguments.length > 1) {
6141
options = extend(api.defaults, options);
6242

6343
if (typeof options.expires === 'number') {
@@ -68,10 +48,10 @@
6848

6949
try {
7050
result = JSON.stringify(value);
71-
if (/^(?:\{[\w\W]*\}|\[[\w\W]*\])$/.test(result)) {
51+
if (/^[\{\[]/.test(result)) {
7252
value = result;
7353
}
74-
} catch(e) {}
54+
} catch (e) {}
7555

7656
value = encodeURIComponent(String(value));
7757
value = value.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
@@ -99,25 +79,38 @@
9979
// in case there are no cookies at all. Also prevents odd result when
10080
// calling "get()"
10181
var cookies = document.cookie ? document.cookie.split('; ') : [];
82+
var rdecode = /(%[0-9A-Z]{2})+/g;
10283
var i = 0;
10384

10485
for (; i < cookies.length; i++) {
105-
var parts = cookies[i].split('='),
106-
name = decode(parts.shift()),
107-
cookie = parts.join('=');
86+
var parts = cookies[i].split('=');
87+
var name = parts[0].replace(rdecode, decodeURIComponent);
88+
var cookie = parts.slice(1).join('=');
89+
90+
if (cookie.charAt(0) === '"') {
91+
cookie = cookie.slice(1, -1);
92+
}
93+
94+
cookie = converter && converter(cookie, name) || cookie.replace(rdecode, decodeURIComponent);
95+
96+
if (this.json) {
97+
try {
98+
cookie = JSON.parse(cookie);
99+
} catch (e) {}
100+
}
108101

109102
if (key === name) {
110-
result = processRead(cookie, name, this.json);
103+
result = cookie;
111104
break;
112105
}
113106

114107
if (!key) {
115-
result[name] = processRead(cookie, name, this.json);
108+
result[name] = cookie;
116109
}
117110
}
118111

119112
return result;
120-
};
113+
}
121114

122115
api.get = api.set = api;
123116
api.getJSON = function () {

‎test/node.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/*jshint node:true */
22
exports.node = {
3-
should_load_js_cookie: function(test) {
3+
should_load_js_cookie: function (test) {
44
test.expect(3);
55
var Cookies = require('../src/js.cookie');
6-
7-
test.ok( !!Cookies.get, 'should expose get api' );
8-
test.ok( !!Cookies.set, 'should expose set api' );
9-
test.ok( !!Cookies.remove, 'should expose remove api' );
10-
6+
7+
test.ok(!!Cookies.get, 'should expose get api');
8+
test.ok(!!Cookies.set, 'should expose set api');
9+
test.ok(!!Cookies.remove, 'should expose remove api');
10+
1111
test.done();
1212
}
1313
};

‎test/tests.js‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ asyncTest('malformed cookie value in IE', function () {
4848
// Sandbox in an iframe so that we can poke around with document.cookie.
4949
var iframe = document.createElement('iframe');
5050
var addEvent = function (element, eventName, fn) {
51-
var method = "addEventListener";
51+
var method = 'addEventListener';
5252
if (element.attachEvent) {
5353
eventName = 'on' + eventName;
54-
method = "attachEvent";
54+
method = 'attachEvent';
5555
}
5656
element[ method ](eventName, fn);
5757
};
@@ -186,12 +186,12 @@ test('passing options reference', function () {
186186
var options = { path: '/' };
187187
Cookies.set('c', 'v', options);
188188
Cookies.remove('c', options);
189-
deepEqual(options, { path: '/' }, "won't alter options object");
189+
deepEqual(options, { path: '/' }, 'won\'t alter options object');
190190
});
191191

192192
module('converters', lifecycle);
193193

194-
//github.com/carhartl/jquery-cookie/pull/166
194+
// github.com/carhartl/jquery-cookie/pull/166
195195
test('provide a way for decoding characters encoded by the escape function', function () {
196196
expect(1);
197197
document.cookie = 'c=%u5317%u4eac';
@@ -209,7 +209,7 @@ test('should decode a malformed char that matches the decodeURIComponent regex',
209209
test('should be able to conditionally decode a single malformed cookie', function () {
210210
expect(4);
211211
var cookies = Cookies.withConverter(function (value, name) {
212-
if ( name === 'escaped' ) {
212+
if (name === 'escaped') {
213213
return unescape(value);
214214
}
215215
});
@@ -278,7 +278,7 @@ test('Object Constructor', function () {
278278
strictEqual(Cookies.get('c'), '{"k":"v"}', 'should return a String');
279279
});
280280

281-
test('Use String(value) for unsupported objects that do not stringify into JSON', function() {
281+
test('Use String(value) for unsupported objects that do not stringify into JSON', function () {
282282
expect(2);
283283

284284
Cookies.set('date', new Date(2015, 04, 13, 0, 0, 0, 0));
@@ -295,7 +295,7 @@ test('Call to read all cookies with mixed json', function () {
295295

296296
module('noConflict', lifecycle);
297297

298-
test('do not conflict with existent globals', function() {
298+
test('do not conflict with existent globals', function () {
299299
expect(2);
300300
var Cookies = window.Cookies.noConflict();
301301
Cookies.set('c', 'v');

0 commit comments

Comments
 (0)