Last active
January 17, 2017 22:14
-
-
Save jdnichollsc/11dc24544442d9491640fa38f8ff5f29 to your computer and use it in GitHub Desktop.
Lodash examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Get a range of hours from a period | |
| * @param {String='8','4-8','2-5-9'} | |
| * @example | |
| * // returns [4,5,6,7] | |
| * @returns {Number|Array} Returns an array of numbers without include the last hour | |
| */ | |
| let periodRange = (period)=> { | |
| let periodArray = period.split('-'); | |
| return _.union(_.range(periodArray[0], periodArray[periodArray.length - 1]), [Number(periodArray[0])]); | |
| }; | |
| let otherQuantities = _.filter(product.quantities, (q) => { | |
| if(!(period.startDate <= q.period.endDate && period.endDate >= q.period.startDate)){ | |
| return false; | |
| } | |
| if(!((period.dayType.normal && q.period.dayType.normal) || (period.dayType.holiday && q.period.dayType.holiday))){ | |
| return false; | |
| } | |
| if(period.specificDays && q.period.specificDays && !_.intersection(q.period.specificDays, period.specificDays).length){ | |
| return false; | |
| } | |
| if(period.specificPeriods){ | |
| if(q.period.specificPeriods){ | |
| let sharedPeriods = _.filter(q.period.specificPeriods, period1 => { | |
| let range1 = periodRange(period1); | |
| return period.specificPeriods.some(period2 => { | |
| let range2 = periodRange(period2); | |
| return !!_.intersection(range1, range2).length; | |
| }); | |
| }); | |
| if(!sharedPeriods.length){ | |
| return false; | |
| } | |
| } | |
| else{ | |
| return false; | |
| } | |
| } | |
| else{ | |
| if(!((period.loadType.high && q.period.loadType.high) || (period.loadType.medium && q.period.loadType.medium) || (period.loadType.low && q.period.loadType.low))) | |
| return false; | |
| } | |
| if(currentQuantity && currentQuantity.index === index){ | |
| return false; | |
| } | |
| if(newQuantity.companies && q.companies && !_.intersection(q.companies, newQuantity.companies).length){ | |
| return false; | |
| } | |
| return true; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment