Skip to content

Commit 5e38f56

Browse files
committed
refactor: completely rewrite data-saver to use modern libraries + save / update / delete nested complex cookie data in PL as JSON
1 parent 74781fb commit 5e38f56

1 file changed

Lines changed: 38 additions & 130 deletions

File tree

Lines changed: 38 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,74 @@
1-
/*!
2-
* jQuery Cookie Plugin v1.3
3-
* https://github.com/carhartl/jquery-cookie
4-
*
5-
* Copyright 2011, Klaus Hartl
6-
* Dual licensed under the MIT or GPL Version 2 licenses.
7-
* http://www.opensource.org/licenses/mit-license.php
8-
* http://www.opensource.org/licenses/GPL-2.0
9-
*/
10-
import $ from 'jquery';
11-
const jQuery = $;
12-
13-
(function($, document, undefined) {
14-
var pluses = /\+/g;
15-
16-
function raw(s) {
17-
return s;
18-
}
19-
20-
function decoded(s) {
21-
return decodeURIComponent(s.replace(pluses, ' '));
22-
}
23-
24-
var config = ($.cookie = function(key, value, options) {
25-
// write
26-
if (value !== undefined) {
27-
options = $.extend({}, config.defaults, options);
28-
29-
if (value === null) {
30-
options.expires = -1;
31-
}
32-
33-
if (typeof options.expires === 'number') {
34-
var days = options.expires,
35-
t = (options.expires = new Date());
36-
t.setDate(t.getDate() + days);
37-
}
38-
39-
value = config.json ? JSON.stringify(value) : String(value);
40-
41-
return (document.cookie = [
42-
encodeURIComponent(key),
43-
'=',
44-
config.raw ? value : encodeURIComponent(value),
45-
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
46-
options.path ? '; path=' + options.path : '',
47-
options.domain ? '; domain=' + options.domain : '',
48-
options.secure ? '; secure' : '',
49-
].join(''));
50-
}
51-
52-
// read
53-
var decode = config.raw ? raw : decoded;
54-
var cookies = document.cookie.split('; ');
55-
for (var i = 0, l = cookies.length; i < l; i++) {
56-
var parts = cookies[i].split('=');
57-
if (decode(parts.shift()) === key) {
58-
var cookie = decode(parts.join('='));
59-
return config.json ? JSON.parse(cookie) : cookie;
60-
}
61-
}
62-
63-
return null;
64-
});
65-
66-
config.defaults = {};
67-
68-
$.removeCookie = function(key, options) {
69-
if ($.cookie(key) !== null) {
70-
$.cookie(key, null, options);
71-
return true;
72-
}
73-
return false;
74-
};
75-
})(jQuery, document);
76-
771
/*!
782
* Data Saver
79-
*
80-
* Copyright (c) 2013-2014 Dave Olsen, http://dmolsen.com
81-
* Licensed under the MIT license
823
*/
834

5+
import Cookies from 'js-cookie';
6+
import deepmerge from 'deepmerge';
7+
848
export const DataSaver = {
85-
// the name of the cookie to store the data in
86-
cookieName: 'patternlab',
9+
// namespace all cookie names are prefixed with
10+
namespace: 'patternlab',
8711

8812
/**
8913
* Add a given value to the cookie
9014
* @param {String} the name of the key
9115
* @param {String} the value
9216
*/
93-
addValue: function(name, val) {
94-
var cookieVal = $.cookie(this.cookieName);
95-
cookieVal =
96-
cookieVal === null || cookieVal === ''
97-
? name + '~' + val
98-
: cookieVal + '|' + name + '~' + val;
99-
$.cookie(this.cookieName, cookieVal);
17+
addValue(name, val) {
18+
const newData = {};
19+
newData[name] = val;
20+
21+
let existingData = {};
22+
23+
if (Cookies.getJSON(DataSaver.namespace)) {
24+
existingData = Cookies.getJSON(DataSaver.namespace);
25+
}
26+
27+
const mergedData = deepmerge(existingData, newData);
28+
29+
Cookies.set(DataSaver.namespace, mergedData, { expires: 7 });
10030
},
10131

10232
/**
10333
* Update a value found in the cookie. If the key doesn't exist add the value
10434
* @param {String} the name of the key
10535
* @param {String} the value
10636
*/
107-
updateValue: function(name, val) {
108-
if (this.findValue(name)) {
109-
var updateCookieVals = '';
110-
var cookieVals = $.cookie(this.cookieName).split('|');
111-
for (var i = 0; i < cookieVals.length; i++) {
112-
var fieldVals = cookieVals[i].split('~');
113-
if (fieldVals[0] == name) {
114-
fieldVals[1] = val;
115-
}
116-
updateCookieVals +=
117-
i > 0
118-
? '|' + fieldVals[0] + '~' + fieldVals[1]
119-
: fieldVals[0] + '~' + fieldVals[1];
120-
}
121-
$.cookie(this.cookieName, updateCookieVals);
122-
} else {
123-
this.addValue(name, val);
124-
}
37+
updateValue(name, val) {
38+
DataSaver.addValue(name, val);
12539
},
12640

12741
/**
12842
* Remove the given key
12943
* @param {String} the name of the key
13044
*/
131-
removeValue: function(name) {
132-
var updateCookieVals = '';
133-
var cookieVals = $.cookie(this.cookieName).split('|');
134-
var k = 0;
135-
for (var i = 0; i < cookieVals.length; i++) {
136-
var fieldVals = cookieVals[i].split('~');
137-
if (fieldVals[0] != name) {
138-
updateCookieVals +=
139-
k === 0
140-
? fieldVals[0] + '~' + fieldVals[1]
141-
: '|' + fieldVals[0] + '~' + fieldVals[1];
142-
k++;
143-
}
45+
removeValue(name) {
46+
const currentData = Cookies.getJSON(DataSaver.namespace);
47+
const updatedData = delete currentData[name];
48+
49+
if (updatedData.keys()) {
50+
Cookies.set(DataSaver.namespace, updatedData, { expires: 7 });
51+
} else {
52+
Cookies.remove(DataSaver.namespace);
14453
}
145-
$.cookie(this.cookieName, updateCookieVals);
14654
},
14755

14856
/**
14957
* Find the value using the given key
15058
* @param {String} the name of the key
151-
*
15259
* @return {String} the value of the key or false if the value isn't found
15360
*/
154-
findValue: function(name) {
155-
if ($.cookie(this.cookieName)) {
156-
var cookieVals = $.cookie(this.cookieName).split('|');
157-
for (var i = 0; i < cookieVals.length; i++) {
158-
var fieldVals = cookieVals[i].split('~');
159-
if (fieldVals[0] == name) {
160-
return fieldVals[1];
161-
}
61+
findValue(name) {
62+
const existingData = Cookies.getJSON(DataSaver.namespace);
63+
64+
if (existingData !== undefined) {
65+
if (existingData[name] !== undefined) {
66+
return existingData[name];
67+
} else {
68+
return false;
16269
}
70+
} else {
71+
return false;
16372
}
164-
return false;
16573
},
16674
};

0 commit comments

Comments
 (0)