{"version":3,"file":"cookie.js","sources":["../index.js"],"sourcesContent":["/**\r\n * @module cookie\r\n * @description node.js version of the cookie.\r\n * Cookies let you store user information in web pages.\r\n * @see {@link https://www.w3schools.com/js/js_cookies.asp}\r\n *\r\n * @author [Andrej Hristoliubov]{@link https://github.com/anhr}\r\n *\r\n * @copyright 2011 Data Arts Team, Google Creative Lab\r\n *\r\n * @license under the Apache License, Version 2.0 (the \"License\");\r\n * you may not use this file except in compliance with the License.\r\n * You may obtain a copy of the License at\r\n *\r\n * http://www.apache.org/licenses/LICENSE-2.0\r\n */\r\n\r\nexport { isEnabled, set, setObject, get, getObject, copyObject, remove, defaultCookie };\r\n\r\n/**\r\n * Is the cookie enabled in your web browser?\r\n * @returns {boolean} true if cookie enabled\r\n * \r\n * @example\r\n\tvar isCookieEnabled = cookie.isEnabled('age', 25);\r\n */\r\nfunction isEnabled() {\r\n\r\n\treturn navigator.cookieEnabled;\r\n\t//Enable cookie\r\n\t//Chrome: Settings/Show advanced settings.../Privacy/Content settings.../Cookies/Allow local data to be set\r\n\r\n}\r\n\r\n/**\r\n * Set a cookie.\r\n * @param {string} name cookie name.\r\n * @param {any} value cookie value.\r\n * @param {Date} [cookie_date] expiry date (in UTC time). Optional.\r\n * @example\r\n\tcookie.set('age', 25);\r\n */\r\nfunction set( name, value, cookie_date ) {\r\n\r\n\tif ( !isEnabled() ) {\r\n\r\n\t\tconsoleCookieEnabled();\r\n\t\treturn;\r\n\r\n\t}\r\n\tvalue = value.toString();\r\n\t//http://ruseller.com/lessons.php?rub=28&id=593\r\n\tif ( cookie_date === undefined ) {\r\n\r\n\t\tcookie_date = new Date(); // Curent date and time\r\n\t\tcookie_date.setTime( cookie_date.getTime() + 1000 * 60 * 60 * 24 * 365 );//expiry date is one year\r\n\r\n\t}\r\n\tdocument.cookie = name + \"=\" + value + ( ( typeof settings == 'undefined' ) ? '' : settings ) + \"; expires=\" + cookie_date.toGMTString();\r\n\tif ( document.cookie === '' )\r\n\t\tconsole.error( 'document.cookie is empty' );\r\n\r\n}\r\n\r\n/**\r\n * sets an object into cookie.\r\n * @param {string} name cookie name.\r\n * @param {any} object an object for saving into cookie.\r\n */\r\nfunction setObject( name, object ) {\r\n\r\n\tset( name, JSON.stringify( object ) );\r\n\r\n};\r\n\r\n/**\r\n * Get a cookie.\r\n * @param {string} name cookie name.\r\n * @param {any} [defaultValue] cookie default value. Optional.\r\n * @returns {string} cookie value or defaultValue if cookie was not found.\r\n * @example\r\n\tvar age = cookie.get('age', 25);\r\n */\r\nfunction get( name, defaultValue ) {\r\n\r\n\tif ( !isEnabled() ) {\r\n\r\n\t\tconsoleCookieEnabled();\r\n\t\treturn;\r\n\r\n\t}\r\n\t//http://ruseller.com/lessons.php?rub=28&id=593\r\n\tvar results = document.cookie.match( '(^|;) ?' + name + '=([^;]*)(;|$)' );\r\n\r\n\tif ( results ) {\r\n\t\t\r\n\t\tconst result = results[2], type = typeof defaultValue;\r\n\t\treturn type === \"number\" ?//number\r\n\t\t\t\tresult % 1 === 0 ? parseInt(result) : parseFloat(result) :\r\n\t\t\ttype === \"boolean\" ?//boolean\r\n\t\t\t\tresult === 'true' ? true : false :\r\n\t\t\tunescape( result );//string\r\n\r\n\t}\r\n\tif ( typeof defaultValue == 'undefined' )\r\n\t\treturn '';\r\n\treturn defaultValue;\r\n\r\n}\r\n\r\n/**\r\n * gets an object from cookie.\r\n * @param {string} name name of the object.\r\n * @param {Object} options load an object from cookie into options.\r\n * @param {Object} [optionsDefault=options] copy to options this default object if named object is not exists in the cookie.\r\n */\r\nfunction getObject( name, options, optionsDefault ) {\r\n\r\n\t//uncompatible with ND build\r\n\t//optionsDefault ||= options;\r\n\t\r\n\tif (optionsDefault === undefined) optionsDefault = options;\r\n\tnew defaultCookie().getObject( name, options, copyObject( name, optionsDefault ) );\r\n\r\n};\r\n\r\n/**\r\n * gets an object from cookie and returns a copy of object.\r\n * @param {string} name name of the object.\r\n * @param {Object} objectDefault copy to options this default object if named object is not exists in the cookie.\r\n * @returns copy of object from cookie.\r\n */\r\nfunction copyObject( name, objectDefault ) {\r\n\r\n\treturn JSON.parse( get( name, JSON.stringify( objectDefault ) ) );\r\n\r\n}\r\n\r\n/**\r\n * Remove cookie\r\n * @param {string} name cookie name.\r\n */\r\nfunction remove( name ) {\r\n\r\n\tif ( !isEnabled() ) {\r\n\r\n\t\tconsoleCookieEnabled();\r\n\t\treturn;\r\n\r\n\t}\r\n\t//http://ruseller.com/lessons.php?rub=28&id=593\r\n\tvar cookie_date = new Date();\r\n\tcookie_date.setTime( cookie_date.getTime() - 1 );\r\n\tdocument.cookie = name += \"=; expires=\" + cookie_date.toGMTString();\r\n\r\n}\r\n\r\nfunction consoleCookieEnabled() {\r\n\r\n\tconsole.error( 'navigator.cookieEnabled = ' + navigator.cookieEnabled );\r\n\r\n}\r\n\r\n/**\r\n * Default cookie is not saving settings\r\n * @param {any} name is not using\r\n */\r\nclass defaultCookie {\r\n\r\n\tconstructor(name) {\r\n\t\t/**\r\n\t\t * Default cookie is not loading settings\r\n\t\t * @param {any} defaultValue\r\n\t\t * @returns defaultValue\r\n\t\t */\r\n\t\tthis.get = function (defaultValue) {\r\n\r\n\t\t\treturn defaultValue;\r\n\r\n\t\t};\r\n\r\n\t\t/**\r\n\t\t * Default cookie is not saving settings\r\n\t\t */\r\n\t\tthis.set = function () {\r\n\r\n\t\t};\r\n\r\n\t\t/**\r\n\t\t * Default cookie is not loading objects\r\n\t\t * @param {string} name is not using\r\n\t\t * @param {any} options load an object from optionsDefault into options\r\n\t\t * @param {Object} optionsDefault source object\r\n\t\t */\r\n\t\tthis.getObject = function (name, options, optionsDefault) {\r\n\r\n\t\t\tif (!optionsDefault)\r\n\t\t\t\treturn;//object's settings is not saving\r\n\t\t\tObject.keys(optionsDefault).forEach(function (key) {\r\n\r\n\t\t\t\t//I cannot modify options[key] if optionsDefault is read only and options[key] is not copy of optionsDefault[key]\r\n\t\t\t\t//options[key] = optionsDefault[key];\r\n\t\t\t\t//copy key\r\n\t\t\t\tvar option = optionsDefault[key];\r\n\t\t\t\tif (\r\n\t\t\t\t\t(option !== undefined) &&\r\n\t\t\t\t\t(typeof option !== 'function') &&\r\n\t\t\t\t\t\r\n\t\t\t\t\t//Ключь со значением Infinity превращается в null во время JSON преобразования.\r\n\t\t\t\t\t//Для проверки надо установить options.playerOptions.interval = Infinity\r\n\t\t\t\t\t//Поэтому оставляем старое значение options[key] = Infinity\r\n\t\t\t\t\t(option != null)\r\n\t\t\t\t)\r\n\t\t\t\t\toptions[key] = JSON.parse(JSON.stringify(option));\r\n\r\n\t\t\t});\r\n\r\n\t\t};\r\n\r\n\t\t/**\r\n\t\t * copy and returns an object from objectDefault\r\n\t\t * @param {string} name is not using\r\n\t\t * @param {any} objectDefault source object\r\n\t\t */\r\n\t\tthis.copyObject = function (name, objectDefault) {\r\n\r\n\t\t\treturn JSON.parse(JSON.stringify(objectDefault));\r\n\r\n\t\t}\r\n\r\n\t\t/**\r\n\t\t * Default cookie is not saving object's settings\r\n\t\t */\r\n\t\tthis.setObject = function () {\r\n\r\n\t\t};\r\n\r\n\t\tthis.isTrue = function (defaultValue) {\r\n\r\n\t\t\treturn defaultValue;\r\n\r\n\t\t};\r\n\r\n\t}\r\n\r\n};\r\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;CAAA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AAGA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA,SAAS,SAAS,GAAG;AACrB;CACA,CAAC,OAAO,SAAS,CAAC,aAAa,CAAC;CAChC;CACA;AACA;CACA,CAAC;AACD;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA,SAAS,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,WAAW,GAAG;AACzC;CACA,CAAC,KAAK,CAAC,SAAS,EAAE,GAAG;AACrB;CACA,EAAE,oBAAoB,EAAE,CAAC;CACzB,EAAE,OAAO;AACT;CACA,EAA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