vue-cookies.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Vue Cookies v1.5.13
  3. * https://github.com/cmp-cc/vue-cookies
  4. *
  5. * Copyright 2016, cmp-cc
  6. * Released under the MIT license
  7. */
  8. (function() {
  9. var defaultConfig = {
  10. expires : '1d',
  11. path : '; path=/'
  12. }
  13. var VueCookies = {
  14. // install of Vue
  15. install: function(Vue) {
  16. Vue.prototype.$cookies = this
  17. Vue.cookies = this
  18. },
  19. config : function(expireTimes,path) {
  20. if(expireTimes) {
  21. defaultConfig.expires = expireTimes;
  22. }
  23. if(path) {
  24. defaultConfig.path = '; path=' + path;
  25. }
  26. },
  27. get: function(key) {
  28. var value = decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(key).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null
  29. if(value && value.substring(0,1) === "{" && value.substring(value.length-1,value.length) === "}") {
  30. try {
  31. value = JSON.parse(value)
  32. }catch (e) {
  33. return value;
  34. }
  35. }
  36. return value;
  37. },
  38. set: function(key, value, expireTimes, path, domain, secure) {
  39. if (!key) {
  40. throw new Error("cookie name is not find in first argument")
  41. }else if(/^(?:expires|max\-age|path|domain|secure)$/i.test(key)){
  42. throw new Error("cookie key name illegality ,Cannot be set to ['expires','max-age','path','domain','secure']\t","current key name: "+key);
  43. }
  44. // support json object
  45. if(value && value.constructor === Object) {
  46. value = JSON.stringify(value);
  47. }
  48. var _expires = "";
  49. expireTimes = expireTimes === undefined ? defaultConfig.expires : expireTimes;
  50. if (expireTimes && expireTimes != 0) {
  51. switch (expireTimes.constructor) {
  52. case Number:
  53. if(expireTimes === Infinity || expireTimes === -1) _expires = "; expires=Fri, 31 Dec 9999 23:59:59 GMT";
  54. else _expires = "; max-age=" + expireTimes;
  55. break;
  56. case String:
  57. if (/^(?:\d{1,}(y|m|d|h|min|s))$/i.test(expireTimes)) {
  58. // get capture number group
  59. var _expireTime = expireTimes.replace(/^(\d{1,})(?:y|m|d|h|min|s)$/i, "$1");
  60. // get capture type group , to lower case
  61. switch (expireTimes.replace(/^(?:\d{1,})(y|m|d|h|min|s)$/i, "$1").toLowerCase()) {
  62. // Frequency sorting
  63. case 'm': _expires = "; max-age=" + +_expireTime * 2592000; break; // 60 * 60 * 24 * 30
  64. case 'd': _expires = "; max-age=" + +_expireTime * 86400; break; // 60 * 60 * 24
  65. case 'h': _expires = "; max-age=" + +_expireTime * 3600; break; // 60 * 60
  66. case 'min': _expires = "; max-age=" + +_expireTime * 60; break; // 60
  67. case 's': _expires = "; max-age=" + _expireTime; break;
  68. case 'y': _expires = "; max-age=" + +_expireTime * 31104000; break; // 60 * 60 * 24 * 30 * 12
  69. default: new Error("unknown exception of 'set operation'");
  70. }
  71. } else {
  72. _expires = "; expires=" + expireTimes;
  73. }
  74. break;
  75. case Date:
  76. _expires = "; expires=" + expireTimes.toUTCString();
  77. break;
  78. }
  79. }
  80. document.cookie = encodeURIComponent(key) + "=" + encodeURIComponent(value) + _expires + (domain ? "; domain=" + domain : "") + (path ? "; path=" + path : defaultConfig.path) + (secure ? "; secure" : "");
  81. return this;
  82. },
  83. remove: function(key, path, domain) {
  84. if (!key || !this.isKey(key)) {
  85. return false;
  86. }
  87. document.cookie = encodeURIComponent(key) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (domain ? "; domain=" + domain : "") + (path ? "; path=" + path : defaultConfig.path);
  88. return this;
  89. },
  90. isKey: function(key) {
  91. return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(key).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
  92. },
  93. keys: function() {
  94. if(!document.cookie) return [];
  95. var _keys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
  96. for (var _index = 0; _index < _keys.length; _index++) {
  97. _keys[_index] = decodeURIComponent(_keys[_index]);
  98. }
  99. return _keys;
  100. }
  101. }
  102. if (typeof exports == "object") {
  103. module.exports = VueCookies;
  104. } else if (typeof define == "function" && define.amd) {
  105. define([], function() {
  106. return VueCookies;
  107. })
  108. } else if (window.Vue) {
  109. Vue.use(VueCookies);
  110. }
  111. // vue-cookies can exist independently,no dependencies library
  112. if(typeof window!=="undefined"){
  113. window.$cookies = VueCookies;
  114. }
  115. })()