2016-06-15 93 views
2

我正在使用node.js和作爲一個副項目我創建一個模塊,讀取.json文件,解析它然後創建目錄結構基於object properties & object values從對象屬性遞歸生成文件路徑

對象屬性(keys)將是路徑本身/到文件&對象的值是文件列表該路徑

我試圖通過對象向下遞歸,但我不知道我怎麼解壓路徑從每個對象的最內部對象

另外對象將是由用戶創建的dynamic

var path = 'c:/templates/<angular-app>'; 
 

 

 
var template = { 
 
    //outline of 'angular-app' 
 
    src:{ 
 
    jade:['main.jade'], 
 
    scripts:{ 
 
     modules:{ 
 
     render:['index.js'], 
 
     winodws:['index.js'], 
 
     header:['header.js' ,'controller.js'], 
 
     SCSS:['index.scss' ,'setup.scss'], 
 
     } 
 
    } 
 
    }, 
 
    compiled:['angular.js','angular-material.js' ,'fallback.js'], 
 
    built:{ 
 
    frontEnd:[],//if the array is empty then create the path anyways 
 
    backEnd:[], 
 
    assets:{ 
 
     fontAwesome:['font-awesome.css'], 
 
     img:[], 
 
     svg:[] 
 
    } 
 
    } 
 
} 
 

 
//desired result... 
 
let out = [ 
 
    'c:/template name/src/jade/main.jade', 
 
    'c:/template name/src/scripts/index.js', 
 
    'c:/template name/src/scripts/modules/render/index.js', 
 
    'c:/template name/compiled/angular.js', 
 
    'c:/template name/compiled/angular-material.js', 
 
    'c:/template name/compiled/fallback.js', 
 
    'c:/template name/built/frontEnd/', 
 
    'c:/template name/built/backEnd/', 
 
    //...ect... 
 
];

回答

1

這裏是你如何可以遞歸編寫這樣一個例子:

var path = 'c:/templates'; 
 

 
var template = { 
 
    //outline of 'angular-app' 
 
    src: { 
 
    jade: ['main.jade'], 
 
    scripts: { 
 
     modules: { 
 
     render: ['index.js'], 
 
     winodws: ['index.js'], 
 
     header: ['header.js', 'controller.js'], 
 
     SCSS: ['index.scss', 'setup.scss'], 
 
     } 
 
    } 
 
    }, 
 
    compiled: ['angular.js', 'angular-material.js', 'fallback.js'], 
 
    built: { 
 
    frontEnd: [], //if the array is empty then create the path anyways 
 
    backEnd: [], 
 
    assets: { 
 
     fontAwesome: ['font-awesome.css'], 
 
     img: [], 
 
     svg: [] 
 
    } 
 
    } 
 
} 
 

 
function recurse(item, path, result) { 
 
    //create default output if not passed-in 
 
    result = result || []; 
 

 
    //item is an object, iterate its properties 
 
    for (let key in item) { 
 
    let value = item[key]; 
 
    let newPath = path + "/" + key; 
 

 
    if (typeof value === "string") {  
 
     //if the property is a string, just append to the result 
 
     result.push(newPath + "/" + value);  
 
    } else if (Array.isArray(value)) {  
 
     //if an array 
 
     if (value.length === 0) { 
 
     //just the directory name 
 
     result.push(newPath + "/"); 
 
     } else { 
 
     //itearate all files 
 
     value.forEach(function(arrayItem) { 
 
      result.push(newPath + "/" + arrayItem); 
 
     }); 
 
     } 
 
    } else { 
 
     //this is an object, recursively build results 
 
     recurse(value, newPath, result); 
 
    } 
 
    } 
 

 
    return result; 
 
} 
 

 
var output = recurse(template, path); 
 
console.log(output);

1

我對這個問題是如下解決方案;

function getPaths(o, root = "", result = []) { 
 
    var ok = Object.keys(o); 
 
    return ok.reduce((a,k) => { var p = root + k + "/"; 
 
           typeof o[k] == "object" && o[k] !== null && 
 
           Array.isArray(o[k]) ? o[k].length ? o[k].forEach(f => a.push(p+=f)) 
 
                   : a.push(p) 
 
                : getPaths(o[k],p,a); 
 
           return a; 
 
          },result); 
 
} 
 
var path = 'c:/templates/', 
 
template = { 
 
    //outline of 'angular-app' 
 
    src:{ 
 
    jade:['main.jade'], 
 
    scripts:{ 
 
     modules:{ 
 
     render:['index.js'], 
 
     winodws:['index.js'], 
 
     header:['header.js' ,'controller.js'], 
 
     SCSS:['index.scss' ,'setup.scss'], 
 
     } 
 
    } 
 
    }, 
 
    compiled:['angular.js','angular-material.js' ,'fallback.js'], 
 
    built:{ 
 
    frontEnd:[],//if the array is empty then create the path anyways 
 
    backEnd:[], 
 
    assets:{ 
 
     fontAwesome:['font-awesome.css'], 
 
     img:[], 
 
     svg:[] 
 
    } 
 
    } 
 
}, 
 
    paths = getPaths(template,path); 
 
console.log(paths);

這只是一個所謂的getPaths簡單的功能,其實它有一個非常基本的遞歸運行。如果你的對象結構良好(不包括對象和數組以外的任何屬性,並且沒有空值),你甚至可以刪除typeof o[k] == "object" && o[k] !== null &&這一行。對不起,我非常規的縮進風格,但這是我發現如何處理代碼更容易做三元,邏輯快捷方式和ES6箭頭回調數組方法。