cookies.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*!
  2. * Cookies.js - 0.3.1
  3. * Wednesday, April 24 2013 @ 2:28 AM EST
  4. *
  5. * Copyright (c) 2013, Scott Hamper
  6. * Licensed under the MIT license,
  7. * http://www.opensource.org/licenses/MIT
  8. */
  9. (function (undefined) {
  10. 'use strict';
  11. var Cookies = function (key, value, options) {
  12. return arguments.length === 1 ?
  13. Cookies.get(key) : Cookies.set(key, value, options);
  14. };
  15. // Allows for setter injection in unit tests
  16. Cookies._document = document;
  17. Cookies._navigator = navigator;
  18. Cookies.defaults = {
  19. path: '/'
  20. };
  21. Cookies.get = function (key) {
  22. if (Cookies._cachedDocumentCookie !== Cookies._document.cookie) {
  23. Cookies._renewCache();
  24. }
  25. return Cookies._cache[key];
  26. };
  27. Cookies.set = function (key, value, options) {
  28. options = Cookies._getExtendedOptions(options);
  29. options.expires = Cookies._getExpiresDate(value === undefined ? -1 : options.expires);
  30. Cookies._document.cookie = Cookies._generateCookieString(key, value, options);
  31. return Cookies;
  32. };
  33. Cookies.expire = function (key, options) {
  34. return Cookies.set(key, undefined, options);
  35. };
  36. Cookies._getExtendedOptions = function (options) {
  37. return {
  38. path: options && options.path || Cookies.defaults.path,
  39. domain: options && options.domain || Cookies.defaults.domain,
  40. expires: options && options.expires || Cookies.defaults.expires,
  41. secure: options && options.secure !== undefined ? options.secure : Cookies.defaults.secure
  42. };
  43. };
  44. Cookies._isValidDate = function (date) {
  45. return Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime());
  46. };
  47. Cookies._getExpiresDate = function (expires, now) {
  48. now = now || new Date();
  49. switch (typeof expires) {
  50. case 'number': expires = new Date(now.getTime() + expires * 1000); break;
  51. case 'string': expires = new Date(expires); break;
  52. }
  53. if (expires && !Cookies._isValidDate(expires)) {
  54. throw new Error('`expires` parameter cannot be converted to a valid Date instance');
  55. }
  56. return expires;
  57. };
  58. Cookies._generateCookieString = function (key, value, options) {
  59. key = encodeURIComponent(key);
  60. value = (value + '').replace(/[^!#$&-+\--:<-\[\]-~]/g, encodeURIComponent);
  61. options = options || {};
  62. var cookieString = key + '=' + value;
  63. cookieString += options.path ? ';path=' + options.path : '';
  64. cookieString += options.domain ? ';domain=' + options.domain : '';
  65. cookieString += options.expires ? ';expires=' + options.expires.toGMTString() : '';
  66. cookieString += options.secure ? ';secure' : '';
  67. return cookieString;
  68. };
  69. Cookies._getCookieObjectFromString = function (documentCookie) {
  70. var cookieObject = {};
  71. var cookiesArray = documentCookie ? documentCookie.split('; ') : [];
  72. for (var i = 0; i < cookiesArray.length; i++) {
  73. var cookieKvp = Cookies._getKeyValuePairFromCookieString(cookiesArray[i]);
  74. if (cookieObject[cookieKvp.key] === undefined) {
  75. cookieObject[cookieKvp.key] = cookieKvp.value;
  76. }
  77. }
  78. return cookieObject;
  79. };
  80. Cookies._getKeyValuePairFromCookieString = function (cookieString) {
  81. // "=" is a valid character in a cookie value according to RFC6265, so cannot `split('=')`
  82. var separatorIndex = cookieString.indexOf('=');
  83. // IE omits the "=" when the cookie value is an empty string
  84. separatorIndex = separatorIndex < 0 ? cookieString.length : separatorIndex;
  85. return {
  86. key: decodeURIComponent(cookieString.substr(0, separatorIndex)),
  87. value: decodeURIComponent(cookieString.substr(separatorIndex + 1))
  88. };
  89. };
  90. Cookies._renewCache = function () {
  91. Cookies._cache = Cookies._getCookieObjectFromString(Cookies._document.cookie);
  92. Cookies._cachedDocumentCookie = Cookies._document.cookie;
  93. };
  94. Cookies._areEnabled = function () {
  95. return Cookies._navigator.cookieEnabled ||
  96. Cookies.set('cookies.js', 1).get('cookies.js') === '1';
  97. };
  98. Cookies.enabled = Cookies._areEnabled();
  99. // AMD support
  100. if (typeof define === 'function' && define.amd) {
  101. define(function () { return Cookies; });
  102. // CommonJS and Node.js module support.
  103. } else if (typeof exports !== 'undefined') {
  104. // Support Node.js specific `module.exports` (which can be a function)
  105. if (typeof module !== 'undefined' && module.exports) {
  106. exports = module.exports = Cookies;
  107. }
  108. // But always support CommonJS module 1.1.1 spec (`exports` cannot be a function)
  109. exports.Cookies = Cookies;
  110. } else {
  111. window.Cookies = Cookies;
  112. }
  113. })();