Skip to content

Commit

Permalink
Merge pull request #17 from damian-pastorini/v0.16.0
Browse files Browse the repository at this point in the history
- Reldens - Modifiers
  • Loading branch information
damian-pastorini authored Aug 15, 2022
2 parents 230d07b + 44bcac7 commit aaeed9f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 89 deletions.
68 changes: 26 additions & 42 deletions lib/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,38 @@ class Calculator

calculateNewValue(originalValue, operation, operationValue, revert)
{
// @NOTE: the following operations needs an opposite method for the revert.
if(
(operation === ModifierConst.OPS.INC && !revert)
|| (operation === ModifierConst.OPS.DEC && revert)
){
originalValue = originalValue + operationValue;
let isIncrease = operation === ModifierConst.OPS.INC;
let isDecrease = operation === ModifierConst.OPS.DEC;
if((isIncrease && !revert) || (isDecrease && revert)){
return originalValue + operationValue;
}
if(
(operation === ModifierConst.OPS.DEC && !revert)
|| (operation === ModifierConst.OPS.INC && revert)
){
originalValue = originalValue - operationValue;
if((isDecrease && !revert) || (isIncrease && revert)){
return originalValue - operationValue;
}
if(
(operation === ModifierConst.OPS.MUL && !revert)
|| (operation === ModifierConst.OPS.DIV && revert)
){
originalValue = originalValue * operationValue;
let isMultiplication = operation === ModifierConst.OPS.MUL;
let isDivision = operation === ModifierConst.OPS.DIV;
if((isMultiplication && !revert) || (isDivision && revert)){
return originalValue * operationValue;
}
if(
(operation === ModifierConst.OPS.DIV && !revert)
|| (operation === ModifierConst.OPS.MUL && revert)
){
originalValue = originalValue / operationValue;
if((isDivision && !revert) || (isMultiplication && revert)){
return originalValue / operationValue;
}
if(
(operation === ModifierConst.OPS.INC_P && !revert)
|| (operation === ModifierConst.OPS.DEC_P && revert)
){
if(revert){
let revertValue = Math.ceil((originalValue / (100 - operationValue)) * 100 - originalValue);
originalValue = originalValue + revertValue;
} else {
originalValue = originalValue + Math.round(originalValue * operationValue / 100);
}
let isIncreasePercentage = operation === ModifierConst.OPS.INC_P;
if((isIncreasePercentage && !revert)){
return originalValue + Math.round(originalValue * operationValue / 100);
}
if(
(operation === ModifierConst.OPS.DEC_P && !revert)
|| (operation === ModifierConst.OPS.INC_P && revert)
){
if(revert){
let revertValue = Math.ceil(originalValue - (originalValue / (100 - operationValue)) * 100);
originalValue = originalValue + revertValue;
} else {
originalValue = originalValue - Math.round(originalValue * operationValue / 100);
}
if(isIncreasePercentage && revert){
let revertValue = Math.ceil(originalValue - (originalValue / (100 - operationValue)) * 100);
return originalValue + revertValue;
}
let isDecreasePercentage = operation === ModifierConst.OPS.DEC_P;
if((isDecreasePercentage && !revert)){
return originalValue - Math.round(originalValue * operationValue / 100);
}
if(isDecreasePercentage && revert){
let revertValue = Math.ceil((originalValue / (100 - operationValue)) * 100 - originalValue);
return originalValue + revertValue;
}
return originalValue;
}

}
Expand Down
27 changes: 16 additions & 11 deletions lib/condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ class Condition
{

constructor(props)
{
this.validateParameters(props);
this.key = props.key;
this.propertyKey = props.propertyKey;
this.conditional = props.conditional;
this.type = sc.get(props, 'type', ModifierConst.TYPES.INT);
this.value = this.parseValue(props.value);
this.propertyManager = new PropertyManager();
}

validateParameters(props)
{
if(!sc.hasOwn(props, 'key')){
ErrorManager.error('Missing Condition key.');
Expand All @@ -25,12 +36,6 @@ class Condition
if(!sc.hasOwn(props, 'value')){
ErrorManager.error('Missing Condition value.');
}
this.key = props.key;
this.propertyKey = props.propertyKey;
this.conditional = props.conditional;
this.type = sc.get(props, 'type', ModifierConst.TYPES.INT);
this.value = this.parseValue(props.value);
this.propertyManager = new PropertyManager();
}

parseValue(value)
Expand All @@ -46,12 +51,12 @@ class Condition

isValidOn(targetObject, overrideVal)
{
if (typeof this[this.conditional] === 'function'){
let value = overrideVal || this.value;
let targetPropValue = this.propertyManager.getPropertyValue(targetObject, this.propertyKey);
return this[this.conditional](targetPropValue, value);
if(!sc.isFunction(this, this.conditional)){
ErrorManager.error('Condition operation is not a function.');
}
ErrorManager.error('Condition operation is not a function.');
let value = overrideVal || this.value;
let targetPropValue = this.propertyManager.getPropertyValue(targetObject, this.propertyKey);
return this[this.conditional](targetPropValue, value);
}

eq(targetPropValue, value)
Expand Down
17 changes: 4 additions & 13 deletions lib/modifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class Modifier
this.basePropertyKey = sc.get(props, 'basePropertyKey', this.propertyKey);
this.operation = props.operation;
this.type = sc.get(props, 'type', ModifierConst.TYPES.INT);
this.value = this.parseValue(props.value);
this.originalValue = props.value;
this.value = this.parseValue(props.value);
this.target = sc.get(props, 'target', false);
this.minValue = sc.hasOwn(props, 'minValue') ? this.parseValue(props.minValue) : false;
this.maxValue = sc.hasOwn(props, 'maxValue') ? this.parseValue(props.maxValue) : false;
Expand Down Expand Up @@ -98,11 +98,7 @@ class Modifier
let newValue = this.getModifiedValue(revert, useBasePropertyToGetValue);
let applyToProp = applyOnBaseProperty ? this.basePropertyKey : this.propertyKey;
this.setOwnerProperty(applyToProp, newValue);
if(revert){
this.state = ModifierConst.MOD_REVERTED;
} else {
this.state = ModifierConst.MOD_APPLIED;
}
this.state = revert ? ModifierConst.MOD_REVERTED : ModifierConst.MOD_APPLIED;
}

validateConditions(target)
Expand All @@ -127,19 +123,14 @@ class Modifier
let propertyValue = this.calculator.calculateNewValue(currentValue, this.operation, this.value, revert);
// parse object specific operations:
if(this.operation === ModifierConst.OPS.SET || this.operation === ModifierConst.OPS.SET_N){
if(revert){
propertyValue = false;
} else {
propertyValue = this.value;
}
propertyValue = revert ? false : this.value;
}
if(this.operation === ModifierConst.OPS.METHOD){
// this allows you to extend a modifier and set your own calculation/application method:
if(!sc.hasOwn(this, this.value) || typeof this[this.value] !== 'function'){
ErrorManager.error(['Modifier error:', this, 'Undefined method:', this.value]);
} else {
propertyValue = this[this.value](this, propertyValue);
}
propertyValue = this[this.value](this, propertyValue);
}
// apply modifier min and max values if required:
return this.applyModifierLimits(propertyValue);
Expand Down
47 changes: 26 additions & 21 deletions lib/property-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,41 @@ const { ErrorManager, sc } = require('@reldens/utils');
class PropertyManager
{

getPropertyValue(propertyOwner, propName)
getPropertyValue(propertyOwner, propertyString)
{
return this.manageOwnerProperty(propertyOwner, propName);
return this.manageOwnerProperty(propertyOwner, propertyString);
}

setOwnerProperty(propertyOwner, propName, value)
setOwnerProperty(propertyOwner, propertyString, value)
{
return this.manageOwnerProperty(propertyOwner, propName, value);
return this.manageOwnerProperty(propertyOwner, propertyString, value);
}

manageOwnerProperty(propertyOwner, propName, value)
manageOwnerProperty(propertyOwner, propertyString, value)
{
if(propName.indexOf('/') !== -1){
let propArray = propName.split('/');
propName = propArray.pop();
for(let prop of propArray){
if(!sc.hasOwn(propertyOwner, prop)){
ErrorManager.error([
'Property not found in path:', propName, prop,
'Property owner:', propertyOwner
]);
}
propertyOwner = propertyOwner[prop];
}
let propertyPathParts = propertyString.split('/');
let property = this.extractDeepProperty(propertyOwner, propertyPathParts);
let propertyKey = propertyPathParts.pop();
let isValidValue = 'undefined' !== typeof value;
if(isValidValue){
property[propertyKey] = value;
}
if(typeof value !== 'undefined'){
propertyOwner[propName] = value;
return propertyOwner;
return property[propertyKey];
}

extractDeepProperty(propertyOwner, propertyPathParts)
{
let propertyName = propertyPathParts.shift();
if(!sc.hasOwn(propertyOwner, propertyName)){
ErrorManager.error([
'Property not found in path:', propertyName,
'Property owner:', propertyOwner
]);
}
if(1 < propertyPathParts.length){
return this.extractDeepProperty(propertyOwner[propertyName], propertyPathParts);
}
return propertyOwner[propName];
return propertyOwner[propertyName];
}

}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@reldens/modifiers",
"scope": "@reldens",
"version": "0.11.0",
"version": "0.16.0",
"description": "Reldens - Modifiers",
"author": "Damian A. Pastorini",
"license": "MIT",
Expand Down Expand Up @@ -33,6 +33,6 @@
"url": "https://github.com/damian-pastorini/reldens-modifiers/issues"
},
"dependencies": {
"@reldens/utils": "^0.11.0"
"@reldens/utils": "^0.16.0"
}
}

0 comments on commit aaeed9f

Please sign in to comment.