Skip to content

Commit f81f12e

Browse files
author
Mateusz Krzeszowiak
committed
Reformat storage polyfill for better minification
1 parent cba9d46 commit f81f12e

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

lib/web/mage/polyfill.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
try {
2-
if (!window.localStorage || !window.sessionStorage) {
3-
throw new Error();
4-
}
5-
6-
localStorage.setItem('storage_test', 1);
7-
localStorage.removeItem('storage_test');
8-
} catch (e) {
9-
(function () {
10-
'use strict';
11-
1+
(function (root, doc) {
2+
'use strict';
3+
try {
4+
if (!root.localStorage || !root.sessionStorage) {
5+
throw new Error();
6+
}
7+
8+
localStorage.setItem('storage_test', 1);
9+
localStorage.removeItem('storage_test');
10+
} catch (e) {
1211
/**
1312
* Returns a storage object to shim local or sessionStorage
1413
* @param {String} type - either 'local' or 'session'
@@ -32,7 +31,7 @@ try {
3231
} else {
3332
expires = '';
3433
}
35-
document.cookie = name + '=' + value + expires + '; path=/';
34+
doc.cookie = name + '=' + value + expires + '; path=/';
3635
}
3736

3837
/**
@@ -41,7 +40,7 @@ try {
4140
*/
4241
function readCookie(name) {
4342
var nameEQ = name + '=',
44-
ca = document.cookie.split(';'),
43+
ca = doc.cookie.split(';'),
4544
i = 0,
4645
c;
4746

@@ -70,11 +69,11 @@ try {
7069
return 'localstorage';
7170
}
7271

73-
if (!window.name) {
74-
window.name = new Date().getTime();
72+
if (!root.name) {
73+
root.name = new Date().getTime();
7574
}
7675

77-
return 'sessionStorage' + window.name;
76+
return 'sessionStorage' + root.name;
7877
}
7978

8079
/**
@@ -170,7 +169,7 @@ try {
170169
};
171170
};
172171

173-
window.localStorage.prototype = window.localStorage = new Storage('local');
174-
window.sessionStorage.prototype = window.sessionStorage = new Storage('session');
175-
})();
176-
}
172+
root.localStorage.prototype = root.localStorage = new Storage('local');
173+
root.sessionStorage.prototype = root.sessionStorage = new Storage('session');
174+
}
175+
})(window, document);

0 commit comments

Comments
 (0)