PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/js-cookie/2.0.3/js.cookie.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 139 lines | 114 code | 13 blank | 12 comment | 16 complexity | 526dbfca43b862f28743f01d63c75140 MD5 | raw file
  1. /*!
  2. * JavaScript Cookie v2.0.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. if (typeof define === 'function' && define.amd) {
  10. define(factory);
  11. } else if (typeof exports === 'object') {
  12. module.exports = factory();
  13. } else {
  14. var _OldCookies = window.Cookies;
  15. var api = window.Cookies = factory(window.jQuery);
  16. api.noConflict = function () {
  17. window.Cookies = _OldCookies;
  18. return api;
  19. };
  20. }
  21. }(function () {
  22. function extend () {
  23. var i = 0;
  24. var result = {};
  25. for (; i < arguments.length; i++) {
  26. var attributes = arguments[ i ];
  27. for (var key in attributes) {
  28. result[key] = attributes[key];
  29. }
  30. }
  31. return result;
  32. }
  33. function init (converter) {
  34. function api (key, value, attributes) {
  35. var result;
  36. // Write
  37. if (arguments.length > 1) {
  38. attributes = extend({
  39. path: '/'
  40. }, api.defaults, attributes);
  41. if (typeof attributes.expires === 'number') {
  42. var expires = new Date();
  43. expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
  44. attributes.expires = expires;
  45. }
  46. try {
  47. result = JSON.stringify(value);
  48. if (/^[\{\[]/.test(result)) {
  49. value = result;
  50. }
  51. } catch (e) {}
  52. value = encodeURIComponent(String(value));
  53. value = value.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
  54. key = encodeURIComponent(String(key));
  55. key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent);
  56. key = key.replace(/[\(\)]/g, escape);
  57. return (document.cookie = [
  58. key, '=', value,
  59. attributes.expires && '; expires=' + attributes.expires.toUTCString(), // use expires attribute, max-age is not supported by IE
  60. attributes.path && '; path=' + attributes.path,
  61. attributes.domain && '; domain=' + attributes.domain,
  62. attributes.secure ? '; secure' : ''
  63. ].join(''));
  64. }
  65. // Read
  66. if (!key) {
  67. result = {};
  68. }
  69. // To prevent the for loop in the first place assign an empty array
  70. // in case there are no cookies at all. Also prevents odd result when
  71. // calling "get()"
  72. var cookies = document.cookie ? document.cookie.split('; ') : [];
  73. var rdecode = /(%[0-9A-Z]{2})+/g;
  74. var i = 0;
  75. for (; i < cookies.length; i++) {
  76. var parts = cookies[i].split('=');
  77. var name = parts[0].replace(rdecode, decodeURIComponent);
  78. var cookie = parts.slice(1).join('=');
  79. if (cookie.charAt(0) === '"') {
  80. cookie = cookie.slice(1, -1);
  81. }
  82. try {
  83. cookie = converter && converter(cookie, name) || cookie.replace(rdecode, decodeURIComponent);
  84. if (this.json) {
  85. try {
  86. cookie = JSON.parse(cookie);
  87. } catch (e) {}
  88. }
  89. if (key === name) {
  90. result = cookie;
  91. break;
  92. }
  93. if (!key) {
  94. result[name] = cookie;
  95. }
  96. } catch (e) {}
  97. }
  98. return result;
  99. }
  100. api.get = api.set = api;
  101. api.getJSON = function () {
  102. return api.apply({
  103. json: true
  104. }, [].slice.call(arguments));
  105. };
  106. api.defaults = {};
  107. api.remove = function (key, attributes) {
  108. api(key, '', extend(attributes, {
  109. expires: -1
  110. }));
  111. };
  112. api.withConverter = init;
  113. return api;
  114. }
  115. return init();
  116. }));