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

cookiejs.set   B

Complexity

Conditions 7
Paths 40

Size

Total Lines 42
Code Lines 27

Duplication

Lines 42
Ratio 100 %

Code Coverage

Tests 20
CRAP Score 7

Importance

Changes 5
Bugs 1 Features 4
Metric Value
cc 7
eloc 27
c 5
b 1
f 4
nc 40
nop 3
dl 42
loc 42
rs 7.8319
ccs 20
cts 20
cp 1
crap 7

1 Function

Rating   Name   Duplication   Size   Complexity  
A 14 14 5
1
/*!
2
 * cookiejs.js | v1.0.0 | cookiejs object for setting/getting/removing cookies
3
 * Copyright (c) 2017 Eric Zieger (MIT license)
4
 * https://github.com/theZieger/cookiejs.js/blob/master/LICENSE
5
 */
6
7
/**
8
 * throwTypeError
9
 *
10
 * @param {String} sName
11
 * @param {String} sType
12
 *
13
 * @throws {TypeError}
14
 */
15 1
var throwTypeError = function(sName, sType) {
16 24
  throw new TypeError(sName + ' is not of type ' + sType);
17
};
18
19 1
var cookiejs = {
20
  global: this.document ? this.document : { cookie: '' },
21
22
  /**
23
   * sets or overwrites a cookie
24
   *
25
   * @param {String} sCookieName - the name of the cookie you want to set
26
   * @param {String} sValue - the value you want to set
27
   * @param {String} oAttributes - options e.g. domain, path, expires
28
   *
29
   * @throws {TypeError} if argument sCookieName is empty or not a string
30
   */
31 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...
32 33
    var sAttributes = '';
33
34 33
    sValue = sValue || '';
35
36 33
    if (!sCookieName || typeof sCookieName !== 'string') {
37 4
      throwTypeError('sCookieName', 'string');
38
    }
39
40 29
    if (typeof sValue !== 'string') {
41 4
      throwTypeError('sValue', 'string');
42
    }
43
44 25
    if (oAttributes === undefined) {
45 7
      sAttributes += '; path=/';
46
    } else {
47 18
      if (typeof oAttributes !== 'object' || Array.isArray(oAttributes)) {
48 5
        throwTypeError('oAttributes', 'object');
49
      }
50
51 13
      Object.keys(oAttributes).forEach(function(sAttr) {
52 20
        if (sAttr === 'secure') {
53 4
          if (oAttributes[sAttr] === true || oAttributes[sAttr] === 'true') {
54 1
            sAttributes += ';' + sAttr;
55 1
            return;
56
          } 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...
57 3
            throwTypeError(sAttr, 'boolean');
58
          }
59 16
        } else if (typeof oAttributes[sAttr] !== 'string') {
60 7
          throwTypeError(sAttr, 'string');
61
        }
62
63 9
        sAttributes += ';' + sAttr + '=' + oAttributes[sAttr];
64
      });
65
    }
66
67 10
    cookiejs.global.cookie =
68
      encodeURIComponent(sCookieName) +
69
      '=' +
70
      encodeURIComponent(sValue) +
71
      sAttributes;
72
  },
73
74
  /**
75
   * returns the value of a cookie
76
   *
77
   * @param {String} sCookieName
78
   *
79
   * @throws {TypeError}
80
   *
81
   * @returns {String|Boolean}
82
   */
83
  get: function(sCookieName) {
84 2
    var gCookieValue = false;
85
86 4
    if (!sCookieName || typeof sCookieName !== 'string') {
87 1
      throwTypeError('sCookieName', 'string');
88
    }
89
90 1
    cookiejs.global.cookie.split('; ').some(function(sCookie) {
91 2
      var aCookie = sCookie.split('=');
92
93 2
      if (decodeURIComponent(aCookie[0]) === sCookieName) {
94 1
        gCookieValue = decodeURIComponent(aCookie[1]);
95 1
        return true;
96
      }
97
98 1
      return false;
99
    });
100
101 1
    return gCookieValue;
102
  },
103
104
  /**
105
   * removes a specific cookie
106
   *
107
   * oAttributes must contain the correct path and domain it won't
108
   * remove the cookie
109
   *
110
   * @param {String} sCookieName
111
   * @param {Object} oAttributes - options e.g. domain, path, expires
112
   *
113
   * @throws {TypeError}
114
   */
115
  remove: function(sCookieName, oAttributes) {
116 4
    var oRemoveAttributes = oAttributes || {};
117
118 4
    if (
119
      typeof oRemoveAttributes === 'object' &&
120
      !Array.isArray(oRemoveAttributes)
121
    ) {
122 3
      oRemoveAttributes.expires = 'Thu, 01 Jan 1970 00:00:01 GMT';
123
    }
124
125 4
    cookiejs.set(sCookieName, '', oRemoveAttributes);
126
  }
127
};
128
129
module.exports = cookiejs;
130