Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ParthBarot-BoTreeConsulting/c15c9cf777f38af015a3094f4769067e to your computer and use it in GitHub Desktop.
Save ParthBarot-BoTreeConsulting/c15c9cf777f38af015a3094f4769067e to your computer and use it in GitHub Desktop.
JS Refactoring - Final version
// 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