@barchart/portfolio-api-common 1.0.27 → 1.0.28
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.
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
const array = require('@barchart/common-js/lang/array')
|
|
2
|
+
assert = require('@barchart/common-js/lang/assert'),
|
|
3
|
+
Decimal = require('@barchart/common-js/lang/Decimal'),
|
|
4
|
+
formatter = require('@barchart/common-js/lang/formatter');
|
|
5
|
+
|
|
6
|
+
module.exports = (() => {
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Formats Position Summary records into groups based on instrument type
|
|
10
|
+
*
|
|
11
|
+
*/
|
|
12
|
+
class PositionSummaryFormatter{
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The formatter
|
|
16
|
+
*
|
|
17
|
+
* @param {Array} summaries
|
|
18
|
+
* @returns {Object}
|
|
19
|
+
*/
|
|
20
|
+
static format(summaries) {
|
|
21
|
+
assert.argumentIsRequired(summaries, 'summaries', Object);
|
|
22
|
+
|
|
23
|
+
const summaryGroups = array.groupBy(summaries, summary => summary.instrument.type.code);
|
|
24
|
+
|
|
25
|
+
const total = {
|
|
26
|
+
start: Decimal.ZERO,
|
|
27
|
+
change: Decimal.ZERO,
|
|
28
|
+
end: Decimal.ZERO
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const formattedSummaryGroups = Object.keys(summaryGroups).map((group) => {
|
|
32
|
+
const positions = summaryGroups[group];
|
|
33
|
+
|
|
34
|
+
let start = Decimal.ZERO;
|
|
35
|
+
let end = Decimal.ZERO;
|
|
36
|
+
|
|
37
|
+
positions.map((position) => {
|
|
38
|
+
start = start.add(position.start.value);
|
|
39
|
+
end = end.add(position.end.value);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const change = end.subtract(start);
|
|
43
|
+
|
|
44
|
+
total.start = total.start.add(start);
|
|
45
|
+
total.change = total.change.add(change);
|
|
46
|
+
total.end = total.end.add(end);
|
|
47
|
+
|
|
48
|
+
return summaryGroups[group] = {
|
|
49
|
+
name: group,
|
|
50
|
+
start: formatNumber(start.toFloat()),
|
|
51
|
+
end: formatNumber(end.toFloat()),
|
|
52
|
+
change: formatNumber(change.toFloat())
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
Object.keys(total).map(key => total[key] = formatNumber(total[key].toFloat()));
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
groups: formattedSummaryGroups,
|
|
60
|
+
total
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const formatNumber = (float) => {
|
|
66
|
+
return formatter.numberToString(float, 2, ',');
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
return PositionSummaryFormatter;
|
|
70
|
+
|
|
71
|
+
})();
|
package/package.json
CHANGED
package/test/SpecRunner.js
CHANGED
|
@@ -631,37 +631,6 @@ module.exports = function () {
|
|
|
631
631
|
return Day.compareDays(this, other) > 0;
|
|
632
632
|
}
|
|
633
633
|
|
|
634
|
-
/**
|
|
635
|
-
* Indicates the current day falls between two other days, inclusive
|
|
636
|
-
* of the range boundaries.
|
|
637
|
-
*
|
|
638
|
-
* @public
|
|
639
|
-
* @param {Day=} first
|
|
640
|
-
* @param {Day=} last
|
|
641
|
-
* @param {boolean=} exclusive
|
|
642
|
-
* @returns {boolean}
|
|
643
|
-
*/
|
|
644
|
-
|
|
645
|
-
}, {
|
|
646
|
-
key: 'getIsContained',
|
|
647
|
-
value: function getIsContained(first, last) {
|
|
648
|
-
assert.argumentIsOptional(first, 'first', Day, 'Day');
|
|
649
|
-
assert.argumentIsOptional(last, 'last', Day, 'Day');
|
|
650
|
-
|
|
651
|
-
var notAfter = void 0;
|
|
652
|
-
var notBefore = void 0;
|
|
653
|
-
|
|
654
|
-
if (first && last && first.getIsAfter(last)) {
|
|
655
|
-
notBefore = false;
|
|
656
|
-
notAfter = false;
|
|
657
|
-
} else {
|
|
658
|
-
notAfter = !(last instanceof Day) || !this.getIsAfter(last);
|
|
659
|
-
notBefore = !(first instanceof Day) || !this.getIsBefore(first);
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
return notAfter && notBefore;
|
|
663
|
-
}
|
|
664
|
-
|
|
665
634
|
/**
|
|
666
635
|
* Indicates if another {@link Day} occurs after the current instance.
|
|
667
636
|
*
|