Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. gvm.determineOverlaps = function () {
  2.     const tmp = {};
  3.     const overlaps = {};
  4.     // creates an empty object with each day of the week as key
  5.     shortDays.forEach(day => {
  6.       tmp[day] = [];
  7.       overlaps[day] = [];
  8.     });
  9.  
  10.     // filters the rules to each day
  11.     gvm.rules().forEach(rule => rule.days().forEach(day => tmp[day].push(rule)));
  12.  
  13.     const keys = Object.keys(tmp);
  14.     const values = Object.values(tmp);
  15.  
  16.     // loop over each day
  17.     values.forEach((rules, index) => {
  18.       // loops over all the rules of that specific day
  19.       rules.forEach(rule => {
  20.         // loops over all the other rules again to check if there are overlaps
  21.         rules.filter(_rule => rule !== _rule).forEach(_rule => {
  22.           if (_rule.start() <= rule.end() && _rule.end() >= rule.start() && !overlaps[keys[index]].includes(rule)) {
  23.             overlaps[keys[index]].push(rule);
  24.           };
  25.         });
  26.       });
  27.     });
  28.  
  29.     return overlaps;
  30.   };
  31.  
  32.   gvm.tijdelijk = function () {
  33.     const obj = {};
  34.     shortDays.forEach(day => obj[day] = []);
  35.  
  36.     //sorts the rules on the start time
  37.     gvm.rules(gvm.rules().sort((a,b) => (a.start() > b.start()) ? 1 : ((b.start() > a.start()) ? -1 : 0)));
  38.  
  39.     const overlaps = gvm.determineOverlaps();
  40.     const keys = Object.keys(overlaps);
  41.     const values = Object.values(overlaps);
  42.  
  43.     values.forEach((overlapRules, index) => {
  44.       overlapRules.forEach(overlapRule => {
  45.         const tmp = [];
  46.         overlapRules.filter(_overlapeRule => _overlapeRule.start() < overlapRule.end() && overlapRule.start() < _overlapeRule.end())
  47.                     .forEach(_overlapeRule => tmp.push(_overlapeRule));
  48.  
  49.         obj[keys[index]].push(tmp);
  50.       });
  51.     });
  52.  
  53.     return obj
  54.   };
  55.  
  56.   gvm.createOverlaps = function () {
  57.  
  58.     const obj = gvm.tijdelijk();
  59.     const keys = Object.keys(obj);
  60.     const values = Object.values(obj)
  61.  
  62.     let tmp = [];
  63.  
  64.     values.forEach((days, index) => {
  65.       const overlaps = [];
  66.       days.forEach(rules => {
  67.         const startTimes = rules.map(x => x.start());
  68.         const endTimes = rules.map(x => x.end());
  69.  
  70.         const duplicated = overlaps.find(x => x.start() === Math.min(...startTimes) && x.end() === Math.max(...endTimes));
  71.  
  72.         if (duplicated === undefined) {
  73.           overlaps.push(new Overlap(gvm, {
  74.             start: Math.min(...startTimes),
  75.             end: Math.max(...endTimes),
  76.             days: [keys[index]],
  77.             rules: ko.toJS(rules)
  78.           }));
  79.         };
  80.       });
  81.  
  82.       tmp = tmp.concat(overlaps);
  83.     });
  84.  
  85.     gvm.tmp(tmp)
  86.   };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement