Conditions | 1 |
Paths | 256 |
Total Lines | 149 |
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 | /** |
||
73 | function TreeNode (value) { |
||
74 | var self = this |
||
75 | /** |
||
76 | * @template {V} |
||
77 | * @typedef {Object.<string, TreeNode<V>>} |
||
78 | */ |
||
79 | var children = {} |
||
80 | |||
81 | /** |
||
82 | * Adds new child to current node. |
||
83 | * |
||
84 | * @param {string} name Child name |
||
85 | * @param {TreeNode<V>} child Child value |
||
86 | * @return {TreeNode<V>} Returns added child |
||
87 | */ |
||
88 | this.addChild = function (name, child) { |
||
89 | children[name] = child |
||
90 | return child |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Removes child |
||
95 | * |
||
96 | * @param {string} name Child name |
||
97 | * @return {TreeNode<V>|null} Removed child value |
||
98 | */ |
||
99 | this.removeChild = function (name) { |
||
100 | var value = children[name] |
||
101 | delete children[name] |
||
102 | return value || null |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Fetches current node child by it's name. |
||
107 | * |
||
108 | * @param {string} name Child name. |
||
109 | * |
||
110 | * @return {TreeNode.<V>|null} Existing node or nothing. |
||
111 | */ |
||
112 | this.child = function (name) { return children[name] || null } |
||
113 | |||
114 | /** |
||
115 | * Sets value for current node. |
||
116 | * |
||
117 | * @param {V} v |
||
118 | * @return {TreeNode} Returns current instance. |
||
119 | */ |
||
120 | this.set = function (v) { |
||
121 | value = v |
||
122 | return self |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Returns current node value. |
||
127 | * |
||
128 | * @return {V} |
||
129 | */ |
||
130 | this.get = function () { return typeof value === 'undefined' ? null : value } |
||
131 | |||
132 | /** |
||
133 | * Retrieves node at path |
||
134 | * |
||
135 | * @param {string[]} path |
||
136 | * @return {TreeNode<V>|null} |
||
137 | */ |
||
138 | this.traverse = function (path) { |
||
139 | if (path.length === 0) { |
||
140 | return this |
||
141 | } |
||
142 | var name = path.shift() |
||
143 | var cursor = this |
||
144 | while (name && cursor) { |
||
145 | cursor = cursor.child(name) |
||
146 | name = path.shift() |
||
147 | } |
||
148 | return cursor || null |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Picks branch as a list of nodes (starting from the root) that match |
||
153 | * requested path the most. In the best case, branch would contain root and |
||
154 | * all requested nodes, in the worst - branch would consist of root only. |
||
155 | * |
||
156 | * @param {string[]} path Branch path as list of child names. |
||
157 | * @return {[TreeNode<V>]} Picked branch. |
||
158 | */ |
||
159 | this.branch = function (path) { |
||
160 | var branch = [self] |
||
161 | var cursor = self |
||
162 | var name = path.shift() |
||
163 | while (name) { |
||
164 | cursor = cursor.child(name) |
||
165 | if (!cursor) { |
||
166 | break |
||
167 | } |
||
168 | branch.push(cursor) |
||
169 | name = path.shift() |
||
170 | } |
||
171 | return branch |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Puts provided value at designated path down the tree. |
||
176 | * |
||
177 | * @param {string[]} path Path at which value has to be set. |
||
178 | * @param {V} value Value to be set. |
||
179 | * @return {TreeNode<V>} Created or updated node. |
||
180 | */ |
||
181 | this.put = function (path, value) { |
||
182 | var cursor = self |
||
183 | var name = path.shift() |
||
184 | while (name) { |
||
185 | if (!cursor.child(name)) { |
||
186 | cursor.addChild(name, new TreeNode(null)) |
||
187 | } |
||
188 | cursor = cursor.child(name) |
||
189 | name = path.shift() |
||
190 | } |
||
191 | return cursor.set(value) |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Fetches value closest to provided path. If node contains falsey value, it |
||
196 | * is skipped. |
||
197 | * |
||
198 | * @param {string[]} path Path to pick the branch. |
||
199 | * @return {V|null} Target value or null |
||
200 | */ |
||
201 | this.retrieve = function (path) { |
||
202 | return self.branch(path).reverse().reduce(function (carrier, node) { |
||
203 | return carrier || node.get() |
||
204 | }, null) |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Removes node at path and returns it's value |
||
209 | * @param {string[]} path |
||
210 | * @return {V|undefined} |
||
211 | */ |
||
212 | this.remove = function (path) { |
||
213 | if (path.length === 0) { |
||
214 | throw new Error('You can\'t remove root node') |
||
215 | } |
||
216 | var segment = path.pop() |
||
217 | var node = this.traverse(path) |
||
218 | var child = node ? node.removeChild(segment) : null |
||
219 | return child ? child.get() : null |
||
220 | } |
||
221 | } |
||
222 | |||
373 |