fable 3.1.2 → 3.1.3

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.1.2",
3
+ "version": "3.1.3",
4
4
  "description": "A service dependency injection, configuration and logging library.",
5
5
  "main": "source/Fable.js",
6
6
  "scripts": {
@@ -41,6 +41,90 @@ class DateManipulation extends libFableServiceProviderBase
41
41
  // const localeDE = require('dayjs/locale/de');
42
42
  // _Fable.Dates.dayJS.locale('de');
43
43
  }
44
+
45
+ /**
46
+ * Calculates the difference in days between two dates.
47
+ *
48
+ * @param {string|Date|number} pDateStart - The start date. Can be a string, Date object, or timestamp.
49
+ * @param {string|Date|number} pDateEnd - The end date. Can be a string, Date object, or timestamp. Defaults to the current date if not provided.
50
+ * @returns {number} The difference in days between the start and end dates. Returns NaN if the start date is invalid.
51
+ */
52
+ dateDayDifference(pDateStart, pDateEnd)
53
+ {
54
+ // If there is not a valid start date, return NaN
55
+ if ((pDateStart === undefined) || (pDateStart === null) || (pDateStart === ''))
56
+ {
57
+ return NaN;
58
+ }
59
+ let tmpStartDate = this.dayJS(pDateStart);
60
+ // Without a valid end date, dayJS defaults to the current date
61
+ let tmpEndDate = this.dayJS(pDateEnd);
62
+ // Returns the difference in days between two dates
63
+ return tmpEndDate.diff(tmpStartDate, 'day');
64
+ }
65
+
66
+ /**
67
+ * Calculates the difference in weeks between two dates.
68
+ *
69
+ * @param {string|Date|number} pDateStart - The start date. Can be a string, Date object, or timestamp.
70
+ * @param {string|Date|number} pDateEnd - The end date. Can be a string, Date object, or timestamp. Defaults to the current date if not provided.
71
+ * @returns {number} The difference in weeks between the two dates. Returns NaN if the start date is invalid.
72
+ */
73
+ dateWeekDifference(pDateStart, pDateEnd)
74
+ {
75
+ // If there is not a valid start date, return NaN
76
+ if ((pDateStart === undefined) || (pDateStart === null) || (pDateStart === ''))
77
+ {
78
+ return NaN;
79
+ }
80
+ let tmpStartDate = this.dayJS(pDateStart);
81
+ // Without a valid end date, dayJS defaults to the current date
82
+ let tmpEndDate = this.dayJS(pDateEnd);
83
+ // Returns the difference in weeks between two dates
84
+ return tmpEndDate.diff(tmpStartDate, 'week');
85
+ }
86
+
87
+ /**
88
+ * Calculates the difference in months between two dates.
89
+ *
90
+ * @param {string|Date|number} pDateStart - The start date. Can be a string, Date object, or timestamp.
91
+ * @param {string|Date|number} pDateEnd - The end date. Can be a string, Date object, or timestamp. Defaults to the current date if not provided.
92
+ * @returns {number} The difference in months between the two dates. Returns NaN if the start date is invalid.
93
+ */
94
+ dateMonthDifference(pDateStart, pDateEnd)
95
+ {
96
+ // If there is not a valid start date, return NaN
97
+ if ((pDateStart === undefined) || (pDateStart === null) || (pDateStart === ''))
98
+ {
99
+ return NaN;
100
+ }
101
+ let tmpStartDate = this.dayJS(pDateStart);
102
+ // Without a valid end date, dayJS defaults to the current date
103
+ let tmpEndDate = this.dayJS(pDateEnd);
104
+ // Returns the difference in months between two dates
105
+ return tmpEndDate.diff(tmpStartDate, 'month');
106
+ }
107
+
108
+ /**
109
+ * Calculates the difference in years between two dates.
110
+ *
111
+ * @param {string|Date|number} pDateStart - The start date. Can be a string, Date object, or timestamp.
112
+ * @param {string|Date|number} pDateEnd - The end date. Can be a string, Date object, or timestamp. Defaults to the current date if not provided.
113
+ * @returns {number} The difference in years between the two dates. Returns NaN if the start date is invalid.
114
+ */
115
+ dateYearDifference(pDateStart, pDateEnd)
116
+ {
117
+ // If there is not a valid start date, return NaN
118
+ if ((pDateStart === undefined) || (pDateStart === null) || (pDateStart === ''))
119
+ {
120
+ return NaN;
121
+ }
122
+ let tmpStartDate = this.dayJS(pDateStart);
123
+ // Without a valid end date, dayJS defaults to the current date
124
+ let tmpEndDate = this.dayJS(pDateEnd);
125
+ // Returns the difference in years between two dates
126
+ return tmpEndDate.diff(tmpStartDate, 'year');
127
+ }
44
128
  }
