Advertisement
fbinnzhivko

Untitled

Dec 5th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function angularParser(data) {
  2.     let apps = new Map;
  3.     let orphanApps = new Map;
  4.     let regEx = /^\$(app|controller|model|view)='([^']+)'(?:&app='([^']+)')?$/;
  5.  
  6.     for (let line of data) {
  7.         let match = line.match(regEx);
  8.         if (match !== null) {
  9.             let [type, name, appName] = [match[1], match[2], match[3]];
  10.  
  11.             if (appName === undefined) {
  12.                 // Add new app
  13.                 apps.set(name, {controllers: [], models: [], views: []});
  14.                 if (orphanApps.has(name)) {
  15.                     // transfer data from non-existent app log
  16.                     apps.get(name)['controllers'] = apps.get(name)['controllers'].concat(orphanApps.get(name)['controllers']);
  17.                     apps.get(name)['models'] = apps.get(name)['models'].concat(orphanApps.get(name)['models']);
  18.                     apps.get(name)['views'] = apps.get(name)['views'].concat(orphanApps.get(name)['views']);
  19.                 }
  20.             } else {
  21.                 // add model, view , controller to app
  22.                 if (!apps.has(appName)) {
  23.                     // non-existing app
  24.                     if (!orphanApps.has(appName))
  25.                         orphanApps.set(appName, {controllers: [], models: [], views: []});
  26.                     orphanApps.get(appName)[type + 's'].push(name);
  27.                 } else {
  28.                     // existing app
  29.                     apps.get(appName)[type + 's'].push(name);
  30.                 }
  31.             }
  32.         }
  33.     }
  34.  
  35.     let result = {};
  36.  
  37.     function sortApps(appA, appB, allApps) {
  38.         let appAControllersCount = allApps.get(appA).controllers.length;
  39.         let appBControllersCount = allApps.get(appB).controllers.length;
  40.         if (appAControllersCount == appBControllersCount) {
  41.             let appAModelsCount = allApps.get(appA).models.length;
  42.             let appBModelsCount = allApps.get(appB).models.length;
  43.  
  44.             return appAModelsCount - appBModelsCount;
  45.         }
  46.  
  47.         return appBControllersCount - appAControllersCount;
  48.     }
  49.  
  50.     let sortedApps = [...apps.keys()].sort((a1, a2) => sortApps(a1, a2, apps));
  51.     for (let app of sortedApps) {
  52.         result[app] = {controllers: [], models: [], views: []};
  53.         result[app].controllers = apps.get(app).controllers.sort((a, b) => a.localeCompare(b));
  54.         result[app].models = apps.get(app).models.sort((a, b) => a.localeCompare(b));
  55.         result[app].views = apps.get(app).views.sort((a, b) => a.localeCompare(b));
  56.     }
  57.  
  58.     console.log(JSON.stringify(result));
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement