fable 3.0.111 → 3.0.113

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fable",
3
- "version": "3.0.111",
3
+ "version": "3.0.113",
4
4
  "description": "An entity behavior management and API bundling library.",
5
5
  "main": "source/Fable.js",
6
6
  "scripts": {
package/source/Fable.js CHANGED
@@ -71,6 +71,7 @@ class Fable extends libFableServiceBase.CoreServiceProviderBase
71
71
  this.addAndInstantiateServiceType('DataFormat', require('./services/Fable-Service-DataFormat.js'));
72
72
  this.addAndInstantiateServiceType('DataGeneration', require('./services/Fable-Service-DataGeneration.js'));
73
73
  this.addAndInstantiateServiceType('Utility', require('./services/Fable-Service-Utility.js'));
74
+ this.addAndInstantiateServiceType('Math', require('./services/Fable-Service-Math.js'));
74
75
  this.addServiceType('Operation', require('./services/Fable-Service-Operation.js'));
75
76
  this.addServiceType('RestClient', require('./services/Fable-Service-RestClient.js'));
76
77
  this.addServiceType('CSVParser', require('./services/Fable-Service-CSVParser.js'));
@@ -8,7 +8,7 @@ class DataFormat extends libFableServiceProviderBase
8
8
  {
9
9
  constructor(pFable, pOptions, pServiceHash)
10
10
  {
11
- super(pFable, pOptions, pServiceHash)
11
+ super(pFable, pOptions, pServiceHash);
12
12
 
13
13
  this.serviceType = 'DataArithmatic';
14
14
 
@@ -60,7 +60,7 @@ class DataFormat extends libFableServiceProviderBase
60
60
  * @param {*} pString
61
61
  * @param {*} pSearchString
62
62
  * @param {*} pStartIndex
63
- * @returns {*}
63
+ * @returns {boolean}
64
64
  */
65
65
  stringStartsWith (pString, pSearchString, pStartIndex)
66
66
  {
@@ -0,0 +1,91 @@
1
+ const libFableServiceBase = require('fable-serviceproviderbase');
2
+
3
+ /**
4
+ * Arbitrary Precision Math Operations
5
+ * @author Steven Velozo <steven@velozo.com>
6
+ * @description Simple functions that perform arbitrary precision math operations and return string resultant values.
7
+ */
8
+
9
+ class FableServiceMath extends libFableServiceBase
10
+ {
11
+ constructor(pFable, pOptions, pServiceHash)
12
+ {
13
+ super(pFable, pOptions, pServiceHash);
14
+
15
+ this.serviceType = 'Math';
16
+ }
17
+
18
+ parsePrecise(pValue)
19
+ {
20
+ let tmpNumber = "0.0";
21
+
22
+ try
23
+ {
24
+ tmpNumber = new this.fable.Utility.bigNumber(pValue);
25
+ }
26
+ catch(pError)
27
+ {
28
+ console.log(`Error parsing number (type ${typeof(pValue)}): ${pError}`);
29
+ }
30
+
31
+ return tmpNumber.toString();
32
+ }
33
+
34
+ addPrecise(pLeftValue, pRightValue)
35
+ {
36
+ let tmpLeftValue = isNaN(pLeftValue) ? 0 : pLeftValue;
37
+ let tmpRightValue = isNaN(pRightValue) ? 0 : pRightValue;
38
+
39
+ let tmpLeftArbitraryValue = new this.fable.Utility.bigNumber(tmpLeftValue);
40
+ let tmpResult = tmpLeftArbitraryValue.plus(tmpRightValue);
41
+ return tmpResult.toString();
42
+ }
43
+
44
+ subtractPrecise(pLeftValue, pRightValue)
45
+ {
46
+ let tmpLeftValue = isNaN(pLeftValue) ? 0 : pLeftValue;
47
+ let tmpRightValue = isNaN(pRightValue) ? 0 : pRightValue;
48
+
49
+ let tmpLeftArbitraryValue = new this.fable.Utility.bigNumber(tmpLeftValue);
50
+ let tmpResult = tmpLeftArbitraryValue.minus(tmpRightValue);
51
+ return tmpResult.toString();
52
+ }
53
+
54
+ multiplyPrecise(pLeftValue, pRightValue)
55
+ {
56
+ let tmpLeftValue = isNaN(pLeftValue) ? 0 : pLeftValue;
57
+ let tmpRightValue = isNaN(pRightValue) ? 0 : pRightValue;
58
+
59
+ let tmpLeftArbitraryValue = new this.fable.Utility.bigNumber(tmpLeftValue);
60
+ let tmpResult = tmpLeftArbitraryValue.times(tmpRightValue);
61
+ return tmpResult.toString();
62
+ }
63
+
64
+ dividePrecise(pLeftValue, pRightValue)
65
+ {
66
+ let tmpLeftValue = isNaN(pLeftValue) ? 0 : pLeftValue;
67
+ let tmpRightValue = isNaN(pRightValue) ? 0 : pRightValue;
68
+
69
+ let tmpLeftArbitraryValue = new this.fable.Utility.bigNumber(tmpLeftValue);
70
+ let tmpResult = tmpLeftArbitraryValue.div(tmpRightValue);
71
+ return tmpResult.toString();
72
+ }
73
+
74
+ percentagePrecise(pIs, pOf)
75
+ {
76
+ let tmpLeftValue = isNaN(pIs) ? 0 : pIs;
77
+ let tmpRightValue = isNaN(pOf) ? 0 : pOf;
78
+
79
+ if (tmpRightValue == 0)
80
+ {
81
+ return '0';
82
+ }
83
+
84
+ let tmpLeftArbitraryValue = new this.fable.Utility.bigNumber(tmpLeftValue);
85
+ let tmpResult = tmpLeftArbitraryValue.div(tmpRightValue);
86
+ tmpResult = tmpResult.times(100);
87
+ return tmpResult.toString();
88
+ }
89
+ }
90
+
91
+ module.exports = FableServiceMath;
@@ -11,6 +11,7 @@ class FableOperation extends libFableServiceBase
11
11
 
12
12
  // Timestamps will just be the long ints
13
13
  this.timeStamps = {};
14
+
14
15
  // ProgressTrackers have an object format of: {Hash:'SomeHash',EndTime:UINT,CurrentTime:UINT,TotalCount:INT,CurrentCount:INT}
15
16
  this.progressTrackers = {};
16
17
 
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Unit tests for Fable Math
3
+ *
4
+ * @license MIT
5
+ *
6
+ * @author Steven Velozo <steven@velozo.com>
7
+ */
8
+
9
+ var libFable = require('../source/Fable.js');
10
+
11
+ var Chai = require("chai");
12
+ var Expect = Chai.expect;
13
+
14
+ suite
15
+ (
16
+ 'Math',
17
+ function()
18
+ {
19
+ suite
20
+ (
21
+ 'Math Operations',
22
+ function()
23
+ {
24
+ test
25
+ (
26
+ 'Exercise Math Tests',
27
+ function(fDone)
28
+ {
29
+ let testFable = new libFable();
30
+
31
+ Expect(testFable.Math.addPrecise(1, 1)).to.equal('2');
32
+ // 3.3333333333333333333333333333333 in the current node.js implementation collapses to 3.3333333333333335
33
+ Expect(testFable.Math.addPrecise(1, '3.3333333333333333333333333333333')).to.equal('4.3333333333333333333333333333333');
34
+ // Similarly mantissa problems occur with decimals
35
+ // So for plain javascript: 0.1 + 0.2 = 0.30000000000000004
36
+ Expect(testFable.Math.addPrecise(0.1, 0.2)).to.equal('0.3');
37
+ Expect(testFable.Math.subtractPrecise(1, 1)).to.equal('0');
38
+ Expect(testFable.Math.multiplyPrecise(1, 1)).to.equal('1');
39
+ Expect(testFable.Math.dividePrecise(1, 1)).to.equal('1');
40
+ Expect(testFable.Math.percentagePrecise(1, 1)).to.equal('100');
41
+
42
+ return fDone();
43
+ }
44
+ );
45
+ test
46
+ (
47
+ 'Parse Numbers',
48
+ function(fDone)
49
+ {
50
+ let testFable = new libFable();
51
+
52
+ Expect(testFable.Math.parsePrecise(1)).to.equal('1');
53
+ // 3.3333333333333333333333333333333 in the current node.js implementation collapses to 3.3333333333333335
54
+ Expect(testFable.Math.parsePrecise('4.3333333333333333333333333333333')).to.equal('4.3333333333333333333333333333333');
55
+ Expect(testFable.Math.parsePrecise(undefined)).to.equal('0.0');
56
+
57
+ return fDone();
58
+ }
59
+ );
60
+ }
61
+ );
62
+ }
63
+ );