Commit 3bba4f154e879aeecf7ddf6876d01aa5cff244a0

Authored by Aresn
Committed by GitHub
2 parents 46f37a14 5ab32e9c

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 189 export {findComponentUpward};
190 190  
191 191 // Find component downward
192   -function findComponentDownward (context, componentName) {
  192 +export function findComponentDownward (context, componentName) {
193 193 const childrens = context.$children;
194 194 let children = null;
195 195  
196 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 198 const name = child.$options.name;
207 199 if (name === componentName) {
208 200 children = child;
... ... @@ -215,27 +207,15 @@ function findComponentDownward (context, componentName) {
215 207 }
216 208 return children;
217 209 }
218   -export {findComponentDownward};
219 210  
220 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 220 /* istanbul ignore next */
241 221 const trim = function(string) {
... ... @@ -297,4 +277,4 @@ export function removeClass(el, cls) {
297 277 if (!el.classList) {
298 278 el.className = trim(curClass);
299 279 }
300   -}
301 280 \ No newline at end of file
  281 +}
... ...