/client/js/lib/cookie/cookie.js

https://github.com/natos/WhatsOn · JavaScript · 172 lines · 158 code · 7 blank · 7 comment · 4 complexity · c1663504fbfa68d071b5564ed818effa MD5 · raw file

  1. // Copyright (c) 2012 Florian H., https://github.com/js-coder https://github.com/js-coder/cookie.js
  2. !function (document, undefined) {
  3. var utils = {
  4. // Is the given value an array? Use ES5 Array.isArray if it's available.
  5. isArray: Array.isArray || function (value) {
  6. return Object.prototype.toString.call(value) === '[object Array]';
  7. },
  8. // Is the given value a plain object / an object whose constructor is `Object`?
  9. isPlainObject: function (value) {
  10. return Object.prototype.toString.call(value) === '[object Object]';
  11. },
  12. // Convert an array-like object to an array – for example `arguments`.
  13. toArray: function (value) {
  14. return Array.prototype.slice.call(value);
  15. },
  16. // Get the keys of an object. Use ES5 Object.keys if it's available.
  17. getKeys: Object.keys || function (obj) {
  18. var keys = [],
  19. key = '';
  20. for (key in obj) {
  21. if (obj.hasOwnProperty(key)) keys.push(key);
  22. }
  23. return keys;
  24. },
  25. // Unlike JavaScript's built-in escape functions, this method
  26. // only escapes characters that are not allowed in cookies.
  27. escape: function (value) {
  28. return value.replace(/[,;"\\=\s%]/g, function (character) {
  29. return encodeURIComponent(character);
  30. });
  31. },
  32. // Return fallback if the value is undefined, otherwise return value.
  33. retrieve: function (value, fallback) {
  34. return value === undefined ? fallback : value;
  35. }
  36. };
  37. var cookie = function () {
  38. return cookie.get.apply(cookie, arguments);
  39. };
  40. cookie.defaults = {};
  41. cookie.expiresMultiplier = 60 * 60 * 24;
  42. cookie.set = function (key, value, options) {
  43. if (utils.isPlainObject(key)) { // Then `key` contains an object with keys and values for cookies, `value` contains the options object.
  44. for (var k in key) { // TODO: `k` really sucks as a variable name, but I didn't come up with a better one yet.
  45. if (key.hasOwnProperty(k)) this.set(k, key[k], value);
  46. }
  47. } else {
  48. options = utils.isPlainObject(options) ? options : { expires: options };
  49. var expires = options.expires !== undefined ? options.expires : (this.defaults.expires || ''), // Empty string for session cookies.
  50. expiresType = typeof(expires);
  51. if (expiresType === 'string' && expires !== '') expires = new Date(expires);
  52. else if (expiresType === 'number') expires = new Date(+new Date + 1000 * this.expiresMultiplier * expires); // This is needed because IE does not support the `max-age` cookie attribute.
  53. if (expires !== '' && 'toGMTString' in expires) expires = ';expires=' + expires.toGMTString();
  54. var path = options.path || this.defaults.path; // TODO: Too much code for a simple feature.
  55. path = path ? ';path=' + path : '';
  56. var domain = options.domain || this.defaults.domain;
  57. domain = domain ? ';domain=' + domain : '';
  58. var secure = options.secure || this.defaults.secure ? ';secure' : '';
  59. document.cookie = utils.escape(key) + '=' + utils.escape(value) + expires + path + domain + secure;
  60. }
  61. return this; // Return the `cookie` object to make chaining possible.
  62. };
  63. // TODO: This is commented out, because I didn't come up with a better method name yet. Any ideas?
  64. // cookie.setIfItDoesNotExist = function (key, value, options) {
  65. // if (this.get(key) === undefined) this.set.call(this, arguments);
  66. // },
  67. cookie.remove = function (keys) {
  68. keys = utils.isArray(keys) ? keys : utils.toArray(arguments);
  69. for (var i = 0, l = keys.length; i < l; i++) {
  70. this.set(keys[i], '', -1);
  71. }
  72. return this; // Return the `cookie` object to make chaining possible.
  73. };
  74. cookie.empty = function () {
  75. return this.remove(utils.getKeys(this.all()));
  76. };
  77. cookie.get = function (keys, fallback) {
  78. fallback = fallback || undefined;
  79. var cookies = this.all();
  80. if (utils.isArray(keys)) {
  81. var result = {};
  82. for (var i = 0, l = keys.length; i < l; i++) {
  83. var value = keys[i];
  84. result[value] = utils.retrieve(cookies[value], fallback);
  85. }
  86. return result;
  87. } else return utils.retrieve(cookies[keys], fallback);
  88. };
  89. cookie.all = function () {
  90. if (document.cookie === '') return {};
  91. var cookies = document.cookie.split('; '),
  92. result = {};
  93. for (var i = 0, l = cookies.length; i < l; i++) {
  94. var item = cookies[i].split('=');
  95. result[decodeURIComponent(item[0])] = decodeURIComponent(item[1]);
  96. }
  97. return result;
  98. };
  99. cookie.enabled = function () {
  100. if (navigator.cookieEnabled) return true;
  101. var ret = cookie.set('_', '_').get('_') === '_';
  102. cookie.remove('_');
  103. return ret;
  104. };
  105. // If an AMD loader is present use AMD.
  106. // If a CommonJS loader is present use CommonJS.
  107. // Otherwise assign the `cookie` object to the global scope.
  108. if (typeof define === 'function' && define.amd) {
  109. define(function () {
  110. return cookie;
  111. });
  112. } else if (typeof exports !== 'undefined') {
  113. exports.cookie = cookie;
  114. } else window.cookie = cookie;
  115. }(document);