From 148c4a6d6860429b4cac510a1bdd959f4fd98db1 Mon Sep 17 00:00:00 2001 From: "Damian A. Pastorini" Date: Sat, 10 Oct 2020 17:30:05 +0200 Subject: [PATCH] - Reldens - Modifiers --- README.md | 1 + index.js | 8 ++++---- lib/calculator.js | 8 +++++++- lib/condition.js | 31 +++++++++++++++++++++++++------ lib/constants.js | 4 ++++ lib/modifier.js | 40 ++++++++++++++++++++++++++-------------- lib/property-manager.js | 12 +++++++++--- package.json | 4 ++-- 8 files changed, 78 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index bcdfdfd..9d06168 100644 --- a/README.md +++ b/README.md @@ -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? diff --git a/index.js b/index.js index 9124f5d..c279617 100644 --- a/index.js +++ b/index.js @@ -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') }; diff --git a/lib/calculator.js b/lib/calculator.js index d814da5..fd6c88e 100644 --- a/lib/calculator.js +++ b/lib/calculator.js @@ -1,3 +1,9 @@ +/** + * + * Reldens - Calculator + * + */ + const ModifierConst = require('./constants'); class Calculator @@ -57,4 +63,4 @@ class Calculator } -module.exports = Calculator; \ No newline at end of file +module.exports = Calculator; diff --git a/lib/condition.js b/lib/condition.js index e94bd02..b1225be 100644 --- a/lib/condition.js +++ b/lib/condition.js @@ -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'){ diff --git a/lib/constants.js b/lib/constants.js index dc1260c..3c38ef9 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -24,6 +24,10 @@ module.exports = { LE: 'le', GE: 'ge' }, + TYPES: { + INT: 'int', + STRING: 'string' + }, MOD_APPLIED: 'ma', MOD_REVERTED: 'mr' }; diff --git a/lib/modifier.js b/lib/modifier.js index 55716fe..662af06 100644 --- a/lib/modifier.js +++ b/lib/modifier.js @@ -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'); @@ -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:', @@ -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); @@ -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); diff --git a/lib/property-manager.js b/lib/property-manager.js index b50c416..469438a 100644 --- a/lib/property-manager.js +++ b/lib/property-manager.js @@ -1,4 +1,10 @@ -const { ErrorManager } = require('@reldens/utils'); +/** + * + * Reldens - PropertyManager + * + */ + +const { ErrorManager, sc } = require('@reldens/utils'); class PropertyManager { @@ -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 @@ -37,4 +43,4 @@ class PropertyManager } -module.exports = PropertyManager; \ No newline at end of file +module.exports = PropertyManager; diff --git a/package.json b/package.json index f269fdd..2d5deb5 100644 --- a/package.json +++ b/package.json @@ -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", @@ -33,6 +33,6 @@ "url": "https://github.com/damian-pastorini/reldens-modifiers/issues" }, "dependencies": { - "@reldens/utils": "^0.1.8" + "@reldens/utils": "^0.2.0" } }