Project

General

Profile

Download (3.33 KB) Statistics
| Branch: | Revision:
1
/*! js-cookie v3.0.0-rc.0 | 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
  function assign (target) {
14
    for (var i = 1; i < arguments.length; i++) {
15
      var source = arguments[i];
16
      for (var key in source) {
17
        target[key] = source[key];
18
      }
19
    }
20
    return target
21
  }
22

    
23
  var defaultConverter = {
24
    read: function (value) {
25
      return value.replace(/%3B/g, ';')
26
    },
27
    write: function (value) {
28
      return value.replace(/;/g, '%3B')
29
    }
30
  };
31

    
32
  function init (converter, defaultAttributes) {
33
    function set (key, value, attributes) {
34
      if (typeof document === 'undefined') {
35
        return
36
      }
37

    
38
      attributes = assign({}, defaultAttributes, attributes);
39

    
40
      if (typeof attributes.expires === 'number') {
41
        attributes.expires = new Date(Date.now() + attributes.expires * 864e5);
42
      }
43
      if (attributes.expires) {
44
        attributes.expires = attributes.expires.toUTCString();
45
      }
46

    
47
      key = defaultConverter.write(key).replace(/=/g, '%3D');
48

    
49
      value = converter.write(String(value), key);
50

    
51
      var stringifiedAttributes = '';
52
      for (var attributeName in attributes) {
53
        if (!attributes[attributeName]) {
54
          continue
55
        }
56

    
57
        stringifiedAttributes += '; ' + attributeName;
58

    
59
        if (attributes[attributeName] === true) {
60
          continue
61
        }
62

    
63
        stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
64
      }
65

    
66
      return (document.cookie = key + '=' + value + stringifiedAttributes)
67
    }
68

    
69
    function get (key) {
70
      if (typeof document === 'undefined' || (arguments.length && !key)) {
71
        return
72
      }
73

    
74
      // To prevent the for loop in the first place assign an empty array
75
      // in case there are no cookies at all.
76
      var cookies = document.cookie ? document.cookie.split('; ') : [];
77
      var jar = {};
78
      for (var i = 0; i < cookies.length; i++) {
79
        var parts = cookies[i].split('=');
80
        var value = parts.slice(1).join('=');
81
        var foundKey = defaultConverter.read(parts[0]).replace(/%3D/g, '=');
82
        jar[foundKey] = converter.read(value, foundKey);
83

    
84
        if (key === foundKey) {
85
          break
86
        }
87
      }
88

    
89
      return key ? jar[key] : jar
90
    }
91

    
92
    return Object.create(
93
      {
94
        set: set,
95
        get: get,
96
        remove: function (key, attributes) {
97
          set(
98
            key,
99
            '',
100
            assign({}, attributes, {
101
              expires: -1
102
            })
103
          );
104
        },
105
        withAttributes: function (attributes) {
106
          return init(this.converter, assign({}, this.attributes, attributes))
107
        },
108
        withConverter: function (converter) {
109
          return init(assign({}, this.converter, converter), this.attributes)
110
        }
111
      },
112
      {
113
        attributes: { value: Object.freeze(defaultAttributes) },
114
        converter: { value: Object.freeze(converter) }
115
      }
116
    )
117
  }
118

    
119
  var api = init(defaultConverter, { path: '/' });
120

    
121
  return api;
122

    
123
})));
(1-1/3)