@barchart/portfolio-api-common 1.0.48 → 1.0.49
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.
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
const array = require('@barchart/common-js/lang/array'),
|
|
2
|
+
ComparatorBuilder = require('@barchart/common-js/collections/sorting/ComparatorBuilder'),
|
|
3
|
+
comparators = require('@barchart/common-js/collections/sorting/comparators'),
|
|
2
4
|
assert = require('@barchart/common-js/lang/assert'),
|
|
3
5
|
is = require('@barchart/common-js/lang/is'),
|
|
4
6
|
Tree = require('@barchart/common-js/collections/Tree');
|
|
@@ -88,6 +90,32 @@ module.exports = (() => {
|
|
|
88
90
|
|
|
89
91
|
const compositeGroups = populatedGroups.concat(empty);
|
|
90
92
|
|
|
93
|
+
let builder;
|
|
94
|
+
|
|
95
|
+
if (currentDefinition.requiredGroups.length !== 0) {
|
|
96
|
+
const ordering = currentDefinition.requiredGroups.reduce((map, group, index) => {
|
|
97
|
+
map[group] = index;
|
|
98
|
+
|
|
99
|
+
return map;
|
|
100
|
+
}, { });
|
|
101
|
+
|
|
102
|
+
const getIndex = (description) => {
|
|
103
|
+
return ordering[description] || Math.MAX_VALUE;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
builder = ComparatorBuilder.startWith((a, b) => {
|
|
107
|
+
return comparators.compareNumbers(getIndex(a.description), getIndex(b.description));
|
|
108
|
+
}).thenBy((a, b) => {
|
|
109
|
+
return comparators.compareStrings(a.description, b.description);
|
|
110
|
+
});
|
|
111
|
+
} else {
|
|
112
|
+
builder = ComparatorBuilder.startWith((a, b) => {
|
|
113
|
+
return comparators.compareStrings(a.description, b.description);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
compositeGroups.sort(builder.toComparator());
|
|
118
|
+
|
|
91
119
|
compositeGroups.forEach((group) => {
|
|
92
120
|
const child = tree.addChild(group);
|
|
93
121
|
|
|
@@ -8,11 +8,12 @@ module.exports = (() => {
|
|
|
8
8
|
* @public
|
|
9
9
|
*/
|
|
10
10
|
class PositionGroupDefinition {
|
|
11
|
-
constructor(name, keySelector, descriptionSelector, requiredGroups, single) {
|
|
11
|
+
constructor(name, keySelector, descriptionSelector, currencySelector, requiredGroups, single) {
|
|
12
12
|
this._name = name;
|
|
13
13
|
|
|
14
14
|
this._keySelector = keySelector;
|
|
15
15
|
this._descriptionSelector = descriptionSelector;
|
|
16
|
+
this._currencySelector = currencySelector;
|
|
16
17
|
|
|
17
18
|
this._requiredGroups = requiredGroups || [ ];
|
|
18
19
|
this._single = is.boolean(single) && single;
|
|
@@ -30,6 +31,10 @@ module.exports = (() => {
|
|
|
30
31
|
return this._descriptionSelector;
|
|
31
32
|
}
|
|
32
33
|
|
|
34
|
+
get currencySelector() {
|
|
35
|
+
return this._currencySelector;
|
|
36
|
+
}
|
|
37
|
+
|
|
33
38
|
get requiredGroups() {
|
|
34
39
|
return this._requiredGroups;
|
|
35
40
|
}
|
package/package.json
CHANGED
package/test/SpecRunner.js
CHANGED
|
@@ -514,6 +514,8 @@ module.exports = (() => {
|
|
|
514
514
|
|
|
515
515
|
},{"@barchart/common-js/lang/Enum":14,"@barchart/common-js/lang/assert":16}],3:[function(require,module,exports){
|
|
516
516
|
const array = require('@barchart/common-js/lang/array'),
|
|
517
|
+
ComparatorBuilder = require('@barchart/common-js/collections/sorting/ComparatorBuilder'),
|
|
518
|
+
comparators = require('@barchart/common-js/collections/sorting/comparators'),
|
|
517
519
|
assert = require('@barchart/common-js/lang/assert'),
|
|
518
520
|
is = require('@barchart/common-js/lang/is'),
|
|
519
521
|
Tree = require('@barchart/common-js/collections/Tree');
|
|
@@ -603,6 +605,32 @@ module.exports = (() => {
|
|
|
603
605
|
|
|
604
606
|
const compositeGroups = populatedGroups.concat(empty);
|
|
605
607
|
|
|
608
|
+
let builder;
|
|
609
|
+
|
|
610
|
+
if (currentDefinition.requiredGroups.length !== 0) {
|
|
611
|
+
const ordering = currentDefinition.requiredGroups.reduce((map, group, index) => {
|
|
612
|
+
map[group] = index;
|
|
613
|
+
|
|
614
|
+
return map;
|
|
615
|
+
}, { });
|
|
616
|
+
|
|
617
|
+
const getIndex = (description) => {
|
|
618
|
+
return ordering[description] || Math.MAX_VALUE;
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
builder = ComparatorBuilder.startWith((a, b) => {
|
|
622
|
+
return comparators.compareNumbers(getIndex(a.description), getIndex(b.description));
|
|
623
|
+
}).thenBy((a, b) => {
|
|
624
|
+
return comparators.compareStrings(a.description, b.description);
|
|
625
|
+
});
|
|
626
|
+
} else {
|
|
627
|
+
builder = ComparatorBuilder.startWith((a, b) => {
|
|
628
|
+
return comparators.compareStrings(a.description, b.description);
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
compositeGroups.sort(builder.toComparator());
|
|
633
|
+
|
|
606
634
|
compositeGroups.forEach((group) => {
|
|
607
635
|
const child = tree.addChild(group);
|
|
608
636
|
|
|
@@ -651,7 +679,7 @@ module.exports = (() => {
|
|
|
651
679
|
return PositionContainer;
|
|
652
680
|
})();
|
|
653
681
|
|
|
654
|
-
},{"./PositionGroup":4,"./PositionGroupDefinition":5,"./PositionItem":6,"@barchart/common-js/collections/Tree":7,"@barchart/common-js/lang/array":15,"@barchart/common-js/lang/assert":16,"@barchart/common-js/lang/is":18}],4:[function(require,module,exports){
|
|
682
|
+
},{"./PositionGroup":4,"./PositionGroupDefinition":5,"./PositionItem":6,"@barchart/common-js/collections/Tree":7,"@barchart/common-js/collections/sorting/ComparatorBuilder":8,"@barchart/common-js/collections/sorting/comparators":9,"@barchart/common-js/lang/array":15,"@barchart/common-js/lang/assert":16,"@barchart/common-js/lang/is":18}],4:[function(require,module,exports){
|
|
655
683
|
const assert = require('@barchart/common-js/lang/assert'),
|
|
656
684
|
Currency = require('@barchart/common-js/lang/Currency'),
|
|
657
685
|
Decimal = require('@barchart/common-js/lang/Decimal'),
|
|
@@ -777,11 +805,12 @@ module.exports = (() => {
|
|
|
777
805
|
* @public
|
|
778
806
|
*/
|
|
779
807
|
class PositionGroupDefinition {
|
|
780
|
-
constructor(name, keySelector, descriptionSelector, requiredGroups, single) {
|
|
808
|
+
constructor(name, keySelector, descriptionSelector, currencySelector, requiredGroups, single) {
|
|
781
809
|
this._name = name;
|
|
782
810
|
|
|
783
811
|
this._keySelector = keySelector;
|
|
784
812
|
this._descriptionSelector = descriptionSelector;
|
|
813
|
+
this._currencySelector = currencySelector;
|
|
785
814
|
|
|
786
815
|
this._requiredGroups = requiredGroups || [ ];
|
|
787
816
|
this._single = is.boolean(single) && single;
|
|
@@ -799,6 +828,10 @@ module.exports = (() => {
|
|
|
799
828
|
return this._descriptionSelector;
|
|
800
829
|
}
|
|
801
830
|
|
|
831
|
+
get currencySelector() {
|
|
832
|
+
return this._currencySelector;
|
|
833
|
+
}
|
|
834
|
+
|
|
802
835
|
get requiredGroups() {
|
|
803
836
|
return this._requiredGroups;
|
|
804
837
|
}
|
|
@@ -5313,9 +5346,9 @@ describe('When a position container data is gathered', () => {
|
|
|
5313
5346
|
|
|
5314
5347
|
beforeEach(() => {
|
|
5315
5348
|
definitions = [
|
|
5316
|
-
new PositionGroupDefinition('Total', x => true, x => 'Total'),
|
|
5317
|
-
new PositionGroupDefinition('Portfolio', x => x.portfolio.portfolio, x => x.portfolio.name),
|
|
5318
|
-
new PositionGroupDefinition('Position', x => x.position.position, x => x.position.instrument.symbol.barchart)
|
|
5349
|
+
new PositionGroupDefinition('Total', x => true, x => 'Total', x => Currency.CAD),
|
|
5350
|
+
new PositionGroupDefinition('Portfolio', x => x.portfolio.portfolio, x => x.portfolio.name, x => Currency.CAD),
|
|
5351
|
+
new PositionGroupDefinition('Position', x => x.position.position, x => x.position.instrument.symbol.barchart, x => x.position.instrument.currency)
|
|
5319
5352
|
];
|
|
5320
5353
|
|
|
5321
5354
|
try {
|
|
@@ -56,9 +56,9 @@ describe('When a position container data is gathered', () => {
|
|
|
56
56
|
|
|
57
57
|
beforeEach(() => {
|
|
58
58
|
definitions = [
|
|
59
|
-
new PositionGroupDefinition('Total', x => true, x => 'Total'),
|
|
60
|
-
new PositionGroupDefinition('Portfolio', x => x.portfolio.portfolio, x => x.portfolio.name),
|
|
61
|
-
new PositionGroupDefinition('Position', x => x.position.position, x => x.position.instrument.symbol.barchart)
|
|
59
|
+
new PositionGroupDefinition('Total', x => true, x => 'Total', x => Currency.CAD),
|
|
60
|
+
new PositionGroupDefinition('Portfolio', x => x.portfolio.portfolio, x => x.portfolio.name, x => Currency.CAD),
|
|
61
|
+
new PositionGroupDefinition('Position', x => x.position.position, x => x.position.instrument.symbol.barchart, x => x.position.instrument.currency)
|
|
62
62
|
];
|
|
63
63
|
|
|
64
64
|
try {
|