Completed
Pull Request — master (#8)
by Eric
40s
created

cookiejs.js ➔ ?!?   A

Complexity

Conditions 1
Paths 8

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
cc 1
c 3
b 1
f 2
nc 8
nop 0
dl 0
loc 74
rs 9.0335

3 Functions

Rating   Name   Duplication   Size   Complexity  
A cookiejs.js ➔ ... ➔ cookiejs.set 0 15 2
B cookiejs.js ➔ ... ➔ cookiejs.get 0 18 5
A cookiejs.js ➔ ... ➔ cookiejs.remove 0 5 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 * cookiejs object for setting/getting/removing cookies
3
 */
4
 (function(root, factory) {
5
    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...
6
        define(["cookiejs"], factory);
7
    } else if (typeof module === "object" && module.exports) {
8
        module.exports = factory();
9
    } else {
10
        root.cookiejs = factory();
11
    }
12
}(this, function() {
13
14
    /**
15
     * @type {Object}
16
     */
17
    var cookiejs = {};
18
19
    /**
20
     * sets or overwrites a cookie
21
     * 
22
     * @param {String} sCookieName - the name of the cookie you want to set
23
     * @param {String} sValue - the value you want to set
24
     * @param {String} oAttributes - options e.g. domain, path, expires
25
     *
26
     * @returns {String}
27
     */
28
    cookiejs.set = function(sCookieName, sValue, oAttributes) {
29
        var sAttributes = '';
30
31
        oAttributes = oAttributes || {};
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter oAttributes. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
32
33
        if (typeof oAttributes.path !== 'string') {
34
            sAttributes += '; path=/';
35
        }
36
37
        Object.keys(oAttributes).forEach(function(sAttributeName) {
38
            sAttributes += ';' + sAttributeName + '=' + oAttributes[sAttributeName];
39
        });    
40
41
        document.cookie = encodeURIComponent(sCookieName) + '=' + encodeURIComponent(sValue) + sAttributes;
42
    };
43
44
    /**
45
     * returns the value of a cookie
46
     * 
47
     * @param {String} sCookieName
48
     *
49
     * @returns {String|Boolean}
50
     */
51
    cookiejs.get = function(sCookieName) {
52
        var aCookies,
53
            gCookieValue = false;
54
        
55
        if (!sCookieName || typeof sCookieName != 'string') {
56
            return gCookieValue;
57
        }
58
        
59
        aCookies = document.cookie.split('; ');
60
61
        for (var i = aCookies.length - 1; i >= 0; i--) {
62
            if (decodeURIComponent(aCookies[i].split('=')[0]) === sCookieName) {
63
                gCookieValue = decodeURIComponent(aCookie[1]);
0 ignored issues
show
Bug introduced by
The variable aCookie seems to be never declared. If this is a global, consider adding a /** global: aCookie */ 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...
64
            }
65
        }
66
        
67
        return gCookieValue;
68
    };
69
70
    /**
71
     * removes a specific cookie
72
     *
73
     * oAttributes must contain the correct path and domain else you can't remove the cookie
74
     *
75
     * @param {String} sCookieName
76
     * @param {Object} oAttributes - options e.g. domain, path, expires
77
     */
78
    cookiejs.remove = function(sCookieName, oAttributes) {
79
        oAttributes = oAttributes || {};
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter oAttributes. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
80
        oAttributes.expires = 'Thu, 01 Jan 1970 00:00:01 GMT';
81
        this.set(sCookieName, '', oAttributes);
82
    };
83
84
    return cookiejs;
85
}));