Skip to content

Commit cbe8ed9

Browse files
Write a custom simple extend for this project's use case
1 parent 25ced9c commit cbe8ed9

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

‎src/jquery.cookie.js‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,25 @@
5252
return $.isFunction(converter) ? converter(value) : value;
5353
}
5454

55+
function extend() {
56+
var key, options;
57+
var i = 0;
58+
var result = {};
59+
for (; i < arguments.length; i++) {
60+
options = arguments[ i ];
61+
for (key in options) {
62+
result[key] = options[key];
63+
}
64+
}
65+
return result;
66+
}
67+
5568
var config = $.cookie = function (key, value, options) {
5669

5770
// Write
5871

5972
if (arguments.length > 1 && !$.isFunction(value)) {
60-
options = $.extend({}, config.defaults, options);
73+
options = extend(config.defaults, options);
6174

6275
if (typeof options.expires === 'number') {
6376
var days = options.expires, t = options.expires = new Date();
@@ -107,13 +120,14 @@
107120

108121
$.removeCookie = function (key, options) {
109122
// Must not alter options, thus extending a fresh object...
110-
$.cookie(key, '', $.extend({}, options, { expires: -1 }));
123+
$.cookie(key, '', extend(options, { expires: -1 }));
111124
return !$.cookie(key);
112125
};
113126

114127
$.cookie.get = $.cookie;
115128
$.cookie.set = $.cookie;
116129
$.cookie.remove = $.removeCookie;
130+
$.cookie._extend = extend;
117131

118132
return $.cookie;
119133
}));

‎test/tests.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,12 @@ test('read converter with raw = true', function() {
346346
Cookies.set('c', '1');
347347
strictEqual(Cookies.get('c', Number), 1, 'does not decode, but converts read value');
348348
});
349+
350+
module('merge');
351+
352+
test('passing object reference', function() {
353+
expect(1);
354+
var original = {};
355+
Cookies._extend(original, { key: 'value' });
356+
deepEqual(original, {}, 'should not alter the original object');
357+
});

0 commit comments

Comments
 (0)