我的目标是通过文件夹树创建最佳算法进行递归搜索,并处理每个"级别" 的所有目录。并在JavaScript上实现它。
我写了这段代码示例:
function deepSearch ( dirPath, startPoint, searchRequest ) {
var listDir = getListDir(dirPath), // pseudo function for getting dir's content
currentElemsAmount = listDir.length, // amount of file objects
i = startPoint;
deepSearch.scale = deepSearch.scale || [];
deepSearch.result = deepSearch.result || [];
deepSearch.request = deepSearch.request || searchRequest || '';
for ( ; i < currentElemsAmount; i += 1 ) {
if ( isFile(listDir[i]) ) { // pseudo function to detect if file object is essentially file
// compare some file attributes with search request
if ( listDir[i].name.match(RegExp(searchRequest)) ) { // pseudo code
deepSearch.result.push(listDir[i]);
}
} else {
deepSearch.scale.push(i);
deepSearch(newDirPath, 0); // pseudo code
}
}
if ( deepSearch.scale.length ) {
deepSearch(goBackInPathOnOneLevel(dirPath, deepSearch.scale.pop() + 1)); // pseudo function to go up in tree
} else {
return deepSearch.result;
}
}
我该如何改进?有可能让它变得更好吗?关于替代方法和解决方案的内容是什么?它完全正确吗?
非常感谢您的意见。
感谢。