Passed
Branch master (73dd7e)
by Eric
01:21
created

cookiejs.js ➔ ... ➔ cookiejs.global.cookie.some   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 6
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
(function(f) {
2
  if (typeof exports === 'object' && typeof module !== 'undefined') {
3
    module.exports = f();
4
  } else if (typeof define === 'function' && define.amd) {
0 ignored issues
show
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
5
    define([], f);
6
  } else {
7
    var g;
8
    if (typeof window !== 'undefined') {
9
      g = window;
10
    } else if (typeof global !== 'undefined') {
11
      g = global;
12
    } else if (typeof self !== 'undefined') {
0 ignored issues
show
Bug introduced by
The variable self seems to be never declared. If this is a global, consider adding a /** global: self */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
13
      g = self;
14
    } else {
15
      g = this;
16
    }
17
    g.cookiejs = f();
18
  }
19
})(function() {
20
  var define, module, exports;
0 ignored issues
show
Unused Code introduced by
The variable define seems to be never used. Consider removing it.
Loading history...
21
  module = { exports: (exports = {}) };
0 ignored issues
show
Bug introduced by
The variable exports seems to be never initialized.
Loading history...
22
  /*!
23
 * cookiejs.js | v1.0.0 | cookiejs object for setting/getting/removing cookies
24
 * Copyright (c) 2017 Eric Zieger (MIT license)
25
 * https://github.com/theZieger/cookiejs.js/blob/master/LICENSE
26
 */
27
28
  /**
29
   * throwTypeError
30
   *
31
   * @param {String} sName
32
   * @param {String} sType
33
   *
34
   * @throws {TypeError}
35
   */
36
  var throwTypeError = function(sName, sType) {
37
    throw new TypeError(sName + ' is not of type ' + sType);
38
  };
39
40
  var cookiejs = {
41
    global: this.document ? this.document : { cookie: '' },
42
43
    /**
44
     * sets or overwrites a cookie
45
     *
46
     * @param {String} sCookieName - the name of the cookie you want to set
47
     * @param {String} sValue - the value you want to set
48
     * @param {String} oAttributes - options e.g. domain, path, expires
49
     *
50
     * @throws {TypeError} if argument sCookieName is empty or not a string
51
     */
52 View Code Duplication
    set: function(sCookieName, sValue, oAttributes) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
53
      var sAttributes = '';
54
55
      sValue = sValue || '';
56
57
      if (!sCookieName || typeof sCookieName !== 'string') {
58
        throwTypeError('sCookieName', 'string');
59
      }
60
61
      if (typeof sValue !== 'string') {
62
        throwTypeError('sValue', 'string');
63
      }
64
65
      if (oAttributes === undefined) {
66
        sAttributes += '; path=/';
67
      } else {
68
        if (typeof oAttributes !== 'object' || Array.isArray(oAttributes)) {
69
          throwTypeError('oAttributes', 'object');
70
        }
71
72
        Object.keys(oAttributes).forEach(function(sAttr) {
73
          if (sAttr === 'secure') {
74
            if (oAttributes[sAttr] === true || oAttributes[sAttr] === 'true') {
75
              sAttributes += ';' + sAttr;
76
              return;
77
            } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
78
              throwTypeError(sAttr, 'boolean');
79
            }
80
          } else if (typeof oAttributes[sAttr] !== 'string') {
81
            throwTypeError(sAttr, 'string');
82
          }
83
84
          sAttributes += ';' + sAttr + '=' + oAttributes[sAttr];
85
        });
86
      }
87
88
      cookiejs.global.cookie =
89
        encodeURIComponent(sCookieName) +
90
        '=' +
91
        encodeURIComponent(sValue) +
92
        sAttributes;
93
    },
94
95
    /**
96
     * returns the value of a cookie
97
     *
98
     * @param {String} sCookieName
99
     *
100
     * @throws {TypeError}
101
     *
102
     * @returns {String|Boolean}
103
     */
104
    get: function(sCookieName) {
105
      var gCookieValue = false;
106
107
      if (!sCookieName || typeof sCookieName !== 'string') {
108
        throwTypeError('sCookieName', 'string');
109
      }
110
111
      cookiejs.global.cookie.split('; ').some(function(sCookie) {
112
        var aCookie = sCookie.split('=');
113
114
        if (decodeURIComponent(aCookie[0]) === sCookieName) {
115
          gCookieValue = decodeURIComponent(aCookie[1]);
116
          return true;
117
        }
118
119
        return false;
120
      });
121
122
      return gCookieValue;
123
    },
124
125
    /**
126
     * removes a specific cookie
127
     *
128
     * oAttributes must contain the correct path and domain it won't
129
     * remove the cookie
130
     *
131
     * @param {String} sCookieName
132
     * @param {Object} oAttributes - options e.g. domain, path, expires
133
     *
134
     * @throws {TypeError}
135
     */
136
    remove: function(sCookieName, oAttributes) {
137
      var oRemoveAttributes = oAttributes || {};
138
139
      if (
140
        typeof oRemoveAttributes === 'object' &&
141
        !Array.isArray(oRemoveAttributes)
142
      ) {
143
        oRemoveAttributes.expires = 'Thu, 01 Jan 1970 00:00:01 GMT';
144
      }
145
146
      cookiejs.set(sCookieName, '', oRemoveAttributes);
147
    }
148
  };
149
150
  module.exports = cookiejs;
151
152
  return module.exports;
153
});
154