fable 3.0.110 → 3.0.112

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.110",
3
+ "version": "3.0.112",
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'));
@@ -25,8 +25,21 @@ class FableServiceAnticipate extends libFableServiceBase
25
25
 
26
26
  checkQueue()
27
27
  {
28
+ // This could be combined with the last else if stanza but the logic for errors and non-errors would be blended and more complex to follow so keeping it unrolled.
29
+ if (this.lastError)
30
+ {
31
+ // If there are no operations left, and we have waiting functions, call them.
32
+ for (let i = 0; i < this.waitingFunctions.length; i++)
33
+ {
34
+ //this.log.trace('Calling waiting function.')
35
+ this.waitingFunctions[i](this.lastError);
36
+ }
37
+ // Reset our state
38
+ this.lastError = undefined;
39
+ this.waitingFunctions = [];
40
+ }
28
41
  // This checks to see if we need to start any operations.
29
- if (this.operationQueue.length > 0 && this.executingOperationCount < this.maxOperations)
42
+ else if (this.operationQueue.length > 0 && this.executingOperationCount < this.maxOperations)
30
43
  {
31
44
  let tmpOperation = this.operationQueue.shift();
32
45
  this.executingOperationCount += 1;
@@ -72,7 +85,7 @@ class FableServiceAnticipate extends libFableServiceBase
72
85
  throw new Error("Anticipation async callback called twice...");
73
86
  }
74
87
  tmpCallbackState.Called = true;
75
- tmpCallbackState.error = pError;
88
+ this.lastError = pError;
76
89
 
77
90
  tmpCallbackState.OperationSet.executingOperationCount -= 1;
78
91
  tmpCallbackState.OperationSet.completedOperationCount += 1;
@@ -0,0 +1,75 @@
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
+ addPrecise(pLeftValue, pRightValue)
19
+ {
20
+ let tmpLeftValue = isNaN(pLeftValue) ? 0 : pLeftValue;
21
+ let tmpRightValue = isNaN(pRightValue) ? 0 : pRightValue;
22
+
23
+ let tmpLeftArbitraryValue = new this.fable.Utility.bigNumber(tmpLeftValue);
24
+ let tmpResult = tmpLeftArbitraryValue.plus(tmpRightValue);
25
+ return tmpResult.toString();
26
+ }
27
+
28
+ subtractPrecise(pLeftValue, pRightValue)
29
+ {
30
+ let tmpLeftValue = isNaN(pLeftValue) ? 0 : pLeftValue;
31
+ let tmpRightValue = isNaN(pRightValue) ? 0 : pRightValue;
32
+
33
+ let tmpLeftArbitraryValue = new this.fable.Utility.bigNumber(tmpLeftValue);
34
+ let tmpResult = tmpLeftArbitraryValue.minus(tmpRightValue);
35
+ return tmpResult.toString();
36
+ }
37
+
38
+ multiplyPrecise(pLeftValue, pRightValue)
39
+ {
40
+ let tmpLeftValue = isNaN(pLeftValue) ? 0 : pLeftValue;
41
+ let tmpRightValue = isNaN(pRightValue) ? 0 : pRightValue;
42
+
43
+ let tmpLeftArbitraryValue = new this.fable.Utility.bigNumber(tmpLeftValue);
44
+ let tmpResult = tmpLeftArbitraryValue.times(tmpRightValue);
45
+ return tmpResult.toString();
46
+ }
47
+
48
+ dividePrecise(pLeftValue, pRightValue)
49
+ {
50
+ let tmpLeftValue = isNaN(pLeftValue) ? 0 : pLeftValue;
51
+ let tmpRightValue = isNaN(pRightValue) ? 0 : pRightValue;
52
+
53
+ let tmpLeftArbitraryValue = new this.fable.Utility.bigNumber(tmpLeftValue);
54
+ let tmpResult = tmpLeftArbitraryValue.div(tmpRightValue);
55
+ return tmpResult.toString();
56
+ }
57
+
58
+ percentagePrecise(pIs, pOf)
59
+ {
60
+ let tmpLeftValue = isNaN(pIs) ? 0 : pIs;
61
+ let tmpRightValue = isNaN(pOf) ? 0 : pOf;
62
+
63
+ if (tmpRightValue == 0)
64
+ {
65
+ return '0';
66
+ }
67
+
68
+ let tmpLeftArbitraryValue = new this.fable.Utility.bigNumber(tmpLeftValue);
69
+ let tmpResult = tmpLeftArbitraryValue.div(tmpRightValue);
70
+ tmpResult = tmpResult.times(100);
71
+ return tmpResult.toString();
72
+ }
73
+ }
74
+
75
+ module.exports = FableServiceMath;
@@ -85,6 +85,60 @@ suite
85
85
  });
86
86
  }
87
87
  );
88
+ test
89
+ (
90
+ 'Error bailout',
91
+ function (fTestComplete)
92
+ {
93
+ let testFable = new libFable();
94
+ let tmpAnticipate = testFable.newAnticipate();
95
+ let tmpPostErrorMethodCalled = false;
96
+ tmpAnticipate.anticipate(function (fCallback)
97
+ {
98
+ testFable.log.info('Operation First test timeout entered...');
99
+ setTimeout(function ()
100
+ {
101
+ testFable.log.info(`Operation First test timeout done!`);
102
+ fCallback();
103
+ }, 500);
104
+ });
105
+ tmpAnticipate.anticipate(function (fCallback)
106
+ {
107
+ testFable.log.info('Operation Second test timeout entered...');
108
+ setTimeout(function ()
109
+ {
110
+ testFable.log.info(`Operation Second test timeout done!`);
111
+ fCallback();
112
+ }, 50);
113
+ });
114
+ tmpAnticipate.anticipate(function (fCallback)
115
+ {
116
+ testFable.log.info('Operation Second test timeout entered...');
117
+ setTimeout(function ()
118
+ {
119
+ testFable.log.info(`Operation Second test timeout done!`);
120
+ fCallback(new Error('Bail out or else!'));
121
+ }, 50);
122
+ });
123
+ tmpAnticipate.anticipate(function (fCallback)
124
+ {
125
+ testFable.log.info('Operation Third test timeout entered...');
126
+ setTimeout(function ()
127
+ {
128
+ tmpPostErrorMethodCalled = true;
129
+ testFable.log.info(`Operation Third test timeout done!`);
130
+ fCallback();
131
+ }, 50);
132
+ });
133
+ tmpAnticipate.wait(function (pError)
134
+ {
135
+ Expect(pError).to.be.an('error');
136
+ Expect(tmpPostErrorMethodCalled).to.equal(false);
137
+ testFable.log.info(`Wait 1 completed: ${pError}`)
138
+ return fTestComplete();
139
+ });
140
+ }
141
+ );
88
142
  test
89
143
  (
90
144
  'Huge call stack',
@@ -0,0 +1,48 @@
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
+ }
46
+ );
47
+ }
48
+ );