45
129
 
46
130
  module.exports = DateManipulation;
@@ -157,5 +157,22 @@
157
157
  "randomfloatupto": {
158
158
  "Name": "Random Float",
159
159
  "Address": "fable.DataGeneration.randomFloatUpTo"
160
+ },
161
+
162
+ "datedaydifference": {
163
+ "Name": "Date Difference in Days",
164
+ "Address": "fable.Dates.dateDayDifference"
165
+ },
166
+ "dateweekdifference": {
167
+ "Name": "Date Difference in Weeks",
168
+ "Address": "fable.Dates.dateWeekDifference"
169
+ },
170
+ "datemonthdifference": {
171
+ "Name": "Date Difference in Months",
172
+ "Address": "fable.Dates.dateMonthDifference"
173
+ },
174
+ "dateyeardifference": {
175
+ "Name": "Date Difference in Years",
176
+ "Address": "fable.Dates.dateYearDifference"
160
177
  }
161
178
  }
@@ -729,6 +729,14 @@ class FableServiceMath extends libFableServiceBase
729
729
  return tmpHistogram;
730
730
  }
731
731
 
732
+ /**
733
+ * Aggregates a histogram by exact value from an internal state object.
734
+ *
735
+ * @param {string} pValueObjectSetAddress - The address of the internal value object set.
736
+ * @param {string} pValueAddress - The address of the value to aggregate by.
737
+ * @param {string} pValueAmountAddress - The address of the amount to aggregate.
738
+ * @returns {Object} The aggregated histogram object. Returns an empty object if the value object set address is not provided.
739
+ */
732
740
  histogramAggregationByExactValueFromInternalState(pValueObjectSetAddress, pValueAddress, pValueAmountAddress)
733
741
  {
734
742
  if (!pValueObjectSetAddress)
@@ -769,11 +777,25 @@ class FableServiceMath extends libFableServiceBase
769
777
  return tmpValueArray[tmpIndex];
770
778
  }
771
779
 
780
+ /**
781
+ * Finds the smallest value in a set of objects based on a specified value address.
782
+ *
783
+ * @param {Object[]} pValueObjectSet - An array of objects to search through.
784
+ * @param {string} pValueAddress - The key or path used to access the value within each object.
785
+ * @returns {*} The smallest value found in the set at the specified value address.
786
+ */
772
787
  smallestInSet(pValueObjectSet, pValueAddress)
773
788
  {
774
789
  return this.entryInSet(pValueObjectSet, pValueAddress, 0);
775
790
  }
776
791
 
792
+ /**
793
+ * Finds the largest value in a set of objects based on a specified value address.
794
+ *
795
+ * @param {Object[]} pValueObjectSet - An array of objects to search through.
796
+ * @param {string} pValueAddress - The address (key or path) within each object to compare values.
797
+ * @returns {*} The largest value found at the specified address in the set of objects.
798
+ */
777
799
  largestInSet(pValueObjectSet, pValueAddress)
