PageRenderTime 30ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs/Cookies.js/1.1.0/cookies.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 156 lines | 109 code | 32 blank | 15 comment | 24 complexity | 8113df93069fb8e2ee5ce996e75259b8 MD5 | raw file
  1. /*
  2. * Cookies.js - 1.1.0
  3. * https://github.com/ScottHamper/Cookies
  4. *
  5. * This is free and unencumbered software released into the public domain.
  6. */
  7. (function (global, undefined) {
  8. 'use strict';
  9. var factory = function (window) {
  10. if (typeof window.document !== 'object') {
  11. throw new Error('Cookies.js requires a `window` with a `document` object');
  12. }
  13. var Cookies = function (key, value, options) {
  14. return arguments.length === 1 ?
  15. Cookies.get(key) : Cookies.set(key, value, options);
  16. };
  17. // Allows for setter injection in unit tests
  18. Cookies._document = window.document;
  19. // Used to ensure cookie keys do not collide with
  20. // built-in `Object` properties
  21. Cookies._cacheKeyPrefix = 'cookey.'; // Hurr hurr, :)
  22. Cookies.defaults = {
  23. path: '/',
  24. secure: false
  25. };
  26. Cookies.get = function (key) {
  27. if (Cookies._cachedDocumentCookie !== Cookies._document.cookie) {
  28. Cookies._renewCache();
  29. }
  30. return Cookies._cache[Cookies._cacheKeyPrefix + key];
  31. };
  32. Cookies.set = function (key, value, options) {
  33. options = Cookies._getExtendedOptions(options);
  34. options.expires = Cookies._getExpiresDate(value === undefined ? -1 : options.expires);
  35. Cookies._document.cookie = Cookies._generateCookieString(key, value, options);
  36. return Cookies;
  37. };
  38. Cookies.expire = function (key, options) {
  39. return Cookies.set(key, undefined, options);
  40. };
  41. Cookies._getExtendedOptions = function (options) {
  42. return {
  43. path: options && options.path || Cookies.defaults.path,
  44. domain: options && options.domain || Cookies.defaults.domain,
  45. expires: options && options.expires || Cookies.defaults.expires,
  46. secure: options && options.secure !== undefined ? options.secure : Cookies.defaults.secure
  47. };
  48. };
  49. Cookies._isValidDate = function (date) {
  50. return Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime());
  51. };
  52. Cookies._getExpiresDate = function (expires, now) {
  53. now = now || new Date();
  54. switch (typeof expires) {
  55. case 'number': expires = new Date(now.getTime() + expires * 1000); break;
  56. case 'string': expires = new Date(expires); break;
  57. }
  58. if (expires && !Cookies._isValidDate(expires)) {
  59. throw new Error('`expires` parameter cannot be converted to a valid Date instance');
  60. }
  61. return expires;
  62. };
  63. Cookies._generateCookieString = function (key, value, options) {
  64. key = key.replace(/[^#$&+\^`|]/g, encodeURIComponent);
  65. key = key.replace(/\(/g, '%28').replace(/\)/g, '%29');
  66. value = (value + '').replace(/[^!#$&-+\--:<-\[\]-~]/g, encodeURIComponent);
  67. options = options || {};
  68. var cookieString = key + '=' + value;
  69. cookieString += options.path ? ';path=' + options.path : '';
  70. cookieString += options.domain ? ';domain=' + options.domain : '';
  71. cookieString += options.expires ? ';expires=' + options.expires.toUTCString() : '';
  72. cookieString += options.secure ? ';secure' : '';
  73. return cookieString;
  74. };
  75. Cookies._getCacheFromString = function (documentCookie) {
  76. var cookieCache = {};
  77. var cookiesArray = documentCookie ? documentCookie.split('; ') : [];
  78. for (var i = 0; i < cookiesArray.length; i++) {
  79. var cookieKvp = Cookies._getKeyValuePairFromCookieString(cookiesArray[i]);
  80. if (cookieCache[Cookies._cacheKeyPrefix + cookieKvp.key] === undefined) {
  81. cookieCache[Cookies._cacheKeyPrefix + cookieKvp.key] = cookieKvp.value;
  82. }
  83. }
  84. return cookieCache;
  85. };
  86. Cookies._getKeyValuePairFromCookieString = function (cookieString) {
  87. // "=" is a valid character in a cookie value according to RFC6265, so cannot `split('=')`
  88. var separatorIndex = cookieString.indexOf('=');
  89. // IE omits the "=" when the cookie value is an empty string
  90. separatorIndex = separatorIndex < 0 ? cookieString.length : separatorIndex;
  91. return {
  92. key: decodeURIComponent(cookieString.substr(0, separatorIndex)),
  93. value: decodeURIComponent(cookieString.substr(separatorIndex + 1))
  94. };
  95. };
  96. Cookies._renewCache = function () {
  97. Cookies._cache = Cookies._getCacheFromString(Cookies._document.cookie);
  98. Cookies._cachedDocumentCookie = Cookies._document.cookie;
  99. };
  100. Cookies._areEnabled = function () {
  101. var testKey = 'cookies.js';
  102. var areEnabled = Cookies.set(testKey, 1).get(testKey) === '1';
  103. Cookies.expire(testKey);
  104. return areEnabled;
  105. };
  106. Cookies.enabled = Cookies._areEnabled();
  107. return Cookies;
  108. };
  109. var cookiesExport = typeof global.document === 'object' ? factory(global) : factory;
  110. // AMD support
  111. if (typeof define === 'function' && define.amd) {
  112. define(function () { return cookiesExport; });
  113. // CommonJS/Node.js support
  114. } else if (typeof exports === 'object') {
  115. // Support Node.js specific `module.exports` (which can be a function)
  116. if (typeof module === 'object' && typeof module.exports === 'object') {
  117. exports = module.exports = cookiesExport;
  118. }
  119. // But always support CommonJS module 1.1.1 spec (`exports` cannot be a function)
  120. exports.Cookies = cookiesExport;
  121. } else {
  122. global.Cookies = cookiesExport;
  123. }
  124. })(typeof window === 'undefined' ? this : window);