| Conditions | 21 |
| Paths | 256 |
| Total Lines | 79 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
Complex classes like index.js ➔ serialize often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /*! |
||
| 101 | function serialize(name, val, options) { |
||
| 102 | var opt = options || {}; |
||
| 103 | var enc = opt.encode || encode; |
||
| 104 | |||
| 105 | if (typeof enc !== 'function') { |
||
| 106 | throw new TypeError('option encode is invalid'); |
||
| 107 | } |
||
| 108 | |||
| 109 | if (!fieldContentRegExp.test(name)) { |
||
| 110 | throw new TypeError('argument name is invalid'); |
||
| 111 | } |
||
| 112 | |||
| 113 | var value = enc(val); |
||
| 114 | |||
| 115 | if (value && !fieldContentRegExp.test(value)) { |
||
| 116 | throw new TypeError('argument val is invalid'); |
||
| 117 | } |
||
| 118 | |||
| 119 | var str = name + '=' + value; |
||
| 120 | |||
| 121 | if (null != opt.maxAge) { |
||
| 122 | var maxAge = opt.maxAge - 0; |
||
| 123 | if (isNaN(maxAge)) throw new Error('maxAge should be a Number'); |
||
| 124 | str += '; Max-Age=' + Math.floor(maxAge); |
||
| 125 | } |
||
| 126 | |||
| 127 | if (opt.domain) { |
||
| 128 | if (!fieldContentRegExp.test(opt.domain)) { |
||
| 129 | throw new TypeError('option domain is invalid'); |
||
| 130 | } |
||
| 131 | |||
| 132 | str += '; Domain=' + opt.domain; |
||
| 133 | } |
||
| 134 | |||
| 135 | if (opt.path) { |
||
| 136 | if (!fieldContentRegExp.test(opt.path)) { |
||
| 137 | throw new TypeError('option path is invalid'); |
||
| 138 | } |
||
| 139 | |||
| 140 | str += '; Path=' + opt.path; |
||
| 141 | } |
||
| 142 | |||
| 143 | if (opt.expires) { |
||
| 144 | if (typeof opt.expires.toUTCString !== 'function') { |
||
| 145 | throw new TypeError('option expires is invalid'); |
||
| 146 | } |
||
| 147 | |||
| 148 | str += '; Expires=' + opt.expires.toUTCString(); |
||
| 149 | } |
||
| 150 | |||
| 151 | if (opt.httpOnly) { |
||
| 152 | str += '; HttpOnly'; |
||
| 153 | } |
||
| 154 | |||
| 155 | if (opt.secure) { |
||
| 156 | str += '; Secure'; |
||
| 157 | } |
||
| 158 | |||
| 159 | if (opt.sameSite) { |
||
| 160 | var sameSite = typeof opt.sameSite === 'string' |
||
| 161 | ? opt.sameSite.toLowerCase() : opt.sameSite; |
||
| 162 | |||
| 163 | switch (sameSite) { |
||
| 164 | case true: |
||
| 165 | str += '; SameSite=Strict'; |
||
| 166 | break; |
||
| 167 | case 'lax': |
||
| 168 | str += '; SameSite=Lax'; |
||
| 169 | break; |
||
| 170 | case 'strict': |
||
| 171 | str += '; SameSite=Strict'; |
||
| 172 | break; |
||
| 173 | default: |
||
| 174 | throw new TypeError('option sameSite is invalid'); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | return str; |
||
| 179 | } |
||
| 180 | |||
| 196 |