Advertisement
dimaslanjaka

js.cookie

Feb 25th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*!
  2.  * JavaScript Cookie v2.1.3
  3.  * https://github.com/js-cookie/js-cookie
  4.  *
  5.  * Copyright 2006, 2015 Klaus Hartl & Fagner Brack
  6.  * Released under the MIT license
  7.  */
  8. ;(function (factory) {
  9.     var registeredInModuleLoader = false;
  10.     if (typeof define === 'function' && define.amd) {
  11.         define(factory);
  12.         registeredInModuleLoader = true;
  13.     }
  14.     if (typeof exports === 'object') {
  15.         module.exports = factory();
  16.         registeredInModuleLoader = true;
  17.     }
  18.     if (!registeredInModuleLoader) {
  19.         var OldCookies = window.Cookies;
  20.         var api = window.Cookies = factory();
  21.         api.noConflict = function () {
  22.             window.Cookies = OldCookies;
  23.             return api;
  24.         };
  25.     }
  26. }(function () {
  27.     function extend () {
  28.         var i = 0;
  29.         var result = {};
  30.         for (; i < arguments.length; i++) {
  31.             var attributes = arguments[ i ];
  32.             for (var key in attributes) {
  33.                 result[key] = attributes[key];
  34.             }
  35.         }
  36.         return result;
  37.     }
  38.  
  39.     function init (converter) {
  40.         function api (key, value, attributes) {
  41.             var result;
  42.             if (typeof document === 'undefined') {
  43.                 return;
  44.             }
  45.  
  46.             // Write
  47.  
  48.             if (arguments.length > 1) {
  49.                 attributes = extend({
  50.                     path: '/'
  51.                 }, api.defaults, attributes);
  52.  
  53.                 if (typeof attributes.expires === 'number') {
  54.                     var expires = new Date();
  55.                     expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
  56.                     attributes.expires = expires;
  57.                 }
  58.  
  59.                 try {
  60.                     result = JSON.stringify(value);
  61.                     if (/^[\{\[]/.test(result)) {
  62.                         value = result;
  63.                     }
  64.                 } catch (e) {}
  65.  
  66.                 if (!converter.write) {
  67.                     value = encodeURIComponent(String(value))
  68.                         .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
  69.                 } else {
  70.                     value = converter.write(value, key);
  71.                 }
  72.  
  73.                 key = encodeURIComponent(String(key));
  74.                 key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent);
  75.                 key = key.replace(/[\(\)]/g, escape);
  76.  
  77.                 return (document.cookie = [
  78.                     key, '=', value,
  79.                     attributes.expires ? '; expires=' + attributes.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  80.                     attributes.path ? '; path=' + attributes.path : '',
  81.                     attributes.domain ? '; domain=' + attributes.domain : '',
  82.                     attributes.secure ? '; secure' : ''
  83.                 ].join(''));
  84.             }
  85.  
  86.             // Read
  87.  
  88.             if (!key) {
  89.                 result = {};
  90.             }
  91.  
  92.             // To prevent the for loop in the first place assign an empty array
  93.             // in case there are no cookies at all. Also prevents odd result when
  94.             // calling "get()"
  95.             var cookies = document.cookie ? document.cookie.split('; ') : [];
  96.             var rdecode = /(%[0-9A-Z]{2})+/g;
  97.             var i = 0;
  98.  
  99.             for (; i < cookies.length; i++) {
  100.                 var parts = cookies[i].split('=');
  101.                 var cookie = parts.slice(1).join('=');
  102.  
  103.                 if (cookie.charAt(0) === '"') {
  104.                     cookie = cookie.slice(1, -1);
  105.                 }
  106.  
  107.                 try {
  108.                     var name = parts[0].replace(rdecode, decodeURIComponent);
  109.                     cookie = converter.read ?
  110.                         converter.read(cookie, name) : converter(cookie, name) ||
  111.                         cookie.replace(rdecode, decodeURIComponent);
  112.  
  113.                     if (this.json) {
  114.                         try {
  115.                             cookie = JSON.parse(cookie);
  116.                         } catch (e) {}
  117.                     }
  118.  
  119.                     if (key === name) {
  120.                         result = cookie;
  121.                         break;
  122.                     }
  123.  
  124.                     if (!key) {
  125.                         result[name] = cookie;
  126.                     }
  127.                 } catch (e) {}
  128.             }
  129.  
  130.             return result;
  131.         }
  132.  
  133.         api.set = api;
  134.         api.get = function (key) {
  135.             return api.call(api, key);
  136.         };
  137.         api.getJSON = function () {
  138.             return api.apply({
  139.                 json: true
  140.             }, [].slice.call(arguments));
  141.         };
  142.         api.defaults = {};
  143.  
  144.         api.remove = function (key, attributes) {
  145.             api(key, '', extend(attributes, {
  146.                 expires: -1
  147.             }));
  148.         };
  149.  
  150.         api.withConverter = init;
  151.  
  152.         return api;
  153.     }
  154.  
  155.     return init(function () {});
  156. }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement