Commit 3bba4f154e879aeecf7ddf6876d01aa5cff244a0
Committed by
GitHub

Merge pull request #2091 from SergioCrisostomo/refactor-findComponentDownwards
remove unused code, small refactor
Showing
1 changed file
with
9 additions
and
29 deletions
Show diff stats
src/utils/assist.js
@@ -189,20 +189,12 @@ function findComponentUpward (context, componentName, componentNames) { | @@ -189,20 +189,12 @@ function findComponentUpward (context, componentName, componentNames) { | ||
189 | export {findComponentUpward}; | 189 | export {findComponentUpward}; |
190 | 190 | ||
191 | // Find component downward | 191 | // Find component downward |
192 | -function findComponentDownward (context, componentName) { | 192 | +export function findComponentDownward (context, componentName) { |
193 | const childrens = context.$children; | 193 | const childrens = context.$children; |
194 | let children = null; | 194 | let children = null; |
195 | 195 | ||
196 | if (childrens.length) { | 196 | if (childrens.length) { |
197 | - childrens.forEach(child => { | ||
198 | - const name = child.$options.name; | ||
199 | - if (name === componentName) { | ||
200 | - children = child; | ||
201 | - } | ||
202 | - }); | ||
203 | - | ||
204 | - for (let i = 0; i < childrens.length; i++) { | ||
205 | - const child = childrens[i]; | 197 | + for (const child of childrens) { |
206 | const name = child.$options.name; | 198 | const name = child.$options.name; |
207 | if (name === componentName) { | 199 | if (name === componentName) { |
208 | children = child; | 200 | children = child; |
@@ -215,27 +207,15 @@ function findComponentDownward (context, componentName) { | @@ -215,27 +207,15 @@ function findComponentDownward (context, componentName) { | ||
215 | } | 207 | } |
216 | return children; | 208 | return children; |
217 | } | 209 | } |
218 | -export {findComponentDownward}; | ||
219 | 210 | ||
220 | // Find components downward | 211 | // Find components downward |
221 | -function findComponentsDownward (context, componentName, components = []) { | ||
222 | - const childrens = context.$children; | ||
223 | - | ||
224 | - if (childrens.length) { | ||
225 | - childrens.forEach(child => { | ||
226 | - const name = child.$options.name; | ||
227 | - const childs = child.$children; | ||
228 | - | ||
229 | - if (name === componentName) components.push(child); | ||
230 | - if (childs.length) { | ||
231 | - const findChilds = findComponentsDownward(child, componentName, components); | ||
232 | - if (findChilds) components.concat(findChilds); | ||
233 | - } | ||
234 | - }); | ||
235 | - } | ||
236 | - return components; | 212 | +export function findComponentsDownward (context, componentName) { |
213 | + return context.$children.reduce((components, child) => { | ||
214 | + if (child.$options.name === componentName) components.push(child); | ||
215 | + const foundChilds = findComponentsDownward(child, componentName); | ||
216 | + return components.concat(foundChilds); | ||
217 | + }, []); | ||
237 | } | 218 | } |
238 | -export {findComponentsDownward}; | ||
239 | 219 | ||
240 | /* istanbul ignore next */ | 220 | /* istanbul ignore next */ |
241 | const trim = function(string) { | 221 | const trim = function(string) { |
@@ -297,4 +277,4 @@ export function removeClass(el, cls) { | @@ -297,4 +277,4 @@ export function removeClass(el, cls) { | ||
297 | if (!el.classList) { | 277 | if (!el.classList) { |
298 | el.className = trim(curClass); | 278 | el.className = trim(curClass); |
299 | } | 279 | } |
300 | -} | ||
301 | \ No newline at end of file | 280 | \ No newline at end of file |
281 | +} |