Skip to content

Commit

Permalink
- Reldens - Modifiers
Browse files Browse the repository at this point in the history
- Merge pull request #19 from damian-pastorini/v0.19.1
  • Loading branch information
damian-pastorini authored Jun 15, 2023
2 parents a19dbb1 + dd984da commit ad828de
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 31 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@
/ts-node*
/v8-compile-cache*
/dev
package-lock.json
2 changes: 1 addition & 1 deletion lib/condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Condition

isValidOn(targetObject, overrideVal)
{
if(!sc.isFunction(this, this.conditional)){
if(!sc.isObjectFunction(this, this.conditional)){
ErrorManager.error('Condition operation is not a function.');
}
let value = overrideVal || this.value;
Expand Down
11 changes: 10 additions & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ module.exports = {
INT: 'int',
STRING: 'string'
},
MOD_MISSING_KEY: 'mk',
MOD_MISSING_PROPERTY_KEY: 'mpk',
MOD_MISSING_OPERATION: 'mo',
MOD_MISSING_VALUE: 'mv',
MOD_READY: 'mre',
MOD_APPLIED: 'ma',
MOD_REVERTED: 'mr'
MOD_REVERTED: 'mr',
MOD_UNDEFINED_TARGET: 'mut',
MOD_INVALID_CONDITIONS: 'mic',
MOD_MISSING_CONDITION_INSTANCE: 'mmci',
MOD_MODIFIER_ERROR: 'me',
};
53 changes: 32 additions & 21 deletions lib/modifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,17 @@
*
*/

const { ErrorManager, sc } = require('@reldens/utils');
const Calculator = require('./calculator');
const Condition = require('./condition');
const PropertyManager = require('./property-manager');
const ModifierConst = require('./constants');
const { Logger, sc } = require('@reldens/utils');

class Modifier
{

constructor(props)
{
if(
!sc.hasOwn(props, 'key')
|| (!sc.hasOwn(props, 'propertyKey') && !sc.hasOwn(props, 'property_key'))
|| !sc.hasOwn(props, 'operation')
|| !sc.hasOwn(props, 'value')
){
ErrorManager.error([
'Undefined required properties {key, propertyKey (property_key), operation, value} in:',
props
]);
}
this.key = props.key;
this.propertyKey = sc.get(props, 'propertyKey', props.property_key);
this.basePropertyKey = sc.get(props, 'basePropertyKey', this.propertyKey);
Expand All @@ -38,14 +27,30 @@ class Modifier
this.maxValue = sc.hasOwn(props, 'maxValue') ? this.parseValue(props.maxValue) : false;
this.minProperty = sc.get(props, 'minProperty', false);
this.maxProperty = sc.get(props, 'maxProperty', false);
// array of conditions objects:
this.conditions = sc.get(props, 'conditions', []);
this.conditionsOnRevert = sc.get(props, 'conditionsOnRevert', false);
this.state = false;
this.state = this.determineState(props);
this.calculator = new Calculator();
this.propertyManager = new PropertyManager();
}

determineState(props)
{
if(!sc.hasOwn(props, 'key')){
return ModifierConst.MOD_MISSING_KEY;
}
if(!sc.hasOwn(props, 'propertyKey') && !sc.hasOwn(props, 'property_key')){
return ModifierConst.MOD_MISSING_PROPERTY_KEY;
}
if(!sc.hasOwn(props, 'operation')){
return ModifierConst.MOD_MISSING_OPERATION;
}
if(!sc.hasOwn(props, 'value')){
return ModifierConst.MOD_MISSING_VALUE;
}
return ModifierConst.MOD_READY;
}

parseValue(value)
{
if(this.type === ModifierConst.TYPES.INT){
Expand All @@ -59,12 +64,12 @@ class Modifier

apply(target, useBasePropertyToGetValue, applyOnBaseProperty)
{
this.execute(target, false, useBasePropertyToGetValue, applyOnBaseProperty);
return this.execute(target, false, useBasePropertyToGetValue, applyOnBaseProperty);
}

revert(target, useBasePropertyToGetValue, applyOnBaseProperty)
{
this.execute(target, true, useBasePropertyToGetValue, applyOnBaseProperty);
return this.execute(target, true, useBasePropertyToGetValue, applyOnBaseProperty);
}

/**
Expand All @@ -78,16 +83,16 @@ class Modifier
*/
execute(target, revert = false, useBasePropertyToGetValue = false, applyOnBaseProperty = false)
{
// return error if target is not present at all:
if(!this.target && !target){
ErrorManager.error('Undefined target.');
this.state = ModifierConst.MOD_UNDEFINED_TARGET;
return false;
}
if(
this.conditions.length
&& !this.validateConditions(target)
&& (!revert || (revert && this.conditionsOnRevert))
){
// conditions not satisfied:
// invalid conditions:
return false;
}
// override target if provided:
Expand All @@ -99,15 +104,19 @@ class Modifier
let applyToProp = applyOnBaseProperty ? this.basePropertyKey : this.propertyKey;
this.setOwnerProperty(applyToProp, newValue);
this.state = revert ? ModifierConst.MOD_REVERTED : ModifierConst.MOD_APPLIED;
return true;
}

validateConditions(target)
{
for(let condition of this.conditions){
if(condition instanceof Condition){
ErrorManager.error(['Missing Condition instance.', (typeof condition), 'was specified.']);
Logger.error(['Missing Condition instance.', (typeof condition), 'was specified.']);
this.state = ModifierConst.MOD_MISSING_CONDITION_INSTANCE;
return false;
}
if(!condition.isValidOn(target)){
this.state = ModifierConst.MOD_INVALID_CONDITIONS;
return false;
}
}
Expand All @@ -128,7 +137,9 @@ class Modifier
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]);
Logger.error(['Modifier error:', this, 'Undefined method:', this.value]);
this.state = ModifierConst.MOD_MODIFIER_ERROR;
return false;
}
propertyValue = this[this.value](this, propertyValue);
}
Expand Down
9 changes: 4 additions & 5 deletions lib/property-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
*/

const { ErrorManager, sc } = require('@reldens/utils');
const { ErrorManager, Logger, sc } = require('@reldens/utils');

class PropertyManager
{
Expand Down Expand Up @@ -35,10 +35,9 @@ class PropertyManager
{
let propertyName = propertyPathParts.shift();
if(!sc.hasOwn(propertyOwner, propertyName)){
ErrorManager.error([
'Property not found in path:', propertyName,
'Property owner:', propertyOwner
]);
let message = 'Property "'+propertyName+'" not found in path parts: [' + propertyPathParts.join(',') + ']';
Logger.error(message, {'Property owner': propertyOwner});
ErrorManager.error(message);
}
if(1 < propertyPathParts.length){
return this.extractDeepProperty(propertyOwner[propertyName], propertyPathParts);
Expand Down
63 changes: 63 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.17.0",
"version": "0.19.1",
"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.16.0"
"@reldens/utils": "^0.20.0"
}
}

0 comments on commit ad828de

Please sign in to comment.