| Conditions | 1 |
| Paths | 8 |
| Total Lines | 74 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 2 |
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:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 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 || {}; |
||
| 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]); |
||
| 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 || {}; |
||
| 80 | oAttributes.expires = 'Thu, 01 Jan 1970 00:00:01 GMT'; |
||
| 81 | this.set(sCookieName, '', oAttributes); |
||
| 82 | }; |
||
| 83 | |||
| 84 | return cookiejs; |
||
| 85 | })); |
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.