Skip to content

Commit

Permalink
Merge pull request #6 from damian-pastorini/develop
Browse files Browse the repository at this point in the history
- Reldens - Modifiers
  • Loading branch information
damian-pastorini authored Oct 10, 2020
2 parents 15b4357 + 148c4a6 commit 09e0557
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 30 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ By implementing this package you will get:
- Conditions array can be set on a Modifier to avoid the execution.
- Calculator class which apply or revert the values.
- The PropertyManager class to easily get or set a value in any object property following a path.
- Shortcuts to common used functions.

Need something specific?

Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*/

module.exports = {
Modifier: require('./lib/modifier'),
Condition: require('./lib/condition'),
Calculator: require('./lib/calculator'),
PropertyManager: require('./lib/property-manager'),
ModifierConst: require('./lib/constants')
Condition: require('./lib/condition'),
Modifier: require('./lib/modifier'),
ModifierConst: require('./lib/constants'),
PropertyManager: require('./lib/property-manager')
};
8 changes: 7 additions & 1 deletion lib/calculator.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
*
* Reldens - Calculator
*
*/

const ModifierConst = require('./constants');

class Calculator
Expand Down Expand Up @@ -57,4 +63,4 @@ class Calculator

}

module.exports = Calculator;
module.exports = Calculator;
31 changes: 25 additions & 6 deletions lib/condition.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
const { ErrorManager } = require('@reldens/utils');
/**
*
* Reldens - Condition
*
*/

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

class Condition
{

constructor(props)
{
if(!{}.hasOwnProperty.call(props, 'key')){
if(!sc.hasOwn(props, 'key')){
ErrorManager.error('Missing Condition key.');
}
if(!{}.hasOwnProperty.call(props, 'propertyKey')){
if(!sc.hasOwn(props, 'propertyKey')){
ErrorManager.error('Missing Condition propertyKey.');
}
if(!{}.hasOwnProperty.call(props, 'conditional')){
if(!sc.hasOwn(props, 'conditional')){
ErrorManager.error('Missing Condition conditional.');
}
if(!{}.hasOwnProperty.call(props, 'value')){
if(!sc.hasOwn(props, 'value')){
ErrorManager.error('Missing Condition value.');
}
this.key = props.key;
this.propertyKey = props.propertyKey;
this.conditional = props.conditional;
this.value = props.value;
this.type = sc.hasOwn(props, 'type') ? props.type : ModifierConst.TYPES.INT;
this.value = this.parseValue(props.value);
this.propertyManager = new PropertyManager();
}

parseValue(value)
{
if(this.type === ModifierConst.TYPES.INT){
value = Number(value);
}
if(this.type === ModifierConst.TYPES.STRING){
value = String(value);
}
return value;
}

isValidOn(targetObject, overrideVal)
{
if (typeof this[this.conditional] === 'function'){
Expand Down
4 changes: 4 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ module.exports = {
LE: 'le',
GE: 'ge'
},
TYPES: {
INT: 'int',
STRING: 'string'
},
MOD_APPLIED: 'ma',
MOD_REVERTED: 'mr'
};
40 changes: 26 additions & 14 deletions lib/modifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
*/

const { ErrorManager } = require('@reldens/utils');
const { ErrorManager, sc } = require('@reldens/utils');
const Calculator = require('./calculator');
const Condition = require('./condition');
const PropertyManager = require('./property-manager');
Expand All @@ -16,10 +16,10 @@ class Modifier
constructor(props)
{
if(
!{}.hasOwnProperty.call(props, 'key')
|| (!{}.hasOwnProperty.call(props, 'propertyKey') && !{}.hasOwnProperty.call(props, 'property_key'))
|| !{}.hasOwnProperty.call(props, 'operation')
|| !{}.hasOwnProperty.call(props, 'value')
!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:',
Expand All @@ -29,22 +29,34 @@ class Modifier
this.key = props.key;
this.propertyKey = props.propertyKey ? props.propertyKey : props.property_key;
this.operation = props.operation;
this.value = props.value;
this.type = sc.hasOwn(props, 'type') ? props.type : ModifierConst.TYPES.INT;
this.value = this.parseValue(props.value);
this.originalValue = props.value;
this.target = {}.hasOwnProperty.call(props, 'target') ? props.target : false;
this.minValue = {}.hasOwnProperty.call(props, 'minValue') ? props.minValue : false;
this.maxValue = {}.hasOwnProperty.call(props, 'maxValue') ? props.maxValue : false;
this.minProperty = {}.hasOwnProperty.call(props, 'minProperty') ? props.minProperty : false;
this.maxProperty = {}.hasOwnProperty.call(props, 'maxProperty') ? props.maxProperty : false;
this.target = sc.hasOwn(props, 'target') ? props.target : false;
this.minValue = sc.hasOwn(props, 'minValue') ? props.minValue : false;
this.maxValue = sc.hasOwn(props, 'maxValue') ? props.maxValue : false;
this.minProperty = sc.hasOwn(props, 'minProperty') ? props.minProperty : false;
this.maxProperty = sc.hasOwn(props, 'maxProperty') ? props.maxProperty : false;
// array of conditions objects:
this.conditions = {}.hasOwnProperty.call(props, 'conditions') ? props.conditions : [];
this.conditionsOnRevert = {}.hasOwnProperty.call(props, 'conditionsOnRevert')
this.conditions = sc.hasOwn(props, 'conditions') ? props.conditions : [];
this.conditionsOnRevert = sc.hasOwn(props, 'conditionsOnRevert')
? props.conditionsOnRevert : false;
this.state = false;
this.calculator = new Calculator();
this.propertyManager = new PropertyManager();
}

parseValue(value)
{
if(this.type === ModifierConst.TYPES.INT){
value = Number(value);
}
if(this.type === ModifierConst.TYPES.STRING){
value = String(value);
}
return value;
}

apply(target)
{
this.execute(target, false);
Expand Down Expand Up @@ -112,7 +124,7 @@ class Modifier
}
if(this.operation === ModifierConst.OPS.METHOD){
// this allows you to extend a modifier and set your own calculation/application method:
if(!{}.hasOwnProperty.call(this, this.value) || typeof this[this.value] !== 'function'){
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);
Expand Down
12 changes: 9 additions & 3 deletions lib/property-manager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const { ErrorManager } = require('@reldens/utils');
/**
*
* Reldens - PropertyManager
*
*/

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

class PropertyManager
{
Expand All @@ -19,7 +25,7 @@ class PropertyManager
let propArray = propName.split('/');
propName = propArray.pop();
for(let prop of propArray){
if(!{}.hasOwnProperty.call(propertyOwner, prop)){
if(!sc.hasOwn(propertyOwner, prop)){
ErrorManager.error([
'Property not found in path:', propName, prop,
'Property owner:', propertyOwner
Expand All @@ -37,4 +43,4 @@ class PropertyManager

}

module.exports = PropertyManager;
module.exports = PropertyManager;
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.3.2",
"version": "0.4.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.1.8"
"@reldens/utils": "^0.2.0"
}
}

0 comments on commit 09e0557

Please sign in to comment.