Browse Source

Change Config class name to ConfigObject

Lucas de Souza 6 years ago
parent
commit
1b34b64f39
3 changed files with 35 additions and 37 deletions
  1. 7 7
      js/processor/ivprogProcessor.js
  2. 15 15
      js/processor/lib/math.js
  3. 13 15
      js/util/config.js

+ 7 - 7
js/processor/ivprogProcessor.js

@@ -20,7 +20,7 @@ import Decimal from 'decimal.js';
 export class IVProgProcessor {
 
   static get LOOP_TIMEOUT () {
-    return Config.config.loopTimeout;
+    return Config.loopTimeout;
   }
 
   static set LOOP_TIMEOUT (ms) {
@@ -789,8 +789,8 @@ export class IVProgProcessor {
           return new StoreObject(resultType, left.value.minus(right.value));
         case Operators.MULT.ord: {
           result = left.value.times(right.value);
-          if(result.dp() > Config.config.decimalPlaces) {
-            result = new Decimal(result.toFixed(Config.config.decimalPlaces));
+          if(result.dp() > Config.decimalPlaces) {
+            result = new Decimal(result.toFixed(Config.decimalPlaces));
           }
           return new StoreObject(resultType, result);
         }
@@ -799,15 +799,15 @@ export class IVProgProcessor {
             result = left.value.divToInt(right.value);
           else
             result = left.value.div(right.value);
-          if(result.dp() > Config.config.decimalPlaces) {
-            result = new Decimal(result.toFixed(Config.config.decimalPlaces));
+          if(result.dp() > Config.decimalPlaces) {
+            result = new Decimal(result.toFixed(Config.decimalPlaces));
           }
           return new StoreObject(resultType, result);
         }
         case Operators.MOD.ord: {
           result = left.value.modulo(right.value);
-          if(result.dp() > Config.config.decimalPlaces) {
-            result = new Decimal(result.toFixed(Config.config.decimalPlaces));
+          if(result.dp() > Config.decimalPlaces) {
+            result = new Decimal(result.toFixed(Config.decimalPlaces));
           }
           return new StoreObject(resultType, result);
         }          

+ 15 - 15
js/processor/lib/math.js

@@ -39,9 +39,9 @@ export function createSinFun () {
        result = new Decimal(-1);
      } else {
        result = Decimal.sin(convertToRadians(angle));
-     }     
-     if(result.dp() > Config.config.decimalPlaces) {
-      result = new Decimal(result.toFixed(Config.config.decimalPlaces));
+     }
+     if(result.dp() > Config.decimalPlaces) {
+      result = new Decimal(result.toFixed(Config.decimalPlaces));
     }
      const temp = new StoreObject(Types.REAL, result);
      sto.mode = Modes.RETURN;
@@ -68,8 +68,8 @@ export function createCosFun () {
       result = new Decimal(0)
     }
     result = Decimal.cos(convertToRadians(angle));
-    if(result.dp() > Config.config.decimalPlaces) {
-      result = new Decimal(result.toFixed(Config.config.decimalPlaces));
+    if(result.dp() > Config.decimalPlaces) {
+      result = new Decimal(result.toFixed(Config.decimalPlaces));
     }
     const temp = new StoreObject(Types.REAL, result);
     sto.mode = Modes.RETURN;
@@ -91,8 +91,8 @@ export function createTanFun () {
       return Promise.reject("Tangent of "+x.value.toNumber()+"° is undefined.");
     }
     let result = Decimal.tan(convertToRadians(angle));
-    if(result.dp() > Config.config.decimalPlaces) {
-      result = new Decimal(result.toFixed(Config.config.decimalPlaces));
+    if(result.dp() > Config.decimalPlaces) {
+      result = new Decimal(result.toFixed(Config.decimalPlaces));
     }
     const temp = new StoreObject(Types.REAL, result);
     sto.mode = Modes.RETURN;
@@ -110,8 +110,8 @@ export function createSqrtFun () {
   const sqrtFun = (sto, _) => {
     const x = sto.applyStore('x');
     let result = x.value.sqrt();
-    if(result.dp() > Config.config.decimalPlaces) {
-      result = new Decimal(result.toFixed(Config.config.decimalPlaces));
+    if(result.dp() > Config.decimalPlaces) {
+      result = new Decimal(result.toFixed(Config.decimalPlaces));
     }
     const temp = new StoreObject(Types.REAL, result);
     sto.mode = Modes.RETURN;
@@ -130,8 +130,8 @@ export function createPowFun () {
     const x = sto.applyStore('x');
     const y = sto.applyStore('y');
     let result = x.value.pow(y.value);
-    if(result.dp() > Config.config.decimalPlaces) {
-      result = new Decimal(result.toFixed(Config.config.decimalPlaces));
+    if(result.dp() > Config.decimalPlaces) {
+      result = new Decimal(result.toFixed(Config.decimalPlaces));
     }
     const temp = new StoreObject(Types.REAL, result);
     sto.mode = Modes.RETURN;
@@ -153,8 +153,8 @@ export function createLogFun () {
       return Promise.reject("the value passed to log function cannot be negative");
     }
     let result = Decimal.log10(x.value);
-    if(result.dp() > Config.config.decimalPlaces) {
-      result = new Decimal(result.toFixed(Config.config.decimalPlaces));
+    if(result.dp() > Config.decimalPlaces) {
+      result = new Decimal(result.toFixed(Config.decimalPlaces));
     }
     const temp = new StoreObject(Types.REAL, result);
     sto.mode = Modes.RETURN;
@@ -204,8 +204,8 @@ export function createInvertFun () {
   const invertFun = (sto, _) => {
     const x = sto.applyStore('x');
     let result = toReal(1).dividedBy(x.value);
-    if(result.dp() > Config.config.decimalPlaces) {
-      result = new Decimal(result.toFixed(Config.config.decimalPlaces));
+    if(result.dp() > Config.decimalPlaces) {
+      result = new Decimal(result.toFixed(Config.decimalPlaces));
     }
     const temp = new StoreObject(Types.REAL, result);
     sto.mode = Modes.RETURN;

+ 13 - 15
js/util/config.js

@@ -1,20 +1,18 @@
-let config = null;
+class ConfigObject {
 
-export class Config {
-
-  static get config () {
-    return config;
-  }
-
-  static setConfig(opts) {
-    config = Object.assign(config, opts);
+  constructor () {
+    this.loopTimeout = 5000;
+    this.decimalPlaces = 5;
+    this.intConvertRoundMode = 2;
   }
 
-  constructor (loopTimeout, decimalPlaces, intConvertRoundMode) {
-    this.loopTimeout = loopTimeout;
-    this.decimalPlaces = decimalPlaces;
-    this.intConvertRoundMode = intConvertRoundMode;
+  setConfig (opts) {
+    for (const key in opts) {
+      if(this.hasOwnProperty(key)){
+        this[key] = opts[key];
+      }
+    }
   }
 }
-
-config = new Config(5000, 5, 2);
+let config = new ConfigObject();
+export const Config = config;