diff --git a/CHANGELOG.md b/CHANGELOG.md index dc39c90..c138ee2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 1.0.1 + +### Fixes + +1. Fixed [#1](https://github.com/jherax/proxy-storage/issues/1): Storages have no existing elements when they are instantiated the first time + # 1.0.0 ### Features diff --git a/README.md b/README.md index 5fc6895..12eba11 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,7 @@ const data = { platform: 'Linux x86_64', }; const options = { - path: '/profile', + path: '/jherax', expires: { hours: 6 } }; diff --git a/dist/proxy-storage.js b/dist/proxy-storage.js index a11c9d6..84096f8 100644 --- a/dist/proxy-storage.js +++ b/dist/proxy-storage.js @@ -60,6 +60,8 @@ return /******/ (function(modules) { // webpackBootstrap value: true }); + var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; + var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } @@ -85,6 +87,25 @@ return /******/ (function(modules) { // webpackBootstrap // If you want to support all ES6 features, uncomment the next line // import 'babel-polyfill'; + /** + * @private + * + * Proxy for the default cookie storage associated with the current document. + * + * @Reference + * https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie + * + * @type {object} + */ + var $cookie = { + get: function get() { + return document.cookie; + }, + set: function set(value) { + document.cookie = value; + } + }; + /** * @private * @@ -99,10 +120,53 @@ return /******/ (function(modules) { // webpackBootstrap var _proxy = { localStorage: window.localStorage, sessionStorage: window.sessionStorage, - cookieStorage: cookieStorage(), - memoryStorage: memoryStorage() + cookieStorage: initApi(cookieStorage()), + memoryStorage: initApi(memoryStorage()) }; + /** + * @private + * + * Adds the current elements in the storage object + * + * @param {object} api: the API to initialize + * @return {object} + */ + function initApi(api) { + // sets read-only and non-enumerable properties + for (var prop in api) { + // eslint-disable-line + if (prop !== 'initialize') setProperty(api, prop); + } + api.initialize(); + // this method is removed after being invoked + // because is not part of the Web Storage interface + delete api.initialize; + return api; + } + + /** + * @private + * + * Creates a non-enumerable read-only property. + * + * @param {object} obj: the object to add the property + * @param {string} name: the name of the property + * @param {any} value: the value of the property + * @return {void} + */ + function setProperty(obj, name, value) { + var descriptor = { + configurable: false, + enumerable: false, + writable: false + }; + if (typeof value !== 'undefined') { + descriptor.value = value; + } + Object.defineProperty(obj, name, descriptor); + } + /** * @private * @@ -118,6 +182,8 @@ return /******/ (function(modules) { // webpackBootstrap }; /** + * @private + * * Executes the interceptors of a WebStorage method * * @param {string} command: name of the method to intercept @@ -147,25 +213,6 @@ return /******/ (function(modules) { // webpackBootstrap } } - /** - * @private - * - * Creates a non-enumerable read-only property. - * - * @param {object} obj: the object to add the property - * @param {string} name: the name of the property - * @param {any} value: the value of the property - * @return {void} - */ - function setProperty(obj, name, value) { - Object.defineProperty(obj, name, { - configurable: false, - enumerable: false, - writable: false, - value: value - }); - } - /** * @public * @@ -187,12 +234,23 @@ return /******/ (function(modules) { // webpackBootstrap * @memberOf WebStorage */ function WebStorage(storageType) { + var _this = this; + _classCallCheck(this, WebStorage); if (!_proxy.hasOwnProperty(storageType)) { throw new Error('Storage type "' + storageType + '" is not valid'); } - setProperty(this, '__storage', _proxy[storageType]); + setProperty(this, '__storage__', storageType); + // copies all existing elements in the storage + Object.keys(_proxy[storageType]).forEach(function (key) { + var value = _proxy[storageType][key]; + try { + _this[key] = JSON.parse(value); + } catch (e) { + _this[key] = value; + } + }); } /** * Stores a value given a key name. @@ -213,7 +271,7 @@ return /******/ (function(modules) { // webpackBootstrap this[key] = value; executeInterceptors('setItem', key, value, options); value = JSON.stringify(value); - this.__storage.setItem(key, value, options); + _proxy[this.__storage__].setItem(key, value, options); } /** * Retrieves a value by its key name. @@ -229,8 +287,8 @@ return /******/ (function(modules) { // webpackBootstrap value: function getItem(key) { checkEmpty(key); executeInterceptors('getItem', key); - var value = JSON.parse(this.__storage.getItem(key)); - return value; + var value = _proxy[this.__storage__].getItem(key); + return JSON.parse(value); } /** * Deletes a key from the storage. @@ -247,7 +305,7 @@ return /******/ (function(modules) { // webpackBootstrap checkEmpty(key); delete this[key]; executeInterceptors('removeItem', key); - this.__storage.removeItem(key); + _proxy[this.__storage__].removeItem(key); } /** * Removes all keys from the storage. @@ -260,13 +318,13 @@ return /******/ (function(modules) { // webpackBootstrap }, { key: 'clear', value: function clear() { - var _this = this; + var _this2 = this; executeInterceptors('clear'); Object.keys(this).forEach(function (key) { - return delete _this[key]; + return delete _this2[key]; }); - this.__storage.clear(); + _proxy[this.__storage__].clear(); } /** * Gets the number of data items stored in the Storage object. @@ -325,14 +383,6 @@ return /******/ (function(modules) { // webpackBootstrap */ var storage = null; - /** - * @private - * - * Current storage type - * @type {string} - */ - var _currentStorageName = null; - /** * @public * @@ -341,7 +391,7 @@ return /******/ (function(modules) { // webpackBootstrap */ var configStorage = { get: function get() { - return _currentStorageName; + return storage.__storage__; }, @@ -355,7 +405,6 @@ return /******/ (function(modules) { // webpackBootstrap throw new Error('Storage type "' + storageType + '" is not valid'); } exports.default = storage = new WebStorage(storageType); - _currentStorageName = storageType; } }; @@ -411,21 +460,15 @@ return /******/ (function(modules) { // webpackBootstrap /** * @private * - * Alias for the default cookie storage associated with the current document. - * - * @Reference - * https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie - * - * @type {object} + * Callback that finds an element in the array. + * @param {string} cookie: key=value + * @return {boolean} */ - var $cookie = { - get: function get() { - return document.cookie; - }, - set: function set(value) { - document.cookie = value; - } - }; + function findCookie(cookie) { + var nameEQ = this.toString(); + // prevent leading spaces before the key + return cookie.trim().indexOf(nameEQ) === 0; + } /** * @private @@ -448,7 +491,7 @@ return /******/ (function(modules) { // webpackBootstrap $cookie.set(cookie); }, getItem: function getItem(key) { - var value = null; + var value = void 0; var nameEQ = key + '='; var cookie = $cookie.get().split(';').find(findCookie, nameEQ); if (cookie) { @@ -473,36 +516,19 @@ return /******/ (function(modules) { // webpackBootstrap api.removeItem(key.trim()); } }); + }, + initialize: function initialize() { + $cookie.get().split(';').forEach(function (cookie) { + var index = cookie.indexOf('='); + var key = cookie.substring(0, index).trim(); + var value = cookie.substring(index + 1).trim(); + api[key] = decodeURIComponent(value); + }); } }; return api; } - /** - * @private - * - * Callback that finds an element in the array. - * @param {string} cookie: key=value - * @return {boolean} - */ - function findCookie(cookie) { - var nameEQ = this.toString(); - // prevent leading spaces before the key - return cookie.trim().indexOf(nameEQ) === 0; - } - - /** - * @private - * - * Callback that finds an element in the array. - * @param {object} item: {key, value} - * @return {boolean} - */ - function findItem(item) { - var key = this.toString(); - return item.key === key; - } - /** * @private * @@ -516,23 +542,24 @@ return /******/ (function(modules) { // webpackBootstrap var hashtable = getStoreFromWindow(); var api = { setItem: function setItem(key, value) { - var item = hashtable.find(findItem, key); - if (item) item.value = value;else hashtable.push({ key: key, value: value }); + hashtable[key] = value; setStoreToWindow(hashtable); }, getItem: function getItem(key) { - var item = hashtable.find(findItem, key); - if (item) return item.value; - return null; + return hashtable[key]; }, removeItem: function removeItem(key) { - var index = hashtable.findIndex(findItem, key); - if (index > -1) hashtable.splice(index, 1); + delete hashtable[key]; setStoreToWindow(hashtable); }, clear: function clear() { - hashtable.length = 0; + Object.keys(hashtable).forEach(function (key) { + return delete hashtable[key]; + }); setStoreToWindow(hashtable); + }, + initialize: function initialize() { + Object.assign(api, hashtable); } }; return api; @@ -541,22 +568,23 @@ return /******/ (function(modules) { // webpackBootstrap /** * @private * - * Gets the hashtable store from the current window. - * @return {array} + * Gets the hashtable-store from the current window. + * @return {object} */ function getStoreFromWindow() { try { var store = JSON.parse(window.self.name); - if (store instanceof Array) return store; - } catch (e) {} // eslint-disable-line - return []; /* {key,value} */ + if (store && (typeof store === 'undefined' ? 'undefined' : _typeof(store)) === 'object') return store; + } catch (e) { + return {}; + } } /** * @private * - * Saves the hashtable store in the current window. - * @param {array} hashtable: list of objects stored in memoryStorage + * Saves the hashtable-store in the current window. + * @param {object} hashtable: {key,value} pairs stored in memoryStorage * @return {void} */ function setStoreToWindow(hashtable) { diff --git a/dist/proxy-storage.min.js b/dist/proxy-storage.min.js index f19871a..c4a2be5 100644 --- a/dist/proxy-storage.min.js +++ b/dist/proxy-storage.min.js @@ -1,2 +1,2 @@ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.proxyStorage=t():e.proxyStorage=t()}(this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return e[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function r(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;r0&&void 0!==arguments[0]?arguments[0]:{},t=Object.assign({},e),n=t.date instanceof Date?t.date:new Date;return+t.hours&&n.setHours(n.getHours()+t.hours),+t.days&&n.setDate(n.getDate()+t.days),+t.months&&n.setMonth(n.getMonth()+t.months),+t.years&&n.setFullYear(n.getFullYear()+t.years),n}function u(e){var t=s(e instanceof Date?{date:e}:e);return"; expires="+t.toUTCString()}function c(){var e={setItem:function(e,t,n){var r="",o=void 0;n=Object.assign({path:"/"},n),(a(n.expires)||n.expires instanceof Date)&&(r=u(n.expires)),o=e+"="+encodeURIComponent(t)+r+"; path="+n.path,O.set(o)},getItem:function(e){var t=null,n=e+"=",r=O.get().split(";").find(f,n);return r&&(t=r.trim().substring(n.length,r.length),t=decodeURIComponent(t)),t},removeItem:function(t){e.setItem(t,"",{expires:{days:-1}})},clear:function(){var t="=",n=void 0,r=void 0;O.get().split(";").forEach(function(o){n=o.indexOf(t),n>-1&&(r=o.substring(0,n),e.removeItem(r.trim()))})}};return e}function f(e){var t=this.toString();return 0===e.trim().indexOf(t)}function l(e){var t=this.toString();return e.key===t}function g(){var e=m(),t={setItem:function(t,n){var r=e.find(l,t);r?r.value=n:e.push({key:t,value:n}),p(e)},getItem:function(t){var n=e.find(l,t);return n?n.value:null},removeItem:function(t){var n=e.findIndex(l,t);n>-1&&e.splice(n,1),p(e)},clear:function(){e.length=0,p(e)}};return t}function m(){try{var e=JSON.parse(window.self.name);if(e instanceof Array)return e}catch(e){}return[]}function p(e){var t=JSON.stringify(e);window.self.name=t}function v(e){var t=S[e],n="__proxy-storage__";try{return t.setItem(n,n),t.removeItem(n),!0}catch(e){return!1}}function d(e){return I[e]&&k.set(e),I[e]}function y(){I.localStorage=v("localStorage"),I.sessionStorage=v("sessionStorage"),I.cookieStorage=v("cookieStorage"),Object.keys(I).some(d)}Object.defineProperty(t,"__esModule",{value:!0});var h=function(){function e(e,t){for(var n=0;n1?t-1:0),o=1;o0&&void 0!==arguments[0]?arguments[0]:{},t=Object.assign({},e),n=t.date instanceof Date?t.date:new Date;return+t.hours&&n.setHours(n.getHours()+t.hours),+t.days&&n.setDate(n.getDate()+t.days),+t.months&&n.setMonth(n.getMonth()+t.months),+t.years&&n.setFullYear(n.getFullYear()+t.years),n}function u(e){var t=s(e instanceof Date?{date:e}:e);return"; expires="+t.toUTCString()}function f(e){var t=this.toString();return 0===e.trim().indexOf(t)}function l(){var e={setItem:function(e,t,n){var o="",r=void 0;n=Object.assign({path:"/"},n),(c(n.expires)||n.expires instanceof Date)&&(o=u(n.expires)),r=e+"="+encodeURIComponent(t)+o+"; path="+n.path,S.set(r)},getItem:function(e){var t=void 0,n=e+"=",o=S.get().split(";").find(f,n);return o&&(t=o.trim().substring(n.length,o.length),t=decodeURIComponent(t)),t},removeItem:function(t){e.setItem(t,"",{expires:{days:-1}})},clear:function(){var t="=",n=void 0,o=void 0;S.get().split(";").forEach(function(r){n=r.indexOf(t),n>-1&&(o=r.substring(0,n),e.removeItem(o.trim()))})},initialize:function(){S.get().split(";").forEach(function(t){var n=t.indexOf("="),o=t.substring(0,n).trim(),r=t.substring(n+1).trim();e[o]=decodeURIComponent(r)})}};return e}function g(){var e=m(),t={setItem:function(t,n){e[t]=n,p(e)},getItem:function(t){return e[t]},removeItem:function(t){delete e[t],p(e)},clear:function(){Object.keys(e).forEach(function(t){return delete e[t]}),p(e)},initialize:function(){Object.assign(t,e)}};return t}function m(){try{var e=JSON.parse(window.self.name);if(e&&"object"===("undefined"==typeof e?"undefined":h(e)))return e}catch(e){return{}}}function p(e){var t=JSON.stringify(e);window.self.name=t}function y(e){var t=_[e],n="__proxy-storage__";try{return t.setItem(n,n),t.removeItem(n),!0}catch(e){return!1}}function d(e){return O[e]&&x.set(e),O[e]}function v(){O.localStorage=y("localStorage"),O.sessionStorage=y("sessionStorage"),O.cookieStorage=y("cookieStorage"),Object.keys(O).some(d)}Object.defineProperty(t,"__esModule",{value:!0});var h="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},b=function(){function e(e,t){for(var n=0;n 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {\n\t args[_key - 1] = arguments[_key];\n\t }\n\t\n\t _interceptors[command].forEach(function (action) {\n\t return action.apply(undefined, args);\n\t });\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Validates if the key is not empty.\n\t * (null, undefined or empty string)\n\t * @param {string} key: keyname of the storage\n\t * @return {void}\n\t */\n\tfunction checkEmpty(key) {\n\t if (key == null || key === '') {\n\t throw new Error('The key provided can not be empty');\n\t }\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Creates a non-enumerable read-only property.\n\t *\n\t * @param {object} obj: the object to add the property\n\t * @param {string} name: the name of the property\n\t * @param {any} value: the value of the property\n\t * @return {void}\n\t */\n\tfunction setProperty(obj, name, value) {\n\t Object.defineProperty(obj, name, {\n\t configurable: false,\n\t enumerable: false,\n\t writable: false,\n\t value: value\n\t });\n\t}\n\t\n\t/**\n\t * @public\n\t *\n\t * Implementation of the Web Storage interface.\n\t * It saves and retrieves values as JSON.\n\t *\n\t * @Reference\n\t * https://developer.mozilla.org/en-US/docs/Web/API/Storage\n\t *\n\t * @type {Class}\n\t */\n\t\n\tvar WebStorage = function () {\n\t /**\n\t * Creates an instance of WebStorage.\n\t *\n\t * @param {string} storageType: it can be \"localStorage\", \"sessionStorage\", \"cookieStorage\", or \"memoryStorage\"\n\t *\n\t * @memberOf WebStorage\n\t */\n\t function WebStorage(storageType) {\n\t _classCallCheck(this, WebStorage);\n\t\n\t if (!_proxy.hasOwnProperty(storageType)) {\n\t throw new Error('Storage type \"' + storageType + '\" is not valid');\n\t }\n\t setProperty(this, '__storage', _proxy[storageType]);\n\t }\n\t /**\n\t * Stores a value given a key name.\n\t *\n\t * @param {string} key: keyname of the storage\n\t * @param {any} value: data to save in the storage\n\t * @param {object} options: additional options for cookieStorage\n\t * @return {void}\n\t *\n\t * @memberOf WebStorage\n\t */\n\t\n\t\n\t _createClass(WebStorage, [{\n\t key: 'setItem',\n\t value: function setItem(key, value, options) {\n\t checkEmpty(key);\n\t this[key] = value;\n\t executeInterceptors('setItem', key, value, options);\n\t value = JSON.stringify(value);\n\t this.__storage.setItem(key, value, options);\n\t }\n\t /**\n\t * Retrieves a value by its key name.\n\t *\n\t * @param {string} key: keyname of the storage\n\t * @return {void}\n\t *\n\t * @memberOf WebStorage\n\t */\n\t\n\t }, {\n\t key: 'getItem',\n\t value: function getItem(key) {\n\t checkEmpty(key);\n\t executeInterceptors('getItem', key);\n\t var value = JSON.parse(this.__storage.getItem(key));\n\t return value;\n\t }\n\t /**\n\t * Deletes a key from the storage.\n\t *\n\t * @param {string} key: keyname of the storage\n\t * @return {void}\n\t *\n\t * @memberOf WebStorage\n\t */\n\t\n\t }, {\n\t key: 'removeItem',\n\t value: function removeItem(key) {\n\t checkEmpty(key);\n\t delete this[key];\n\t executeInterceptors('removeItem', key);\n\t this.__storage.removeItem(key);\n\t }\n\t /**\n\t * Removes all keys from the storage.\n\t *\n\t * @return {void}\n\t *\n\t * @memberOf WebStorage\n\t */\n\t\n\t }, {\n\t key: 'clear',\n\t value: function clear() {\n\t var _this = this;\n\t\n\t executeInterceptors('clear');\n\t Object.keys(this).forEach(function (key) {\n\t return delete _this[key];\n\t });\n\t this.__storage.clear();\n\t }\n\t /**\n\t * Gets the number of data items stored in the Storage object.\n\t *\n\t * @readonly\n\t *\n\t * @memberOf WebStorage\n\t */\n\t\n\t }, {\n\t key: 'length',\n\t get: function get() {\n\t return Object.keys(this).length;\n\t }\n\t /**\n\t * Adds an interceptor to a WebStorage method\n\t *\n\t * @param {string} command: name of the API method to intercept\n\t * @param {function} action: callback executed when the API method is called\n\t * @return {void}\n\t *\n\t * @memberOf WebStorage\n\t */\n\t\n\t }], [{\n\t key: 'interceptors',\n\t value: function interceptors(command, action) {\n\t if (command in _interceptors && typeof action === 'function') _interceptors[command].push(action);\n\t }\n\t }]);\n\t\n\t return WebStorage;\n\t}();\n\t\n\t/**\n\t * @public\n\t *\n\t * Determines which storage mechanisms are available.\n\t *\n\t * @type {object}\n\t */\n\t\n\t\n\tvar isAvaliable = {\n\t localStorage: false,\n\t sessionStorage: false,\n\t cookieStorage: false,\n\t memoryStorage: true\n\t};\n\t\n\t/**\n\t * @public\n\t *\n\t * Current storage mechanism.\n\t * @type {object}\n\t */\n\tvar storage = null;\n\t\n\t/**\n\t * @private\n\t *\n\t * Current storage type\n\t * @type {string}\n\t */\n\tvar _currentStorageName = null;\n\t\n\t/**\n\t * @public\n\t *\n\t * Get/Set the storage mechanism to use by default.\n\t * @type {object}\n\t */\n\tvar configStorage = {\n\t get: function get() {\n\t return _currentStorageName;\n\t },\n\t\n\t\n\t /**\n\t * Sets the storage mechanism to use by default.\n\t * @param {string} storageType: it can be \"localStorage\", \"sessionStorage\", \"cookieStorage\", or \"memoryStorage\"\n\t * @return {void}\n\t */\n\t set: function set(storageType) {\n\t if (!_proxy.hasOwnProperty(storageType)) {\n\t throw new Error('Storage type \"' + storageType + '\" is not valid');\n\t }\n\t exports.default = storage = new WebStorage(storageType);\n\t _currentStorageName = storageType;\n\t }\n\t};\n\t\n\t/**\n\t * @private\n\t *\n\t * Determines whether a value is a plain object\n\t *\n\t * @param {any} value: the object to test\n\t * @return {boolean}\n\t */\n\tfunction isObject(value) {\n\t return Object.prototype.toString.call(value) === '[object Object]';\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Allows add or subtract timestamps to the current date or to a specific date.\n\t * @param {object} options: It contains the timestamps to add or remove to the date, and have the following properties:\n\t * - {Date} date: if provided, the timestamps will affect this date, otherwise a new current date will be used.\n\t * - {number} hours: hours to add/subtract\n\t * - {number} days: days to add/subtract\n\t * - {number} months: months to add/subtract\n\t * - {number} years: years to add/subtract\n\t * @return {Date}\n\t */\n\tfunction setTimestamp() {\n\t var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};\n\t\n\t var opt = Object.assign({}, options);\n\t var d = opt.date instanceof Date ? opt.date : new Date();\n\t if (+opt.hours) d.setHours(d.getHours() + opt.hours);\n\t if (+opt.days) d.setDate(d.getDate() + opt.days);\n\t if (+opt.months) d.setMonth(d.getMonth() + opt.months);\n\t if (+opt.years) d.setFullYear(d.getFullYear() + opt.years);\n\t return d;\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Builds the expiration part for the cookie\n\t *\n\t * @param {Date|object} date: the expiration date. See `setTimestamp(options)`\n\t * @return {string}\n\t */\n\tfunction buildExpirationString(date) {\n\t var expires = date instanceof Date ? setTimestamp({ date: date }) : setTimestamp(date);\n\t return '; expires=' + expires.toUTCString();\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Alias for the default cookie storage associated with the current document.\n\t *\n\t * @Reference\n\t * https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie\n\t *\n\t * @type {object}\n\t */\n\tvar $cookie = {\n\t get: function get() {\n\t return document.cookie;\n\t },\n\t set: function set(value) {\n\t document.cookie = value;\n\t }\n\t};\n\t\n\t/**\n\t * @private\n\t *\n\t * Manages actions for creation/reading/deleting cookies.\n\t * Implements Web Storage interface methods.\n\t *\n\t * @return {object}\n\t */\n\tfunction cookieStorage() {\n\t var api = {\n\t setItem: function setItem(key, value, options) {\n\t var expires = '',\n\t cookie = void 0;\n\t options = Object.assign({ path: '/' }, options);\n\t if (isObject(options.expires) || options.expires instanceof Date) {\n\t expires = buildExpirationString(options.expires);\n\t }\n\t cookie = key + '=' + encodeURIComponent(value) + expires + '; path=' + options.path;\n\t $cookie.set(cookie);\n\t },\n\t getItem: function getItem(key) {\n\t var value = null;\n\t var nameEQ = key + '=';\n\t var cookie = $cookie.get().split(';').find(findCookie, nameEQ);\n\t if (cookie) {\n\t // prevent leading spaces before the key name\n\t value = cookie.trim().substring(nameEQ.length, cookie.length);\n\t value = decodeURIComponent(value);\n\t }\n\t return value;\n\t },\n\t removeItem: function removeItem(key) {\n\t api.setItem(key, '', { expires: { days: -1 } });\n\t },\n\t clear: function clear() {\n\t var eq = '=';\n\t var indexEQ = void 0,\n\t key = void 0;\n\t $cookie.get().split(';').forEach(function (cookie) {\n\t indexEQ = cookie.indexOf(eq);\n\t if (indexEQ > -1) {\n\t key = cookie.substring(0, indexEQ);\n\t // prevent leading spaces before the key\n\t api.removeItem(key.trim());\n\t }\n\t });\n\t }\n\t };\n\t return api;\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Callback that finds an element in the array.\n\t * @param {string} cookie: key=value\n\t * @return {boolean}\n\t */\n\tfunction findCookie(cookie) {\n\t var nameEQ = this.toString();\n\t // prevent leading spaces before the key\n\t return cookie.trim().indexOf(nameEQ) === 0;\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Callback that finds an element in the array.\n\t * @param {object} item: {key, value}\n\t * @return {boolean}\n\t */\n\tfunction findItem(item) {\n\t var key = this.toString();\n\t return item.key === key;\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Manages actions for creation/reading/deleting data in memory.\n\t * Implements Web Storage interface methods.\n\t * It also adds a hack to persist the store as a session in the current window.\n\t *\n\t * @return {object}\n\t */\n\tfunction memoryStorage() {\n\t var hashtable = getStoreFromWindow();\n\t var api = {\n\t setItem: function setItem(key, value) {\n\t var item = hashtable.find(findItem, key);\n\t if (item) item.value = value;else hashtable.push({ key: key, value: value });\n\t setStoreToWindow(hashtable);\n\t },\n\t getItem: function getItem(key) {\n\t var item = hashtable.find(findItem, key);\n\t if (item) return item.value;\n\t return null;\n\t },\n\t removeItem: function removeItem(key) {\n\t var index = hashtable.findIndex(findItem, key);\n\t if (index > -1) hashtable.splice(index, 1);\n\t setStoreToWindow(hashtable);\n\t },\n\t clear: function clear() {\n\t hashtable.length = 0;\n\t setStoreToWindow(hashtable);\n\t }\n\t };\n\t return api;\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Gets the hashtable store from the current window.\n\t * @return {array}\n\t */\n\tfunction getStoreFromWindow() {\n\t try {\n\t var store = JSON.parse(window.self.name);\n\t if (store instanceof Array) return store;\n\t } catch (e) {} // eslint-disable-line\n\t return []; /* {key,value} */\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Saves the hashtable store in the current window.\n\t * @param {array} hashtable: list of objects stored in memoryStorage\n\t * @return {void}\n\t */\n\tfunction setStoreToWindow(hashtable) {\n\t var store = JSON.stringify(hashtable);\n\t window.self.name = store;\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Checks whether a storage mechanism is available.\n\t * @param {string} storageType: it can be \"localStorage\", \"sessionStorage\", \"cookieStorage\", or \"memoryStorage\"\n\t * @return {boolean}\n\t */\n\tfunction isStorageAvailable(storageType) {\n\t var storageObj = _proxy[storageType];\n\t var data = '__proxy-storage__';\n\t try {\n\t storageObj.setItem(data, data);\n\t storageObj.removeItem(data);\n\t return true;\n\t } catch (e) {\n\t return false;\n\t }\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Sets the default storage mechanism available.\n\t * @param {string} storageType: it can be \"localStorage\", \"sessionStorage\", \"cookieStorage\", or \"memoryStorage\"\n\t * @return {boolean}\n\t */\n\tfunction storageAvaliable(storageType) {\n\t if (isAvaliable[storageType]) {\n\t configStorage.set(storageType);\n\t }\n\t return isAvaliable[storageType];\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Initializes the module.\n\t * @return {void}\n\t */\n\tfunction init() {\n\t isAvaliable.localStorage = isStorageAvailable('localStorage');\n\t isAvaliable.sessionStorage = isStorageAvailable('sessionStorage');\n\t isAvaliable.cookieStorage = isStorageAvailable('cookieStorage');\n\t // sets the default storage mechanism available\n\t Object.keys(isAvaliable).some(storageAvaliable);\n\t}\n\t\n\tinit();\n\t\n\t// @public API\n\texports.default = storage;\n\texports.WebStorage = WebStorage;\n\texports.configStorage = configStorage;\n\texports.isAvaliable = isAvaliable;\n\n/***/ }\n/******/ ])\n});\n;\n\n\n// WEBPACK FOOTER //\n// proxy-storage.min.js"," \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId])\n \t\t\treturn installedModules[moduleId].exports;\n\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\texports: {},\n \t\t\tid: moduleId,\n \t\t\tloaded: false\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.loaded = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(0);\n\n\n\n// WEBPACK FOOTER //\n// webpack/bootstrap e3ddb6ef6d26ca879973","/* eslint-disable no-use-before-define, no-invalid-this */\n\n/**\n * This library uses an adapter that implements the Web Storage interface,\n * which is very useful to deal with the lack of compatibility between\n * document.cookie and localStorage and sessionStorage.\n *\n * It also provides a memoryStorage fallback that stores the data in memory\n * when all of above mechanisms are not available.\n *\n * Author: David Rivera\n * Github: https://github.com/jherax\n * License: \"MIT\"\n *\n * You can fork this project on github:\n * https://github.com/jherax/proxy-storage.git\n */\n\n// If you want to support all ES6 features, uncomment the next line\n// import 'babel-polyfill';\n\n/**\n * @private\n *\n * Proxy for Web Storage and Cookies.\n * All members implement the Web Storage interface.\n *\n * @Reference\n * https://developer.mozilla.org/en-US/docs/Web/API/Storage\n *\n * @type {object}\n */\nconst _proxy = {\n localStorage: window.localStorage,\n sessionStorage: window.sessionStorage,\n cookieStorage: cookieStorage(),\n memoryStorage: memoryStorage(),\n};\n\n/**\n * @private\n *\n * Stores the interceptors for WebStorage methods\n *\n * @type {object}\n */\nconst _interceptors = {\n setItem: [],\n getItem: [],\n removeItem: [],\n clear: [],\n};\n\n/**\n * Executes the interceptors of a WebStorage method\n *\n * @param {string} command: name of the method to intercept\n * @return {void}\n */\nfunction executeInterceptors(command, ...args) {\n _interceptors[command].forEach((action) => action(...args));\n}\n\n/**\n * @private\n *\n * Validates if the key is not empty.\n * (null, undefined or empty string)\n * @param {string} key: keyname of the storage\n * @return {void}\n */\nfunction checkEmpty(key) {\n if (key == null || key === '') {\n throw new Error('The key provided can not be empty');\n }\n}\n\n/**\n * @private\n *\n * Creates a non-enumerable read-only property.\n *\n * @param {object} obj: the object to add the property\n * @param {string} name: the name of the property\n * @param {any} value: the value of the property\n * @return {void}\n */\nfunction setProperty(obj, name, value) {\n Object.defineProperty(obj, name, {\n configurable: false,\n enumerable: false,\n writable: false,\n value: value,\n });\n}\n\n/**\n * @public\n *\n * Implementation of the Web Storage interface.\n * It saves and retrieves values as JSON.\n *\n * @Reference\n * https://developer.mozilla.org/en-US/docs/Web/API/Storage\n *\n * @type {Class}\n */\nclass WebStorage {\n /**\n * Creates an instance of WebStorage.\n *\n * @param {string} storageType: it can be \"localStorage\", \"sessionStorage\", \"cookieStorage\", or \"memoryStorage\"\n *\n * @memberOf WebStorage\n */\n constructor(storageType) {\n if (!_proxy.hasOwnProperty(storageType)) {\n throw new Error(`Storage type \"${storageType}\" is not valid`);\n }\n setProperty(this, '__storage', _proxy[storageType]);\n }\n /**\n * Stores a value given a key name.\n *\n * @param {string} key: keyname of the storage\n * @param {any} value: data to save in the storage\n * @param {object} options: additional options for cookieStorage\n * @return {void}\n *\n * @memberOf WebStorage\n */\n setItem(key, value, options) {\n checkEmpty(key);\n this[key] = value;\n executeInterceptors('setItem', key, value, options);\n value = JSON.stringify(value);\n this.__storage.setItem(key, value, options);\n }\n /**\n * Retrieves a value by its key name.\n *\n * @param {string} key: keyname of the storage\n * @return {void}\n *\n * @memberOf WebStorage\n */\n getItem(key) {\n checkEmpty(key);\n executeInterceptors('getItem', key);\n const value = JSON.parse(this.__storage.getItem(key));\n return value;\n }\n /**\n * Deletes a key from the storage.\n *\n * @param {string} key: keyname of the storage\n * @return {void}\n *\n * @memberOf WebStorage\n */\n removeItem(key) {\n checkEmpty(key);\n delete this[key];\n executeInterceptors('removeItem', key);\n this.__storage.removeItem(key);\n }\n /**\n * Removes all keys from the storage.\n *\n * @return {void}\n *\n * @memberOf WebStorage\n */\n clear() {\n executeInterceptors('clear');\n Object.keys(this).forEach((key) => delete this[key]);\n this.__storage.clear();\n }\n /**\n * Gets the number of data items stored in the Storage object.\n *\n * @readonly\n *\n * @memberOf WebStorage\n */\n get length() {\n return Object.keys(this).length;\n }\n /**\n * Adds an interceptor to a WebStorage method\n *\n * @param {string} command: name of the API method to intercept\n * @param {function} action: callback executed when the API method is called\n * @return {void}\n *\n * @memberOf WebStorage\n */\n static interceptors(command, action) {\n if (command in _interceptors && typeof action === 'function')\n _interceptors[command].push(action);\n }\n}\n\n/**\n * @public\n *\n * Determines which storage mechanisms are available.\n *\n * @type {object}\n */\nconst isAvaliable = {\n localStorage: false,\n sessionStorage: false,\n cookieStorage: false,\n memoryStorage: true,\n};\n\n/**\n * @public\n *\n * Current storage mechanism.\n * @type {object}\n */\nlet storage = null;\n\n/**\n * @private\n *\n * Current storage type\n * @type {string}\n */\nlet _currentStorageName = null;\n\n/**\n * @public\n *\n * Get/Set the storage mechanism to use by default.\n * @type {object}\n */\nconst configStorage = {\n get() {\n return _currentStorageName;\n },\n\n /**\n * Sets the storage mechanism to use by default.\n * @param {string} storageType: it can be \"localStorage\", \"sessionStorage\", \"cookieStorage\", or \"memoryStorage\"\n * @return {void}\n */\n set(storageType) {\n if (!_proxy.hasOwnProperty(storageType)) {\n throw new Error(`Storage type \"${storageType}\" is not valid`);\n }\n storage = new WebStorage(storageType);\n _currentStorageName = storageType;\n },\n};\n\n/**\n * @private\n *\n * Determines whether a value is a plain object\n *\n * @param {any} value: the object to test\n * @return {boolean}\n */\nfunction isObject(value) {\n return Object.prototype.toString.call(value) === '[object Object]';\n}\n\n/**\n * @private\n *\n * Allows add or subtract timestamps to the current date or to a specific date.\n * @param {object} options: It contains the timestamps to add or remove to the date, and have the following properties:\n * - {Date} date: if provided, the timestamps will affect this date, otherwise a new current date will be used.\n * - {number} hours: hours to add/subtract\n * - {number} days: days to add/subtract\n * - {number} months: months to add/subtract\n * - {number} years: years to add/subtract\n * @return {Date}\n */\nfunction setTimestamp(options = {}) {\n const opt = Object.assign({}, options);\n let d = opt.date instanceof Date ? opt.date : new Date();\n if (+opt.hours) d.setHours(d.getHours() + opt.hours);\n if (+opt.days) d.setDate(d.getDate() + opt.days);\n if (+opt.months) d.setMonth(d.getMonth() + opt.months);\n if (+opt.years) d.setFullYear(d.getFullYear() + opt.years);\n return d;\n}\n\n/**\n * @private\n *\n * Builds the expiration part for the cookie\n *\n * @param {Date|object} date: the expiration date. See `setTimestamp(options)`\n * @return {string}\n */\nfunction buildExpirationString(date) {\n let expires = (date instanceof Date ?\n setTimestamp({date}) :\n setTimestamp(date)\n );\n return `; expires=${expires.toUTCString()}`;\n}\n\n/**\n * @private\n *\n * Alias for the default cookie storage associated with the current document.\n *\n * @Reference\n * https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie\n *\n * @type {object}\n */\nconst $cookie = {\n get: () => document.cookie,\n set: (value) => {\n document.cookie = value;\n },\n};\n\n/**\n * @private\n *\n * Manages actions for creation/reading/deleting cookies.\n * Implements Web Storage interface methods.\n *\n * @return {object}\n */\nfunction cookieStorage() {\n const api = {\n setItem(key, value, options) {\n let expires = '', cookie;\n options = Object.assign({path: '/'}, options);\n if (isObject(options.expires) || options.expires instanceof Date) {\n expires = buildExpirationString(options.expires);\n }\n cookie = `${key}=${encodeURIComponent(value)}${expires}; path=${options.path}`;\n $cookie.set(cookie);\n },\n\n getItem(key) {\n let value = null;\n const nameEQ = `${key}=`;\n const cookie = $cookie.get().split(';').find(findCookie, nameEQ);\n if (cookie) {\n // prevent leading spaces before the key name\n value = cookie.trim().substring(nameEQ.length, cookie.length);\n value = decodeURIComponent(value);\n }\n return value;\n },\n\n removeItem(key) {\n api.setItem(key, '', {expires: {days: -1}});\n },\n\n clear() {\n const eq = '=';\n let indexEQ, key;\n $cookie.get().split(';').forEach((cookie) => {\n indexEQ = cookie.indexOf(eq);\n if (indexEQ > -1) {\n key = cookie.substring(0, indexEQ);\n // prevent leading spaces before the key\n api.removeItem(key.trim());\n }\n });\n },\n };\n return api;\n}\n\n/**\n * @private\n *\n * Callback that finds an element in the array.\n * @param {string} cookie: key=value\n * @return {boolean}\n */\nfunction findCookie(cookie) {\n const nameEQ = this.toString();\n // prevent leading spaces before the key\n return cookie.trim().indexOf(nameEQ) === 0;\n}\n\n/**\n * @private\n *\n * Callback that finds an element in the array.\n * @param {object} item: {key, value}\n * @return {boolean}\n */\nfunction findItem(item) {\n const key = this.toString();\n return item.key === key;\n}\n\n/**\n * @private\n *\n * Manages actions for creation/reading/deleting data in memory.\n * Implements Web Storage interface methods.\n * It also adds a hack to persist the store as a session in the current window.\n *\n * @return {object}\n */\nfunction memoryStorage() {\n const hashtable = getStoreFromWindow();\n const api = {\n setItem(key, value) {\n const item = hashtable.find(findItem, key);\n if (item) item.value = value;\n else hashtable.push({key, value});\n setStoreToWindow(hashtable);\n },\n getItem(key) {\n const item = hashtable.find(findItem, key);\n if (item) return item.value;\n return null;\n },\n removeItem(key) {\n const index = hashtable.findIndex(findItem, key);\n if (index > -1) hashtable.splice(index, 1);\n setStoreToWindow(hashtable);\n },\n clear() {\n hashtable.length = 0;\n setStoreToWindow(hashtable);\n },\n };\n return api;\n}\n\n/**\n * @private\n *\n * Gets the hashtable store from the current window.\n * @return {array}\n */\nfunction getStoreFromWindow() {\n try {\n const store = JSON.parse(window.self.name);\n if (store instanceof Array) return store;\n } catch (e) {} // eslint-disable-line\n return []; /* {key,value} */\n}\n\n/**\n * @private\n *\n * Saves the hashtable store in the current window.\n * @param {array} hashtable: list of objects stored in memoryStorage\n * @return {void}\n */\nfunction setStoreToWindow(hashtable) {\n const store = JSON.stringify(hashtable);\n window.self.name = store;\n}\n\n/**\n * @private\n *\n * Checks whether a storage mechanism is available.\n * @param {string} storageType: it can be \"localStorage\", \"sessionStorage\", \"cookieStorage\", or \"memoryStorage\"\n * @return {boolean}\n */\nfunction isStorageAvailable(storageType) {\n const storageObj = _proxy[storageType];\n const data = '__proxy-storage__';\n try {\n storageObj.setItem(data, data);\n storageObj.removeItem(data);\n return true;\n } catch (e) {\n return false;\n }\n}\n\n/**\n * @private\n *\n * Sets the default storage mechanism available.\n * @param {string} storageType: it can be \"localStorage\", \"sessionStorage\", \"cookieStorage\", or \"memoryStorage\"\n * @return {boolean}\n */\nfunction storageAvaliable(storageType) {\n if (isAvaliable[storageType]) {\n configStorage.set(storageType);\n }\n return isAvaliable[storageType];\n}\n\n/**\n * @private\n *\n * Initializes the module.\n * @return {void}\n */\nfunction init() {\n isAvaliable.localStorage = isStorageAvailable('localStorage');\n isAvaliable.sessionStorage = isStorageAvailable('sessionStorage');\n isAvaliable.cookieStorage = isStorageAvailable('cookieStorage');\n // sets the default storage mechanism available\n Object.keys(isAvaliable).some(storageAvaliable);\n}\n\ninit();\n\n// @public API\nexport {storage as default, WebStorage, configStorage, isAvaliable};\n\n\n\n// WEBPACK FOOTER //\n// ./src/proxy-storage.js"],"sourceRoot":""} \ No newline at end of file +{"version":3,"sources":["webpack:///webpack/universalModuleDefinition","webpack:///proxy-storage.min.js","webpack:///webpack/bootstrap c9b7043ba837b106a41c","webpack:///./src/proxy-storage.js"],"names":["root","factory","exports","module","define","amd","this","modules","__webpack_require__","moduleId","installedModules","id","loaded","call","m","c","p","_classCallCheck","instance","Constructor","TypeError","initApi","api","prop","setProperty","initialize","obj","name","value","descriptor","configurable","enumerable","writable","Object","defineProperty","executeInterceptors","command","_len","arguments","length","args","Array","_key","_interceptors","forEach","action","checkEmpty","key","Error","isObject","prototype","toString","setTimestamp","options","undefined","opt","assign","d","date","Date","hours","setHours","getHours","days","setDate","getDate","months","setMonth","getMonth","years","setFullYear","getFullYear","buildExpirationString","expires","toUTCString","findCookie","cookie","nameEQ","trim","indexOf","cookieStorage","setItem","path","encodeURIComponent","$cookie","set","getItem","get","split","find","substring","decodeURIComponent","removeItem","clear","eq","indexEQ","index","memoryStorage","hashtable","getStoreFromWindow","setStoreToWindow","keys","store","JSON","parse","window","self","_typeof","e","stringify","isStorageAvailable","storageType","storageObj","_proxy","data","storageAvaliable","isAvaliable","configStorage","init","localStorage","sessionStorage","some","Symbol","iterator","constructor","_createClass","defineProperties","target","props","i","protoProps","staticProps","document","WebStorage","_this","hasOwnProperty","__storage__","_this2","push","storage","default"],"mappings":"CAAA,SAAAA,EAAAC,GACA,gBAAAC,UAAA,gBAAAC,QACAA,OAAAD,QAAAD,IACA,kBAAAG,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AAQH,aAQjBnD,IAVoB,SAUhB0B,GACF,IAAKE,EAAOsB,eAAexB,GACzB,KAAM,IAAI/D,OAAJ,iBAA2B+D,EAA3B,iBAER7G,GA+Oe0I,QA/OfD,EAAU,GAAIN,YAAWtB,IA4O7BO,KF+GCpH,EE5GkB0I,QAAXD,EF6GPzI,EE7G2BmI,sBF8G3BnI,EE9GuCmH,gBF+GvCnH,EE/GsDkH","file":"proxy-storage.min.js","sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"proxyStorage\"] = factory();\n\telse\n\t\troot[\"proxyStorage\"] = factory();\n})(this, function() {\nreturn \n\n\n// WEBPACK FOOTER //\n// webpack/universalModuleDefinition","(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"proxyStorage\"] = factory();\n\telse\n\t\troot[\"proxyStorage\"] = factory();\n})(this, function() {\nreturn /******/ (function(modules) { // webpackBootstrap\n/******/ \t// The module cache\n/******/ \tvar installedModules = {};\n/******/\n/******/ \t// The require function\n/******/ \tfunction __webpack_require__(moduleId) {\n/******/\n/******/ \t\t// Check if module is in cache\n/******/ \t\tif(installedModules[moduleId])\n/******/ \t\t\treturn installedModules[moduleId].exports;\n/******/\n/******/ \t\t// Create a new module (and put it into the cache)\n/******/ \t\tvar module = installedModules[moduleId] = {\n/******/ \t\t\texports: {},\n/******/ \t\t\tid: moduleId,\n/******/ \t\t\tloaded: false\n/******/ \t\t};\n/******/\n/******/ \t\t// Execute the module function\n/******/ \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n/******/\n/******/ \t\t// Flag the module as loaded\n/******/ \t\tmodule.loaded = true;\n/******/\n/******/ \t\t// Return the exports of the module\n/******/ \t\treturn module.exports;\n/******/ \t}\n/******/\n/******/\n/******/ \t// expose the modules object (__webpack_modules__)\n/******/ \t__webpack_require__.m = modules;\n/******/\n/******/ \t// expose the module cache\n/******/ \t__webpack_require__.c = installedModules;\n/******/\n/******/ \t// __webpack_public_path__\n/******/ \t__webpack_require__.p = \"\";\n/******/\n/******/ \t// Load entry module and return exports\n/******/ \treturn __webpack_require__(0);\n/******/ })\n/************************************************************************/\n/******/ ([\n/* 0 */\n/***/ function(module, exports) {\n\n\t'use strict';\n\t\n\tObject.defineProperty(exports, \"__esModule\", {\n\t value: true\n\t});\n\t\n\tvar _typeof = typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; };\n\t\n\tvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\t\n\tfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\t\n\t/* eslint-disable no-use-before-define, no-invalid-this */\n\t\n\t/**\n\t * This library uses an adapter that implements the Web Storage interface,\n\t * which is very useful to deal with the lack of compatibility between\n\t * document.cookie and localStorage and sessionStorage.\n\t *\n\t * It also provides a memoryStorage fallback that stores the data in memory\n\t * when all of above mechanisms are not available.\n\t *\n\t * Author: David Rivera\n\t * Github: https://github.com/jherax\n\t * License: \"MIT\"\n\t *\n\t * You can fork this project on github:\n\t * https://github.com/jherax/proxy-storage.git\n\t */\n\t\n\t// If you want to support all ES6 features, uncomment the next line\n\t// import 'babel-polyfill';\n\t\n\t/**\n\t * @private\n\t *\n\t * Proxy for the default cookie storage associated with the current document.\n\t *\n\t * @Reference\n\t * https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie\n\t *\n\t * @type {object}\n\t */\n\tvar $cookie = {\n\t get: function get() {\n\t return document.cookie;\n\t },\n\t set: function set(value) {\n\t document.cookie = value;\n\t }\n\t};\n\t\n\t/**\n\t * @private\n\t *\n\t * Proxy for Web Storage and Cookies.\n\t * All members implement the Web Storage interface.\n\t *\n\t * @Reference\n\t * https://developer.mozilla.org/en-US/docs/Web/API/Storage\n\t *\n\t * @type {object}\n\t */\n\tvar _proxy = {\n\t localStorage: window.localStorage,\n\t sessionStorage: window.sessionStorage,\n\t cookieStorage: initApi(cookieStorage()),\n\t memoryStorage: initApi(memoryStorage())\n\t};\n\t\n\t/**\n\t * @private\n\t *\n\t * Adds the current elements in the storage object\n\t *\n\t * @param {object} api: the API to initialize\n\t * @return {object}\n\t */\n\tfunction initApi(api) {\n\t // sets read-only and non-enumerable properties\n\t for (var prop in api) {\n\t // eslint-disable-line\n\t if (prop !== 'initialize') setProperty(api, prop);\n\t }\n\t api.initialize();\n\t // this method is removed after being invoked\n\t // because is not part of the Web Storage interface\n\t delete api.initialize;\n\t return api;\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Creates a non-enumerable read-only property.\n\t *\n\t * @param {object} obj: the object to add the property\n\t * @param {string} name: the name of the property\n\t * @param {any} value: the value of the property\n\t * @return {void}\n\t */\n\tfunction setProperty(obj, name, value) {\n\t var descriptor = {\n\t configurable: false,\n\t enumerable: false,\n\t writable: false\n\t };\n\t if (typeof value !== 'undefined') {\n\t descriptor.value = value;\n\t }\n\t Object.defineProperty(obj, name, descriptor);\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Stores the interceptors for WebStorage methods\n\t *\n\t * @type {object}\n\t */\n\tvar _interceptors = {\n\t setItem: [],\n\t getItem: [],\n\t removeItem: [],\n\t clear: []\n\t};\n\t\n\t/**\n\t * @private\n\t *\n\t * Executes the interceptors of a WebStorage method\n\t *\n\t * @param {string} command: name of the method to intercept\n\t * @return {void}\n\t */\n\tfunction executeInterceptors(command) {\n\t for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {\n\t args[_key - 1] = arguments[_key];\n\t }\n\t\n\t _interceptors[command].forEach(function (action) {\n\t return action.apply(undefined, args);\n\t });\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Validates if the key is not empty.\n\t * (null, undefined or empty string)\n\t * @param {string} key: keyname of the storage\n\t * @return {void}\n\t */\n\tfunction checkEmpty(key) {\n\t if (key == null || key === '') {\n\t throw new Error('The key provided can not be empty');\n\t }\n\t}\n\t\n\t/**\n\t * @public\n\t *\n\t * Implementation of the Web Storage interface.\n\t * It saves and retrieves values as JSON.\n\t *\n\t * @Reference\n\t * https://developer.mozilla.org/en-US/docs/Web/API/Storage\n\t *\n\t * @type {Class}\n\t */\n\t\n\tvar WebStorage = function () {\n\t /**\n\t * Creates an instance of WebStorage.\n\t *\n\t * @param {string} storageType: it can be \"localStorage\", \"sessionStorage\", \"cookieStorage\", or \"memoryStorage\"\n\t *\n\t * @memberOf WebStorage\n\t */\n\t function WebStorage(storageType) {\n\t var _this = this;\n\t\n\t _classCallCheck(this, WebStorage);\n\t\n\t if (!_proxy.hasOwnProperty(storageType)) {\n\t throw new Error('Storage type \"' + storageType + '\" is not valid');\n\t }\n\t setProperty(this, '__storage__', storageType);\n\t // copies all existing elements in the storage\n\t Object.keys(_proxy[storageType]).forEach(function (key) {\n\t var value = _proxy[storageType][key];\n\t try {\n\t _this[key] = JSON.parse(value);\n\t } catch (e) {\n\t _this[key] = value;\n\t }\n\t });\n\t }\n\t /**\n\t * Stores a value given a key name.\n\t *\n\t * @param {string} key: keyname of the storage\n\t * @param {any} value: data to save in the storage\n\t * @param {object} options: additional options for cookieStorage\n\t * @return {void}\n\t *\n\t * @memberOf WebStorage\n\t */\n\t\n\t\n\t _createClass(WebStorage, [{\n\t key: 'setItem',\n\t value: function setItem(key, value, options) {\n\t checkEmpty(key);\n\t this[key] = value;\n\t executeInterceptors('setItem', key, value, options);\n\t value = JSON.stringify(value);\n\t _proxy[this.__storage__].setItem(key, value, options);\n\t }\n\t /**\n\t * Retrieves a value by its key name.\n\t *\n\t * @param {string} key: keyname of the storage\n\t * @return {void}\n\t *\n\t * @memberOf WebStorage\n\t */\n\t\n\t }, {\n\t key: 'getItem',\n\t value: function getItem(key) {\n\t checkEmpty(key);\n\t executeInterceptors('getItem', key);\n\t var value = _proxy[this.__storage__].getItem(key);\n\t return JSON.parse(value);\n\t }\n\t /**\n\t * Deletes a key from the storage.\n\t *\n\t * @param {string} key: keyname of the storage\n\t * @return {void}\n\t *\n\t * @memberOf WebStorage\n\t */\n\t\n\t }, {\n\t key: 'removeItem',\n\t value: function removeItem(key) {\n\t checkEmpty(key);\n\t delete this[key];\n\t executeInterceptors('removeItem', key);\n\t _proxy[this.__storage__].removeItem(key);\n\t }\n\t /**\n\t * Removes all keys from the storage.\n\t *\n\t * @return {void}\n\t *\n\t * @memberOf WebStorage\n\t */\n\t\n\t }, {\n\t key: 'clear',\n\t value: function clear() {\n\t var _this2 = this;\n\t\n\t executeInterceptors('clear');\n\t Object.keys(this).forEach(function (key) {\n\t return delete _this2[key];\n\t });\n\t _proxy[this.__storage__].clear();\n\t }\n\t /**\n\t * Gets the number of data items stored in the Storage object.\n\t *\n\t * @readonly\n\t *\n\t * @memberOf WebStorage\n\t */\n\t\n\t }, {\n\t key: 'length',\n\t get: function get() {\n\t return Object.keys(this).length;\n\t }\n\t /**\n\t * Adds an interceptor to a WebStorage method\n\t *\n\t * @param {string} command: name of the API method to intercept\n\t * @param {function} action: callback executed when the API method is called\n\t * @return {void}\n\t *\n\t * @memberOf WebStorage\n\t */\n\t\n\t }], [{\n\t key: 'interceptors',\n\t value: function interceptors(command, action) {\n\t if (command in _interceptors && typeof action === 'function') _interceptors[command].push(action);\n\t }\n\t }]);\n\t\n\t return WebStorage;\n\t}();\n\t\n\t/**\n\t * @public\n\t *\n\t * Determines which storage mechanisms are available.\n\t *\n\t * @type {object}\n\t */\n\t\n\t\n\tvar isAvaliable = {\n\t localStorage: false,\n\t sessionStorage: false,\n\t cookieStorage: false,\n\t memoryStorage: true\n\t};\n\t\n\t/**\n\t * @public\n\t *\n\t * Current storage mechanism.\n\t * @type {object}\n\t */\n\tvar storage = null;\n\t\n\t/**\n\t * @public\n\t *\n\t * Get/Set the storage mechanism to use by default.\n\t * @type {object}\n\t */\n\tvar configStorage = {\n\t get: function get() {\n\t return storage.__storage__;\n\t },\n\t\n\t\n\t /**\n\t * Sets the storage mechanism to use by default.\n\t * @param {string} storageType: it can be \"localStorage\", \"sessionStorage\", \"cookieStorage\", or \"memoryStorage\"\n\t * @return {void}\n\t */\n\t set: function set(storageType) {\n\t if (!_proxy.hasOwnProperty(storageType)) {\n\t throw new Error('Storage type \"' + storageType + '\" is not valid');\n\t }\n\t exports.default = storage = new WebStorage(storageType);\n\t }\n\t};\n\t\n\t/**\n\t * @private\n\t *\n\t * Determines whether a value is a plain object\n\t *\n\t * @param {any} value: the object to test\n\t * @return {boolean}\n\t */\n\tfunction isObject(value) {\n\t return Object.prototype.toString.call(value) === '[object Object]';\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Allows add or subtract timestamps to the current date or to a specific date.\n\t * @param {object} options: It contains the timestamps to add or remove to the date, and have the following properties:\n\t * - {Date} date: if provided, the timestamps will affect this date, otherwise a new current date will be used.\n\t * - {number} hours: hours to add/subtract\n\t * - {number} days: days to add/subtract\n\t * - {number} months: months to add/subtract\n\t * - {number} years: years to add/subtract\n\t * @return {Date}\n\t */\n\tfunction setTimestamp() {\n\t var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};\n\t\n\t var opt = Object.assign({}, options);\n\t var d = opt.date instanceof Date ? opt.date : new Date();\n\t if (+opt.hours) d.setHours(d.getHours() + opt.hours);\n\t if (+opt.days) d.setDate(d.getDate() + opt.days);\n\t if (+opt.months) d.setMonth(d.getMonth() + opt.months);\n\t if (+opt.years) d.setFullYear(d.getFullYear() + opt.years);\n\t return d;\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Builds the expiration part for the cookie\n\t *\n\t * @param {Date|object} date: the expiration date. See `setTimestamp(options)`\n\t * @return {string}\n\t */\n\tfunction buildExpirationString(date) {\n\t var expires = date instanceof Date ? setTimestamp({ date: date }) : setTimestamp(date);\n\t return '; expires=' + expires.toUTCString();\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Callback that finds an element in the array.\n\t * @param {string} cookie: key=value\n\t * @return {boolean}\n\t */\n\tfunction findCookie(cookie) {\n\t var nameEQ = this.toString();\n\t // prevent leading spaces before the key\n\t return cookie.trim().indexOf(nameEQ) === 0;\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Manages actions for creation/reading/deleting cookies.\n\t * Implements Web Storage interface methods.\n\t *\n\t * @return {object}\n\t */\n\tfunction cookieStorage() {\n\t var api = {\n\t setItem: function setItem(key, value, options) {\n\t var expires = '',\n\t cookie = void 0;\n\t options = Object.assign({ path: '/' }, options);\n\t if (isObject(options.expires) || options.expires instanceof Date) {\n\t expires = buildExpirationString(options.expires);\n\t }\n\t cookie = key + '=' + encodeURIComponent(value) + expires + '; path=' + options.path;\n\t $cookie.set(cookie);\n\t },\n\t getItem: function getItem(key) {\n\t var value = void 0;\n\t var nameEQ = key + '=';\n\t var cookie = $cookie.get().split(';').find(findCookie, nameEQ);\n\t if (cookie) {\n\t // prevent leading spaces before the key name\n\t value = cookie.trim().substring(nameEQ.length, cookie.length);\n\t value = decodeURIComponent(value);\n\t }\n\t return value;\n\t },\n\t removeItem: function removeItem(key) {\n\t api.setItem(key, '', { expires: { days: -1 } });\n\t },\n\t clear: function clear() {\n\t var eq = '=';\n\t var indexEQ = void 0,\n\t key = void 0;\n\t $cookie.get().split(';').forEach(function (cookie) {\n\t indexEQ = cookie.indexOf(eq);\n\t if (indexEQ > -1) {\n\t key = cookie.substring(0, indexEQ);\n\t // prevent leading spaces before the key\n\t api.removeItem(key.trim());\n\t }\n\t });\n\t },\n\t initialize: function initialize() {\n\t $cookie.get().split(';').forEach(function (cookie) {\n\t var index = cookie.indexOf('=');\n\t var key = cookie.substring(0, index).trim();\n\t var value = cookie.substring(index + 1).trim();\n\t api[key] = decodeURIComponent(value);\n\t });\n\t }\n\t };\n\t return api;\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Manages actions for creation/reading/deleting data in memory.\n\t * Implements Web Storage interface methods.\n\t * It also adds a hack to persist the store as a session in the current window.\n\t *\n\t * @return {object}\n\t */\n\tfunction memoryStorage() {\n\t var hashtable = getStoreFromWindow();\n\t var api = {\n\t setItem: function setItem(key, value) {\n\t hashtable[key] = value;\n\t setStoreToWindow(hashtable);\n\t },\n\t getItem: function getItem(key) {\n\t return hashtable[key];\n\t },\n\t removeItem: function removeItem(key) {\n\t delete hashtable[key];\n\t setStoreToWindow(hashtable);\n\t },\n\t clear: function clear() {\n\t Object.keys(hashtable).forEach(function (key) {\n\t return delete hashtable[key];\n\t });\n\t setStoreToWindow(hashtable);\n\t },\n\t initialize: function initialize() {\n\t Object.assign(api, hashtable);\n\t }\n\t };\n\t return api;\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Gets the hashtable-store from the current window.\n\t * @return {object}\n\t */\n\tfunction getStoreFromWindow() {\n\t try {\n\t var store = JSON.parse(window.self.name);\n\t if (store && (typeof store === 'undefined' ? 'undefined' : _typeof(store)) === 'object') return store;\n\t } catch (e) {\n\t return {};\n\t }\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Saves the hashtable-store in the current window.\n\t * @param {object} hashtable: {key,value} pairs stored in memoryStorage\n\t * @return {void}\n\t */\n\tfunction setStoreToWindow(hashtable) {\n\t var store = JSON.stringify(hashtable);\n\t window.self.name = store;\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Checks whether a storage mechanism is available.\n\t * @param {string} storageType: it can be \"localStorage\", \"sessionStorage\", \"cookieStorage\", or \"memoryStorage\"\n\t * @return {boolean}\n\t */\n\tfunction isStorageAvailable(storageType) {\n\t var storageObj = _proxy[storageType];\n\t var data = '__proxy-storage__';\n\t try {\n\t storageObj.setItem(data, data);\n\t storageObj.removeItem(data);\n\t return true;\n\t } catch (e) {\n\t return false;\n\t }\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Sets the default storage mechanism available.\n\t * @param {string} storageType: it can be \"localStorage\", \"sessionStorage\", \"cookieStorage\", or \"memoryStorage\"\n\t * @return {boolean}\n\t */\n\tfunction storageAvaliable(storageType) {\n\t if (isAvaliable[storageType]) {\n\t configStorage.set(storageType);\n\t }\n\t return isAvaliable[storageType];\n\t}\n\t\n\t/**\n\t * @private\n\t *\n\t * Initializes the module.\n\t * @return {void}\n\t */\n\tfunction init() {\n\t isAvaliable.localStorage = isStorageAvailable('localStorage');\n\t isAvaliable.sessionStorage = isStorageAvailable('sessionStorage');\n\t isAvaliable.cookieStorage = isStorageAvailable('cookieStorage');\n\t // sets the default storage mechanism available\n\t Object.keys(isAvaliable).some(storageAvaliable);\n\t}\n\t\n\tinit();\n\t\n\t// @public API\n\texports.default = storage;\n\texports.WebStorage = WebStorage;\n\texports.configStorage = configStorage;\n\texports.isAvaliable = isAvaliable;\n\n/***/ }\n/******/ ])\n});\n;\n\n\n// WEBPACK FOOTER //\n// proxy-storage.min.js"," \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId])\n \t\t\treturn installedModules[moduleId].exports;\n\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\texports: {},\n \t\t\tid: moduleId,\n \t\t\tloaded: false\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.loaded = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(0);\n\n\n\n// WEBPACK FOOTER //\n// webpack/bootstrap c9b7043ba837b106a41c","/* eslint-disable no-use-before-define, no-invalid-this */\n\n/**\n * This library uses an adapter that implements the Web Storage interface,\n * which is very useful to deal with the lack of compatibility between\n * document.cookie and localStorage and sessionStorage.\n *\n * It also provides a memoryStorage fallback that stores the data in memory\n * when all of above mechanisms are not available.\n *\n * Author: David Rivera\n * Github: https://github.com/jherax\n * License: \"MIT\"\n *\n * You can fork this project on github:\n * https://github.com/jherax/proxy-storage.git\n */\n\n// If you want to support all ES6 features, uncomment the next line\n// import 'babel-polyfill';\n\n/**\n * @private\n *\n * Proxy for the default cookie storage associated with the current document.\n *\n * @Reference\n * https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie\n *\n * @type {object}\n */\nconst $cookie = {\n get: () => document.cookie,\n set: (value) => {\n document.cookie = value;\n },\n};\n\n/**\n * @private\n *\n * Proxy for Web Storage and Cookies.\n * All members implement the Web Storage interface.\n *\n * @Reference\n * https://developer.mozilla.org/en-US/docs/Web/API/Storage\n *\n * @type {object}\n */\nconst _proxy = {\n localStorage: window.localStorage,\n sessionStorage: window.sessionStorage,\n cookieStorage: initApi(cookieStorage()),\n memoryStorage: initApi(memoryStorage()),\n};\n\n/**\n * @private\n *\n * Adds the current elements in the storage object\n *\n * @param {object} api: the API to initialize\n * @return {object}\n */\nfunction initApi(api) {\n // sets read-only and non-enumerable properties\n for (let prop in api) { // eslint-disable-line\n if (prop !== 'initialize')\n setProperty(api, prop);\n }\n api.initialize();\n // this method is removed after being invoked\n // because is not part of the Web Storage interface\n delete api.initialize;\n return api;\n}\n\n/**\n * @private\n *\n * Creates a non-enumerable read-only property.\n *\n * @param {object} obj: the object to add the property\n * @param {string} name: the name of the property\n * @param {any} value: the value of the property\n * @return {void}\n */\nfunction setProperty(obj, name, value) {\n let descriptor = {\n configurable: false,\n enumerable: false,\n writable: false,\n };\n if (typeof value !== 'undefined') {\n descriptor.value = value;\n }\n Object.defineProperty(obj, name, descriptor);\n}\n\n/**\n * @private\n *\n * Stores the interceptors for WebStorage methods\n *\n * @type {object}\n */\nconst _interceptors = {\n setItem: [],\n getItem: [],\n removeItem: [],\n clear: [],\n};\n\n/**\n * @private\n *\n * Executes the interceptors of a WebStorage method\n *\n * @param {string} command: name of the method to intercept\n * @return {void}\n */\nfunction executeInterceptors(command, ...args) {\n _interceptors[command].forEach((action) => action(...args));\n}\n\n/**\n * @private\n *\n * Validates if the key is not empty.\n * (null, undefined or empty string)\n * @param {string} key: keyname of the storage\n * @return {void}\n */\nfunction checkEmpty(key) {\n if (key == null || key === '') {\n throw new Error('The key provided can not be empty');\n }\n}\n\n/**\n * @public\n *\n * Implementation of the Web Storage interface.\n * It saves and retrieves values as JSON.\n *\n * @Reference\n * https://developer.mozilla.org/en-US/docs/Web/API/Storage\n *\n * @type {Class}\n */\nclass WebStorage {\n /**\n * Creates an instance of WebStorage.\n *\n * @param {string} storageType: it can be \"localStorage\", \"sessionStorage\", \"cookieStorage\", or \"memoryStorage\"\n *\n * @memberOf WebStorage\n */\n constructor(storageType) {\n if (!_proxy.hasOwnProperty(storageType)) {\n throw new Error(`Storage type \"${storageType}\" is not valid`);\n }\n setProperty(this, '__storage__', storageType);\n // copies all existing elements in the storage\n Object.keys(_proxy[storageType]).forEach((key) => {\n let value = _proxy[storageType][key];\n try {\n this[key] = JSON.parse(value);\n } catch (e) {\n this[key] = value;\n }\n });\n }\n /**\n * Stores a value given a key name.\n *\n * @param {string} key: keyname of the storage\n * @param {any} value: data to save in the storage\n * @param {object} options: additional options for cookieStorage\n * @return {void}\n *\n * @memberOf WebStorage\n */\n setItem(key, value, options) {\n checkEmpty(key);\n this[key] = value;\n executeInterceptors('setItem', key, value, options);\n value = JSON.stringify(value);\n _proxy[this.__storage__].setItem(key, value, options);\n }\n /**\n * Retrieves a value by its key name.\n *\n * @param {string} key: keyname of the storage\n * @return {void}\n *\n * @memberOf WebStorage\n */\n getItem(key) {\n checkEmpty(key);\n executeInterceptors('getItem', key);\n const value = _proxy[this.__storage__].getItem(key);\n return JSON.parse(value);\n }\n /**\n * Deletes a key from the storage.\n *\n * @param {string} key: keyname of the storage\n * @return {void}\n *\n * @memberOf WebStorage\n */\n removeItem(key) {\n checkEmpty(key);\n delete this[key];\n executeInterceptors('removeItem', key);\n _proxy[this.__storage__].removeItem(key);\n }\n /**\n * Removes all keys from the storage.\n *\n * @return {void}\n *\n * @memberOf WebStorage\n */\n clear() {\n executeInterceptors('clear');\n Object.keys(this).forEach((key) => delete this[key]);\n _proxy[this.__storage__].clear();\n }\n /**\n * Gets the number of data items stored in the Storage object.\n *\n * @readonly\n *\n * @memberOf WebStorage\n */\n get length() {\n return Object.keys(this).length;\n }\n /**\n * Adds an interceptor to a WebStorage method\n *\n * @param {string} command: name of the API method to intercept\n * @param {function} action: callback executed when the API method is called\n * @return {void}\n *\n * @memberOf WebStorage\n */\n static interceptors(command, action) {\n if (command in _interceptors && typeof action === 'function')\n _interceptors[command].push(action);\n }\n}\n\n/**\n * @public\n *\n * Determines which storage mechanisms are available.\n *\n * @type {object}\n */\nconst isAvaliable = {\n localStorage: false,\n sessionStorage: false,\n cookieStorage: false,\n memoryStorage: true,\n};\n\n/**\n * @public\n *\n * Current storage mechanism.\n * @type {object}\n */\nlet storage = null;\n\n/**\n * @public\n *\n * Get/Set the storage mechanism to use by default.\n * @type {object}\n */\nconst configStorage = {\n get() {\n return storage.__storage__;\n },\n\n /**\n * Sets the storage mechanism to use by default.\n * @param {string} storageType: it can be \"localStorage\", \"sessionStorage\", \"cookieStorage\", or \"memoryStorage\"\n * @return {void}\n */\n set(storageType) {\n if (!_proxy.hasOwnProperty(storageType)) {\n throw new Error(`Storage type \"${storageType}\" is not valid`);\n }\n storage = new WebStorage(storageType);\n },\n};\n\n/**\n * @private\n *\n * Determines whether a value is a plain object\n *\n * @param {any} value: the object to test\n * @return {boolean}\n */\nfunction isObject(value) {\n return Object.prototype.toString.call(value) === '[object Object]';\n}\n\n/**\n * @private\n *\n * Allows add or subtract timestamps to the current date or to a specific date.\n * @param {object} options: It contains the timestamps to add or remove to the date, and have the following properties:\n * - {Date} date: if provided, the timestamps will affect this date, otherwise a new current date will be used.\n * - {number} hours: hours to add/subtract\n * - {number} days: days to add/subtract\n * - {number} months: months to add/subtract\n * - {number} years: years to add/subtract\n * @return {Date}\n */\nfunction setTimestamp(options = {}) {\n const opt = Object.assign({}, options);\n let d = opt.date instanceof Date ? opt.date : new Date();\n if (+opt.hours) d.setHours(d.getHours() + opt.hours);\n if (+opt.days) d.setDate(d.getDate() + opt.days);\n if (+opt.months) d.setMonth(d.getMonth() + opt.months);\n if (+opt.years) d.setFullYear(d.getFullYear() + opt.years);\n return d;\n}\n\n/**\n * @private\n *\n * Builds the expiration part for the cookie\n *\n * @param {Date|object} date: the expiration date. See `setTimestamp(options)`\n * @return {string}\n */\nfunction buildExpirationString(date) {\n let expires = (date instanceof Date ?\n setTimestamp({date}) :\n setTimestamp(date)\n );\n return `; expires=${expires.toUTCString()}`;\n}\n\n/**\n * @private\n *\n * Callback that finds an element in the array.\n * @param {string} cookie: key=value\n * @return {boolean}\n */\nfunction findCookie(cookie) {\n const nameEQ = this.toString();\n // prevent leading spaces before the key\n return cookie.trim().indexOf(nameEQ) === 0;\n}\n\n/**\n * @private\n *\n * Manages actions for creation/reading/deleting cookies.\n * Implements Web Storage interface methods.\n *\n * @return {object}\n */\nfunction cookieStorage() {\n const api = {\n setItem(key, value, options) {\n let expires = '', cookie;\n options = Object.assign({path: '/'}, options);\n if (isObject(options.expires) || options.expires instanceof Date) {\n expires = buildExpirationString(options.expires);\n }\n cookie = `${key}=${encodeURIComponent(value)}${expires}; path=${options.path}`;\n $cookie.set(cookie);\n },\n\n getItem(key) {\n let value = void 0;\n const nameEQ = `${key}=`;\n const cookie = $cookie.get().split(';').find(findCookie, nameEQ);\n if (cookie) {\n // prevent leading spaces before the key name\n value = cookie.trim().substring(nameEQ.length, cookie.length);\n value = decodeURIComponent(value);\n }\n return value;\n },\n\n removeItem(key) {\n api.setItem(key, '', {expires: {days: -1}});\n },\n\n clear() {\n const eq = '=';\n let indexEQ, key;\n $cookie.get().split(';').forEach((cookie) => {\n indexEQ = cookie.indexOf(eq);\n if (indexEQ > -1) {\n key = cookie.substring(0, indexEQ);\n // prevent leading spaces before the key\n api.removeItem(key.trim());\n }\n });\n },\n\n initialize() {\n $cookie.get().split(';').forEach((cookie) => {\n const index = cookie.indexOf('=');\n const key = cookie.substring(0, index).trim();\n const value = cookie.substring(index + 1).trim();\n api[key] = decodeURIComponent(value);\n });\n },\n };\n return api;\n}\n\n/**\n * @private\n *\n * Manages actions for creation/reading/deleting data in memory.\n * Implements Web Storage interface methods.\n * It also adds a hack to persist the store as a session in the current window.\n *\n * @return {object}\n */\nfunction memoryStorage() {\n const hashtable = getStoreFromWindow();\n const api = {\n setItem(key, value) {\n hashtable[key] = value;\n setStoreToWindow(hashtable);\n },\n getItem(key) {\n return hashtable[key];\n },\n removeItem(key) {\n delete hashtable[key];\n setStoreToWindow(hashtable);\n },\n clear() {\n Object.keys(hashtable).forEach((key) => delete hashtable[key]);\n setStoreToWindow(hashtable);\n },\n initialize() {\n Object.assign(api, hashtable);\n },\n };\n return api;\n}\n\n/**\n * @private\n *\n * Gets the hashtable-store from the current window.\n * @return {object}\n */\nfunction getStoreFromWindow() {\n try {\n const store = JSON.parse(window.self.name);\n if (store && typeof store === 'object') return store;\n } catch (e) {\n return {};\n }\n}\n\n/**\n * @private\n *\n * Saves the hashtable-store in the current window.\n * @param {object} hashtable: {key,value} pairs stored in memoryStorage\n * @return {void}\n */\nfunction setStoreToWindow(hashtable) {\n const store = JSON.stringify(hashtable);\n window.self.name = store;\n}\n\n/**\n * @private\n *\n * Checks whether a storage mechanism is available.\n * @param {string} storageType: it can be \"localStorage\", \"sessionStorage\", \"cookieStorage\", or \"memoryStorage\"\n * @return {boolean}\n */\nfunction isStorageAvailable(storageType) {\n const storageObj = _proxy[storageType];\n const data = '__proxy-storage__';\n try {\n storageObj.setItem(data, data);\n storageObj.removeItem(data);\n return true;\n } catch (e) {\n return false;\n }\n}\n\n/**\n * @private\n *\n * Sets the default storage mechanism available.\n * @param {string} storageType: it can be \"localStorage\", \"sessionStorage\", \"cookieStorage\", or \"memoryStorage\"\n * @return {boolean}\n */\nfunction storageAvaliable(storageType) {\n if (isAvaliable[storageType]) {\n configStorage.set(storageType);\n }\n return isAvaliable[storageType];\n}\n\n/**\n * @private\n *\n * Initializes the module.\n * @return {void}\n */\nfunction init() {\n isAvaliable.localStorage = isStorageAvailable('localStorage');\n isAvaliable.sessionStorage = isStorageAvailable('sessionStorage');\n isAvaliable.cookieStorage = isStorageAvailable('cookieStorage');\n // sets the default storage mechanism available\n Object.keys(isAvaliable).some(storageAvaliable);\n}\n\ninit();\n\n// @public API\nexport {storage as default, WebStorage, configStorage, isAvaliable};\n\n\n\n// WEBPACK FOOTER //\n// ./src/proxy-storage.js"],"sourceRoot":""} \ No newline at end of file diff --git a/package.json b/package.json index 675c7dc..d22d61f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "proxy-storage", "description": "Storage mechanism that implements the Web Storage interface", - "version": "1.0.0", + "version": "1.0.1", "author": "David Rivera ", "src": "src/proxy-storage.js", "main": "dist/proxy-storage.js", diff --git a/src/proxy-storage.js b/src/proxy-storage.js index bf86cfd..5bdfe5b 100644 --- a/src/proxy-storage.js +++ b/src/proxy-storage.js @@ -19,6 +19,23 @@ // If you want to support all ES6 features, uncomment the next line // import 'babel-polyfill'; +/** + * @private + * + * Proxy for the default cookie storage associated with the current document. + * + * @Reference + * https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie + * + * @type {object} + */ +const $cookie = { + get: () => document.cookie, + set: (value) => { + document.cookie = value; + }, +}; + /** * @private * @@ -33,10 +50,53 @@ const _proxy = { localStorage: window.localStorage, sessionStorage: window.sessionStorage, - cookieStorage: cookieStorage(), - memoryStorage: memoryStorage(), + cookieStorage: initApi(cookieStorage()), + memoryStorage: initApi(memoryStorage()), }; +/** + * @private + * + * Adds the current elements in the storage object + * + * @param {object} api: the API to initialize + * @return {object} + */ +function initApi(api) { + // sets read-only and non-enumerable properties + for (let prop in api) { // eslint-disable-line + if (prop !== 'initialize') + setProperty(api, prop); + } + api.initialize(); + // this method is removed after being invoked + // because is not part of the Web Storage interface + delete api.initialize; + return api; +} + +/** + * @private + * + * Creates a non-enumerable read-only property. + * + * @param {object} obj: the object to add the property + * @param {string} name: the name of the property + * @param {any} value: the value of the property + * @return {void} + */ +function setProperty(obj, name, value) { + let descriptor = { + configurable: false, + enumerable: false, + writable: false, + }; + if (typeof value !== 'undefined') { + descriptor.value = value; + } + Object.defineProperty(obj, name, descriptor); +} + /** * @private * @@ -52,6 +112,8 @@ const _interceptors = { }; /** + * @private + * * Executes the interceptors of a WebStorage method * * @param {string} command: name of the method to intercept @@ -75,25 +137,6 @@ function checkEmpty(key) { } } -/** - * @private - * - * Creates a non-enumerable read-only property. - * - * @param {object} obj: the object to add the property - * @param {string} name: the name of the property - * @param {any} value: the value of the property - * @return {void} - */ -function setProperty(obj, name, value) { - Object.defineProperty(obj, name, { - configurable: false, - enumerable: false, - writable: false, - value: value, - }); -} - /** * @public * @@ -117,7 +160,16 @@ class WebStorage { if (!_proxy.hasOwnProperty(storageType)) { throw new Error(`Storage type "${storageType}" is not valid`); } - setProperty(this, '__storage', _proxy[storageType]); + setProperty(this, '__storage__', storageType); + // copies all existing elements in the storage + Object.keys(_proxy[storageType]).forEach((key) => { + let value = _proxy[storageType][key]; + try { + this[key] = JSON.parse(value); + } catch (e) { + this[key] = value; + } + }); } /** * Stores a value given a key name. @@ -134,7 +186,7 @@ class WebStorage { this[key] = value; executeInterceptors('setItem', key, value, options); value = JSON.stringify(value); - this.__storage.setItem(key, value, options); + _proxy[this.__storage__].setItem(key, value, options); } /** * Retrieves a value by its key name. @@ -147,8 +199,8 @@ class WebStorage { getItem(key) { checkEmpty(key); executeInterceptors('getItem', key); - const value = JSON.parse(this.__storage.getItem(key)); - return value; + const value = _proxy[this.__storage__].getItem(key); + return JSON.parse(value); } /** * Deletes a key from the storage. @@ -162,7 +214,7 @@ class WebStorage { checkEmpty(key); delete this[key]; executeInterceptors('removeItem', key); - this.__storage.removeItem(key); + _proxy[this.__storage__].removeItem(key); } /** * Removes all keys from the storage. @@ -174,7 +226,7 @@ class WebStorage { clear() { executeInterceptors('clear'); Object.keys(this).forEach((key) => delete this[key]); - this.__storage.clear(); + _proxy[this.__storage__].clear(); } /** * Gets the number of data items stored in the Storage object. @@ -223,14 +275,6 @@ const isAvaliable = { */ let storage = null; -/** - * @private - * - * Current storage type - * @type {string} - */ -let _currentStorageName = null; - /** * @public * @@ -239,7 +283,7 @@ let _currentStorageName = null; */ const configStorage = { get() { - return _currentStorageName; + return storage.__storage__; }, /** @@ -252,7 +296,6 @@ const configStorage = { throw new Error(`Storage type "${storageType}" is not valid`); } storage = new WebStorage(storageType); - _currentStorageName = storageType; }, }; @@ -309,19 +352,15 @@ function buildExpirationString(date) { /** * @private * - * Alias for the default cookie storage associated with the current document. - * - * @Reference - * https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie - * - * @type {object} + * Callback that finds an element in the array. + * @param {string} cookie: key=value + * @return {boolean} */ -const $cookie = { - get: () => document.cookie, - set: (value) => { - document.cookie = value; - }, -}; +function findCookie(cookie) { + const nameEQ = this.toString(); + // prevent leading spaces before the key + return cookie.trim().indexOf(nameEQ) === 0; +} /** * @private @@ -344,7 +383,7 @@ function cookieStorage() { }, getItem(key) { - let value = null; + let value = void 0; const nameEQ = `${key}=`; const cookie = $cookie.get().split(';').find(findCookie, nameEQ); if (cookie) { @@ -371,35 +410,19 @@ function cookieStorage() { } }); }, + + initialize() { + $cookie.get().split(';').forEach((cookie) => { + const index = cookie.indexOf('='); + const key = cookie.substring(0, index).trim(); + const value = cookie.substring(index + 1).trim(); + api[key] = decodeURIComponent(value); + }); + }, }; return api; } -/** - * @private - * - * Callback that finds an element in the array. - * @param {string} cookie: key=value - * @return {boolean} - */ -function findCookie(cookie) { - const nameEQ = this.toString(); - // prevent leading spaces before the key - return cookie.trim().indexOf(nameEQ) === 0; -} - -/** - * @private - * - * Callback that finds an element in the array. - * @param {object} item: {key, value} - * @return {boolean} - */ -function findItem(item) { - const key = this.toString(); - return item.key === key; -} - /** * @private * @@ -413,25 +436,23 @@ function memoryStorage() { const hashtable = getStoreFromWindow(); const api = { setItem(key, value) { - const item = hashtable.find(findItem, key); - if (item) item.value = value; - else hashtable.push({key, value}); + hashtable[key] = value; setStoreToWindow(hashtable); }, getItem(key) { - const item = hashtable.find(findItem, key); - if (item) return item.value; - return null; + return hashtable[key]; }, removeItem(key) { - const index = hashtable.findIndex(findItem, key); - if (index > -1) hashtable.splice(index, 1); + delete hashtable[key]; setStoreToWindow(hashtable); }, clear() { - hashtable.length = 0; + Object.keys(hashtable).forEach((key) => delete hashtable[key]); setStoreToWindow(hashtable); }, + initialize() { + Object.assign(api, hashtable); + }, }; return api; } @@ -439,22 +460,23 @@ function memoryStorage() { /** * @private * - * Gets the hashtable store from the current window. - * @return {array} + * Gets the hashtable-store from the current window. + * @return {object} */ function getStoreFromWindow() { try { const store = JSON.parse(window.self.name); - if (store instanceof Array) return store; - } catch (e) {} // eslint-disable-line - return []; /* {key,value} */ + if (store && typeof store === 'object') return store; + } catch (e) { + return {}; + } } /** * @private * - * Saves the hashtable store in the current window. - * @param {array} hashtable: list of objects stored in memoryStorage + * Saves the hashtable-store in the current window. + * @param {object} hashtable: {key,value} pairs stored in memoryStorage * @return {void} */ function setStoreToWindow(hashtable) {