PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/cuckoo/web/static/js/js.cookie.js

https://github.com/cuckoobox/cuckoo
JavaScript | 151 lines | 126 code | 13 blank | 12 comment | 16 complexity | c0a1b1d096c0da87f236895fcd4fc8ee MD5 | raw file
  1. /*!
  2. * JavaScript Cookie v2.1.2
  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();
  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. if (typeof document === 'undefined') {
  37. return;
  38. }
  39. // Write
  40. if (arguments.length > 1) {
  41. attributes = extend({
  42. path: '/'
  43. }, api.defaults, attributes);
  44. if (typeof attributes.expires === 'number') {
  45. var expires = new Date();
  46. expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
  47. attributes.expires = expires;
  48. }
  49. try {
  50. result = JSON.stringify(value);
  51. if (/^[\{\[]/.test(result)) {
  52. value = result;
  53. }
  54. } catch (e) {}
  55. if (!converter.write) {
  56. value = encodeURIComponent(String(value))
  57. .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
  58. } else {
  59. value = converter.write(value, key);
  60. }
  61. key = encodeURIComponent(String(key));
  62. key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent);
  63. key = key.replace(/[\(\)]/g, escape);
  64. return (document.cookie = [
  65. key, '=', value,
  66. attributes.expires ? '; expires=' + attributes.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  67. attributes.path ? '; path=' + attributes.path : '',
  68. attributes.domain ? '; domain=' + attributes.domain : '',
  69. attributes.secure ? '; secure' : ''
  70. ].join(''));
  71. }
  72. // Read
  73. if (!key) {
  74. result = {};
  75. }
  76. // To prevent the for loop in the first place assign an empty array
  77. // in case there are no cookies at all. Also prevents odd result when
  78. // calling "get()"
  79. var cookies = document.cookie ? document.cookie.split('; ') : [];
  80. var rdecode = /(%[0-9A-Z]{2})+/g;
  81. var i = 0;
  82. for (; i < cookies.length; i++) {
  83. var parts = cookies[i].split('=');
  84. var cookie = parts.slice(1).join('=');
  85. if (cookie.charAt(0) === '"') {
  86. cookie = cookie.slice(1, -1);
  87. }
  88. try {
  89. var name = parts[0].replace(rdecode, decodeURIComponent);
  90. cookie = converter.read ?
  91. converter.read(cookie, name) : converter(cookie, name) ||
  92. cookie.replace(rdecode, decodeURIComponent);
  93. if (this.json) {
  94. try {
  95. cookie = JSON.parse(cookie);
  96. } catch (e) {}
  97. }
  98. if (key === name) {
  99. result = cookie;
  100. break;
  101. }
  102. if (!key) {
  103. result[name] = cookie;
  104. }
  105. } catch (e) {}
  106. }
  107. return result;
  108. }
  109. api.set = api;
  110. api.get = function (key) {
  111. return api(key);
  112. };
  113. api.getJSON = function () {
  114. return api.apply({
  115. json: true
  116. }, [].slice.call(arguments));
  117. };
  118. api.defaults = {};
  119. api.remove = function (key, attributes) {
  120. api(key, '', extend(attributes, {
  121. expires: -1
  122. }));
  123. };
  124. api.withConverter = init;
  125. return api;
  126. }
  127. return init(function () {});
  128. }));