778
800
  {
779
801
  return this.entryInSet(pValueObjectSet, pValueAddress, -1);
@@ -20,9 +20,65 @@ suite
20
20
 
21
21
  suite
22
22
  (
23
- 'Format Dates and Times',
23
+ 'Format and Calculate Dates and Times',
24
24
  ()=>
25
25
  {
26
+ test
27
+ (
28
+ 'Calculate the difference in days between two dates',
29
+ (fTestComplete)=>
30
+ {
31
+ let testFable = new libFable({LogStreams: false});
32
+ Expect(testFable.Dates.dateDayDifference('2021-03-12', '2023-09-01'))
33
+ .to.equal(903);
34
+ Expect(testFable.Dates.dateDayDifference('2023-08-01', '2023-09-01'))
35
+ .to.equal(31);
36
+ Expect(testFable.Dates.dateDayDifference('2023-09-01', '2023-10-01'))
37
+ .to.equal(30);
38
+ Expect(testFable.Dates.dateDayDifference('2023-9-1', '2023-10-01'))
39
+ .to.equal(30);
40
+ Expect(testFable.Dates.dateWeekDifference('2023-9-1', '2023-10-01'))
41
+ .to.equal(4);
42
+ Expect(testFable.Dates.dateMonthDifference('1986-10-01', '2023-09-01'))
43
+ .to.equal(443);
44
+ Expect(testFable.Dates.dateYearDifference('1986-10-01', '2023-09-01'))
45
+ .to.equal(36);
46
+ Expect(testFable.Dates.dateYearDifference('1986-08-01', '2023-09-01'))
47
+ .to.equal(37);
48
+ Expect(testFable.Dates.dateYearDifference('1986-08-31', '2023-09-01'))
49
+ .to.equal(37);
50
+ let tmpValue = testFable.Dates.dateDayDifference('BadDatesIncoming', '2023-09-01');
51
+ Expect(tmpValue)
52
+ .to.be.NaN;
53
+
54
+ tmpValue = testFable.Dates.dateDayDifference();
55
+ Expect(tmpValue)
56
+ .to.be.NaN;
57
+ return fTestComplete();
58
+ }
59
+ );
60
+ test
61
+ (
62
+ 'Format a time span in milliseconds to a human readable string',
63
+ (fTestComplete)=>
64
+ {
65
+ let testFable = new libFable({LogStreams: false});
66
+ let _DataFormat = testFable.services.DataFormat;
67
+ Expect(_DataFormat
68
+ .formatTimeSpan(1000))
69
+ .to.equal('00:00:01.000');
70
+ Expect(_DataFormat
71
+ .formatTimeSpan(100243231))
72
+ .to.equal('27:50:43.231');
73
+ Expect(_DataFormat
74
+ .formatTimeSpan(100299211))
75
+ .to.equal('27:51:39.211');
76
+ Expect(_DataFormat
77
+ .formatTimeSpan())
78
+ .to.equal('');
79
+ return fTestComplete();
80
+ }
81
+ );
26
82
  test
27
83
  (
28
84
  'Format a time span in milliseconds to a human readable string',
@@ -256,6 +256,11 @@ suite
256
256
  Expect(tmpResult).to.equal("1545");
257
257
  tmpResult = _Parser.solve('Area = ROUND(X * Y * Z, 3, 3)', tmpDataObject, tmpSolveResults);
258
258
  Expect(tmpResult).to.equal("1545.2");
259
+ // Test the getvaluarray function]
260
+ // TODO: Fix the return values for these expression return functions
261
+ //tmpResult = _Parser.solve('NewSet = GETVALUEARRAY(X, Y, Z)', tmpDataObject, tmpSolveResults);
262
+ //tmpResult = _Parser.solve('NewSetAverage = SUM(NewSet)', tmpDataObject, tmpSolveResults);
263
+ //Expect(tmpResult).to.equal("84.115923423");
259
264
  return fDone();
260
265
  }
261
266
  );