Conditions | 2 |
Paths | 2 |
Total Lines | 51 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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:
1 | |||
56 | (function() { |
||
57 | 'use strict'; // needed to support `apply`/`call` with `undefined`/`null` |
||
58 | var defineProperty = (function() { |
||
59 | // IE 8 only supports `Object.defineProperty` on DOM elements |
||
60 | try { |
||
61 | var object = {}; |
||
62 | var $defineProperty = Object.defineProperty; |
||
63 | var result = $defineProperty(object, object, object) && $defineProperty; |
||
64 | } catch(error) {} |
||
65 | return result; |
||
66 | }()); |
||
67 | var codePointAt = function(position) { |
||
68 | if (this == null) { |
||
69 | throw TypeError(); |
||
70 | } |
||
71 | var string = String(this); |
||
72 | var size = string.length; |
||
73 | // `ToInteger` |
||
74 | var index = position ? Number(position) : 0; |
||
75 | if (index != index) { // better `isNaN` |
||
76 | index = 0; |
||
77 | } |
||
78 | // Account for out-of-bounds indices: |
||
79 | if (index < 0 || index >= size) { |
||
80 | return undefined; |
||
81 | } |
||
82 | // Get the first code unit |
||
83 | var first = string.charCodeAt(index); |
||
84 | var second; |
||
85 | if ( // check if it’s the start of a surrogate pair |
||
86 | first >= 0xD800 && first <= 0xDBFF && // high surrogate |
||
87 | size > index + 1 // there is a next code unit |
||
88 | ) { |
||
89 | second = string.charCodeAt(index + 1); |
||
90 | if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate |
||
91 | // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae |
||
92 | return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000; |
||
93 | } |
||
94 | } |
||
95 | return first; |
||
96 | }; |
||
97 | if (defineProperty) { |
||
98 | defineProperty(String.prototype, 'codePointAt', { |
||
99 | 'value': codePointAt, |
||
100 | 'configurable': true, |
||
101 | 'writable': true |
||
102 | }); |
||
103 | } else { |
||
104 | String.prototype.codePointAt = codePointAt; |
||
105 | } |
||
106 | }()); |
||
107 | } |
||
115 |