|
| 1 | +/*! js-cookie v3.0.1 | MIT */ |
| 2 | +; |
| 3 | +(function (global, factory) { |
| 4 | + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : |
| 5 | + typeof define === 'function' && define.amd ? define(factory) : |
| 6 | + (global = global || self, (function () { |
| 7 | + var current = global.Cookies; |
| 8 | + var exports = global.Cookies = factory(); |
| 9 | + exports.noConflict = function () { global.Cookies = current; return exports; }; |
| 10 | + }())); |
| 11 | +}(this, (function () { 'use strict'; |
| 12 | + |
| 13 | + /* eslint-disable no-var */ |
| 14 | + function assign (target) { |
| 15 | + for (var i = 1; i < arguments.length; i++) { |
| 16 | + var source = arguments[i]; |
| 17 | + for (var key in source) { |
| 18 | + target[key] = source[key]; |
| 19 | + } |
| 20 | + } |
| 21 | + return target |
| 22 | + } |
| 23 | + /* eslint-enable no-var */ |
| 24 | + |
| 25 | + /* eslint-disable no-var */ |
| 26 | + var defaultConverter = { |
| 27 | + read: function (value) { |
| 28 | + if (value[0] === '"') { |
| 29 | + value = value.slice(1, -1); |
| 30 | + } |
| 31 | + return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent) |
| 32 | + }, |
| 33 | + write: function (value) { |
| 34 | + return encodeURIComponent(value).replace( |
| 35 | + /%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g, |
| 36 | + decodeURIComponent |
| 37 | + ) |
| 38 | + } |
| 39 | + }; |
| 40 | + /* eslint-enable no-var */ |
| 41 | + |
| 42 | + /* eslint-disable no-var */ |
| 43 | + |
| 44 | + function init (converter, defaultAttributes) { |
| 45 | + function set (key, value, attributes) { |
| 46 | + if (typeof document === 'undefined') { |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + attributes = assign({}, defaultAttributes, attributes); |
| 51 | + |
| 52 | + if (typeof attributes.expires === 'number') { |
| 53 | + attributes.expires = new Date(Date.now() + attributes.expires * 864e5); |
| 54 | + } |
| 55 | + if (attributes.expires) { |
| 56 | + attributes.expires = attributes.expires.toUTCString(); |
| 57 | + } |
| 58 | + |
| 59 | + key = encodeURIComponent(key) |
| 60 | + .replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent) |
| 61 | + .replace(/[()]/g, escape); |
| 62 | + |
| 63 | + var stringifiedAttributes = ''; |
| 64 | + for (var attributeName in attributes) { |
| 65 | + if (!attributes[attributeName]) { |
| 66 | + continue |
| 67 | + } |
| 68 | + |
| 69 | + stringifiedAttributes += '; ' + attributeName; |
| 70 | + |
| 71 | + if (attributes[attributeName] === true) { |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + // Considers RFC 6265 section 5.2: |
| 76 | + // ... |
| 77 | + // 3. If the remaining unparsed-attributes contains a %x3B (";") |
| 78 | + // character: |
| 79 | + // Consume the characters of the unparsed-attributes up to, |
| 80 | + // not including, the first %x3B (";") character. |
| 81 | + // ... |
| 82 | + stringifiedAttributes += '=' + attributes[attributeName].split(';')[0]; |
| 83 | + } |
| 84 | + |
| 85 | + return (document.cookie = |
| 86 | + key + '=' + converter.write(value, key) + stringifiedAttributes) |
| 87 | + } |
| 88 | + |
| 89 | + function get (key) { |
| 90 | + if (typeof document === 'undefined' || (arguments.length && !key)) { |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + // To prevent the for loop in the first place assign an empty array |
| 95 | + // in case there are no cookies at all. |
| 96 | + var cookies = document.cookie ? document.cookie.split('; ') : []; |
| 97 | + var jar = {}; |
| 98 | + for (var i = 0; i < cookies.length; i++) { |
| 99 | + var parts = cookies[i].split('='); |
| 100 | + var value = parts.slice(1).join('='); |
| 101 | + |
| 102 | + try { |
| 103 | + var foundKey = decodeURIComponent(parts[0]); |
| 104 | + jar[foundKey] = converter.read(value, foundKey); |
| 105 | + |
| 106 | + if (key === foundKey) { |
| 107 | + break |
| 108 | + } |
| 109 | + } catch (e) {} |
| 110 | + } |
| 111 | + |
| 112 | + return key ? jar[key] : jar |
| 113 | + } |
| 114 | + |
| 115 | + return Object.create( |
| 116 | + { |
| 117 | + set: set, |
| 118 | + get: get, |
| 119 | + remove: function (key, attributes) { |
| 120 | + set( |
| 121 | + key, |
| 122 | + '', |
| 123 | + assign({}, attributes, { |
| 124 | + expires: -1 |
| 125 | + }) |
| 126 | + ); |
| 127 | + }, |
| 128 | + withAttributes: function (attributes) { |
| 129 | + return init(this.converter, assign({}, this.attributes, attributes)) |
| 130 | + }, |
| 131 | + withConverter: function (converter) { |
| 132 | + return init(assign({}, this.converter, converter), this.attributes) |
| 133 | + } |
| 134 | + }, |
| 135 | + { |
| 136 | + attributes: { value: Object.freeze(defaultAttributes) }, |
| 137 | + converter: { value: Object.freeze(converter) } |
| 138 | + } |
| 139 | + ) |
| 140 | + } |
| 141 | + |
| 142 | + var api = init(defaultConverter, { path: '/' }); |
| 143 | + /* eslint-enable no-var */ |
| 144 | + |
| 145 | + return api; |
| 146 | + |
| 147 | +}))); |
0 commit comments