Created
May 17, 2017 19:09
-
-
Save ParthBarot-BoTreeConsulting/c15c9cf777f38af015a3094f4769067e to your computer and use it in GitHub Desktop.
JS Refactoring - Final version
This file contains 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
// Final code | |
var MayApp = {}; | |
MyApp.Base = { | |
data: { | |
rules: { | |
FirstSet:{ | |
"0-12": { | |
"0-12000":{ | |
"0-90" :{ | |
fixedVal: 0.4 | |
}, | |
"91-*" :{ | |
fixedVal: 0.7 | |
} | |
}, | |
"12001-*":{ | |
"0-78" :{ | |
fixedVal: 0.4 | |
}, | |
"78.01-90" :{ | |
fixedVal: 0.7 | |
}, | |
"91-*" :{ | |
fixedVal: 0.9 | |
} | |
} | |
}, | |
"13-*": { | |
"0-12000":{ | |
"0-95" :{ | |
fixedVal: 0.8 | |
}, | |
"96-*" :{ | |
fixedVal: 0.7 | |
} | |
}, | |
"12001-*":{ | |
"0-95" :{ | |
fixedVal: 1.0 | |
}, | |
"96-*" :{ | |
fixedVal: 1.1 | |
} | |
} | |
} | |
} | |
} | |
}, | |
_isDataRangeMatches: function(dataVal,dataRange){ | |
if(dataRange[1] == '*'){ | |
return (dataVal >= parseInt(dataRange[0])); | |
} else { | |
return (dataVal >= parseInt(dataRange[0]) && dataVal <= parseInt(dataRange[1])); | |
} | |
}, | |
_enumerateFirstSetRules: function(dataValArr,jsonData){ | |
var dataVal = dataValArr.pop(); | |
for (var key in jsonData){ | |
if(jsonData.hasOwnProperty(key) && key != 'fixedVal'){ | |
var keyRange = key.split('-'); | |
if(this._isDataRangeMatches(dataVal,keyRange)){ | |
if(jsonData.hasOwnProperty('fixedVal')){ | |
return jsonData.fixedVal; | |
}else{ | |
return this._enumerateFirstSetRules(dataValArr, jsonData[key]); | |
} | |
} | |
}else if(jsonData.hasOwnProperty('fixedVal')){ | |
return jsonData.fixedVal; | |
} | |
} | |
}, | |
getFixedVal: function(myDataVal, amount, calculatedVal){ | |
var rulesData = MyApp.Base.data.rules.FirstSet; | |
return this._enumerateFirstSetRules([calculatedVal,amount,myDataVal], rulesData); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment