@barchart/portfolio-api-common 1.0.98 → 1.0.102

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.
@@ -136,16 +136,22 @@ module.exports = (() => {
136
136
  const levelDefinition = levelDefinitions[0];
137
137
 
138
138
  const populatedObjects = array.groupBy(items, levelDefinition.keySelector);
139
- const populatedGroups = Object.keys(populatedObjects).map(key => populatedObjects[key]).map((items) => {
139
+ const populatedGroups = Object.keys(populatedObjects).reduce((list, key) => {
140
+ const items = populatedObjects[key];
140
141
  const first = items[0];
141
142
 
142
- return new PositionGroup(this, parent, items, levelDefinition.currencySelector(first), levelDefinition.descriptionSelector(first), levelDefinition.single && items.length === 1);
143
- });
143
+ list.push(new PositionGroup(this, parent, items, levelDefinition.currencySelector(first), key, levelDefinition.descriptionSelector(first), levelDefinition.single && items.length === 1));
144
+
145
+ return list;
146
+ }, [ ]);
144
147
 
145
- const missingGroups = array.difference(levelDefinition.requiredGroups.map(group => group.description), populatedGroups.map(group => group.description));
148
+ const missingGroups = array.difference(levelDefinition.requiredGroups.map(group => group.key), populatedGroups.map(group => group.key))
149
+ .map((key) => {
150
+ return levelDefinition.requiredGroups.find(g => g.key === key);
151
+ });
146
152
 
147
- const empty = missingGroups.map((description) => {
148
- return new PositionGroup(this, parent, [ ], levelDefinition.requiredGroups.find(group => group.description === description).currency, description);
153
+ const empty = missingGroups.map((group) => {
154
+ return new PositionGroup(this, parent, [ ], group.currency, group.key, group.description);
149
155
  });
150
156
 
151
157
  const compositeGroups = populatedGroups.concat(empty);
@@ -268,11 +274,7 @@ module.exports = (() => {
268
274
  }
269
275
 
270
276
  function findNode(tree, keys) {
271
- return keys.reduce((tree, key) => {
272
- tree = tree.findChild(group => group.description === key);
273
-
274
- return tree;
275
- }, tree);
277
+ return keys.reduce((tree, key) => tree.findChild(group => group.key === key), tree);
276
278
  }
277
279
 
278
280
  function getSummaryArray(ranges) {
@@ -12,13 +12,14 @@ module.exports = (() => {
12
12
  * @public
13
13
  */
14
14
  class PositionGroup {
15
- constructor(container, parent, items, currency, description, single) {
15
+ constructor(container, parent, items, currency, key, description, single) {
16
16
  this._container = container;
17
17
  this._parent = parent || null;
18
18
 
19
19
  this._items = items;
20
20
  this._currency = currency || Currency.CAD;
21
21
 
22
+ this._key = key;
22
23
  this._description = description;
23
24
 
24
25
  this._single = is.boolean(single) && single;
@@ -30,9 +31,16 @@ module.exports = (() => {
30
31
 
31
32
  this._dataFormat = { };
32
33
  this._dataActual = { };
33
-
34
+
35
+ this._dataFormat.key = this._key;
34
36
  this._dataFormat.description = this._description;
35
-
37
+
38
+ if (this._single) {
39
+ this._dataFormat.instrument = items[0].position.instrument;
40
+ } else {
41
+ this._dataFormat.instrument = null;
42
+ }
43
+
36
44
  this._dataActual.currentPrice = null;
37
45
  this._dataActual.previousPrice = null;
38
46
  this._dataActual.basis = null;
@@ -79,16 +87,20 @@ module.exports = (() => {
79
87
  this.refresh();
80
88
  }
81
89
 
82
- get items() {
83
- return this._items;
90
+ get key() {
91
+ return this._key;
92
+ }
93
+
94
+ get description() {
95
+ return this._description;
84
96
  }
85
97
 
86
98
  get currency() {
87
99
  return this._currency;
88
100
  }
89
101
 
90
- get description() {
91
- return this._description;
102
+ get items() {
103
+ return this._items;
92
104
  }
93
105
 
94
106
  get data() {
@@ -14,7 +14,7 @@ module.exports = (() => {
14
14
  * @param {PositionLevelDefinition~keySelector} keySelector
15
15
  * @param {PositionLevelDefinition~descriptionSelector} descriptionSelector
16
16
  * @param {PositionLevelDefinition~currencySelector} currencySelector
17
- * @param {Array.<String>=} requiredGroups
17
+ * @param {Array.<PositionLevelDefinition~RequiredGroup>=} requiredGroups
18
18
  * @param {Boolean=} single
19
19
  */
20
20
  class PositionLevelDefinition {
@@ -139,5 +139,16 @@ module.exports = (() => {
139
139
  * @returns {Currency}
140
140
  */
141
141
 
142
+ /**
143
+ * The data required to construct a group.
144
+ *
145
+ * @public
146
+ * @typedef PositionLevelDefinition~RequiredGroup
147
+ * @type {Object}
148
+ * @property {String} key
149
+ * @property {String} description
150
+ * @property {Currency} currency
151
+ */
152
+
142
153
  return PositionLevelDefinition;
143
154
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.98",
3
+ "version": "1.0.102",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -852,16 +852,22 @@ module.exports = (() => {
852
852
  const levelDefinition = levelDefinitions[0];
853
853
 
854
854
  const populatedObjects = array.groupBy(items, levelDefinition.keySelector);
855
- const populatedGroups = Object.keys(populatedObjects).map(key => populatedObjects[key]).map((items) => {
855
+ const populatedGroups = Object.keys(populatedObjects).reduce((list, key) => {
856
+ const items = populatedObjects[key];
856
857
  const first = items[0];
857
858
 
858
- return new PositionGroup(this, parent, items, levelDefinition.currencySelector(first), levelDefinition.descriptionSelector(first), levelDefinition.single && items.length === 1);
859
- });
859
+ list.push(new PositionGroup(this, parent, items, levelDefinition.currencySelector(first), key, levelDefinition.descriptionSelector(first), levelDefinition.single && items.length === 1));
860
+
861
+ return list;
862
+ }, [ ]);
860
863
 
861
- const missingGroups = array.difference(levelDefinition.requiredGroups.map(group => group.description), populatedGroups.map(group => group.description));
864
+ const missingGroups = array.difference(levelDefinition.requiredGroups.map(group => group.key), populatedGroups.map(group => group.key))
865
+ .map((key) => {
866
+ return levelDefinition.requiredGroups.find(g => g.key === key);
867
+ });
862
868
 
863
- const empty = missingGroups.map((description) => {
864
- return new PositionGroup(this, parent, [ ], levelDefinition.requiredGroups.find(group => group.description === description).currency, description);
869
+ const empty = missingGroups.map((group) => {
870
+ return new PositionGroup(this, parent, [ ], group.currency, group.key, group.description);
865
871
  });
866
872
 
867
873
  const compositeGroups = populatedGroups.concat(empty);
@@ -984,11 +990,7 @@ module.exports = (() => {
984
990
  }
985
991
 
986
992
  function findNode(tree, keys) {
987
- return keys.reduce((tree, key) => {
988
- tree = tree.findChild(group => group.description === key);
989
-
990
- return tree;
991
- }, tree);
993
+ return keys.reduce((tree, key) => tree.findChild(group => group.key === key), tree);
992
994
  }
993
995
 
994
996
  function getSummaryArray(ranges) {
@@ -1013,13 +1015,14 @@ module.exports = (() => {
1013
1015
  * @public
1014
1016
  */
1015
1017
  class PositionGroup {
1016
- constructor(container, parent, items, currency, description, single) {
1018
+ constructor(container, parent, items, currency, key, description, single) {
1017
1019
  this._container = container;
1018
1020
  this._parent = parent || null;
1019
1021
 
1020
1022
  this._items = items;
1021
1023
  this._currency = currency || Currency.CAD;
1022
1024
 
1025
+ this._key = key;
1023
1026
  this._description = description;
1024
1027
 
1025
1028
  this._single = is.boolean(single) && single;
@@ -1031,9 +1034,16 @@ module.exports = (() => {
1031
1034
 
1032
1035
  this._dataFormat = { };
1033
1036
  this._dataActual = { };
1034
-
1037
+
1038
+ this._dataFormat.key = this._key;
1035
1039
  this._dataFormat.description = this._description;
1036
-
1040
+
1041
+ if (this._single) {
1042
+ this._dataFormat.instrument = items[0].position.instrument;
1043
+ } else {
1044
+ this._dataFormat.instrument = null;
1045
+ }
1046
+
1037
1047
  this._dataActual.currentPrice = null;
1038
1048
  this._dataActual.previousPrice = null;
1039
1049
  this._dataActual.basis = null;
@@ -1080,16 +1090,20 @@ module.exports = (() => {
1080
1090
  this.refresh();
1081
1091
  }
1082
1092
 
1083
- get items() {
1084
- return this._items;
1093
+ get key() {
1094
+ return this._key;
1095
+ }
1096
+
1097
+ get description() {
1098
+ return this._description;
1085
1099
  }
1086
1100
 
1087
1101
  get currency() {
1088
1102
  return this._currency;
1089
1103
  }
1090
1104
 
1091
- get description() {
1092
- return this._description;
1105
+ get items() {
1106
+ return this._items;
1093
1107
  }
1094
1108
 
1095
1109
  get data() {
@@ -1570,7 +1584,7 @@ module.exports = (() => {
1570
1584
  * @param {PositionLevelDefinition~keySelector} keySelector
1571
1585
  * @param {PositionLevelDefinition~descriptionSelector} descriptionSelector
1572
1586
  * @param {PositionLevelDefinition~currencySelector} currencySelector
1573
- * @param {Array.<String>=} requiredGroups
1587
+ * @param {Array.<PositionLevelDefinition~RequiredGroup>=} requiredGroups
1574
1588
  * @param {Boolean=} single
1575
1589
  */
1576
1590
  class PositionLevelDefinition {
@@ -1695,6 +1709,17 @@ module.exports = (() => {
1695
1709
  * @returns {Currency}
1696
1710
  */
1697
1711
 
1712
+ /**
1713
+ * The data required to construct a group.
1714
+ *
1715
+ * @public
1716
+ * @typedef PositionLevelDefinition~RequiredGroup
1717
+ * @type {Object}
1718
+ * @property {String} key
1719
+ * @property {String} description
1720
+ * @property {Currency} currency
1721
+ */
1722
+
1698
1723
  return PositionLevelDefinition;
1699
1724
  })();
1700
1725
 
@@ -6178,18 +6203,18 @@ describe('When a position container data is gathered', () => {
6178
6203
  beforeEach(() => {
6179
6204
  portfolios = [
6180
6205
  {
6181
- portfolio: 'a',
6206
+ portfolio: 'My First Portfolio',
6182
6207
  name: 'a'
6183
6208
  }, {
6184
- portfolio: 'b',
6209
+ portfolio: 'My Second Portfolio',
6185
6210
  name: 'b'
6186
6211
  }
6187
6212
  ];
6188
6213
 
6189
6214
  positions = [
6190
- getPosition('a', 'AAPL'),
6191
- getPosition('b', 'AAPL'),
6192
- getPosition('b', 'TSLA')
6215
+ getPosition('My First Portfolio', 'AAPL'),
6216
+ getPosition('My Second Portfolio', 'AAPL'),
6217
+ getPosition('My Second Portfolio', 'TSLA')
6193
6218
  ];
6194
6219
 
6195
6220
  summaries = [ ];
@@ -6203,7 +6228,7 @@ describe('When a position container data is gathered', () => {
6203
6228
  beforeEach(() => {
6204
6229
  definitions = [
6205
6230
  new PositionTreeDefinition(name = 'the only tree', [
6206
- new PositionLevelDefinition('Total', x => true, x => 'Total', x => Currency.CAD),
6231
+ new PositionLevelDefinition('Total', x => 'totals', x => 'Total', x => Currency.CAD),
6207
6232
  new PositionLevelDefinition('Portfolio', x => x.portfolio.portfolio, x => x.portfolio.name, x => Currency.CAD),
6208
6233
  new PositionLevelDefinition('Position', x => x.position.position, x => x.position.instrument.symbol.barchart, x => x.position.instrument.currency)
6209
6234
  ])
@@ -6217,27 +6242,27 @@ describe('When a position container data is gathered', () => {
6217
6242
  });
6218
6243
 
6219
6244
  it('the "Total" group should have two children groups', () => {
6220
- expect(container.getGroups(name, [ 'Total' ]).length).toEqual(2);
6245
+ expect(container.getGroups(name, [ 'totals' ]).length).toEqual(2);
6221
6246
  });
6222
6247
 
6223
6248
  it('the "Total" group should have three items', () => {
6224
- expect(container.getGroup(name, [ 'Total' ]).items.length).toEqual(3);
6249
+ expect(container.getGroup(name, [ 'totals' ]).items.length).toEqual(3);
6225
6250
  });
6226
6251
 
6227
6252
  it('The "a" portfolio group should have one child group', () => {
6228
- expect(container.getGroups(name, [ 'Total', 'a' ]).length).toEqual(1);
6253
+ expect(container.getGroups(name, [ 'totals', 'My First Portfolio' ]).length).toEqual(1);
6229
6254
  });
6230
6255
 
6231
6256
  it('the "a" portfolio group should have one item', () => {
6232
- expect(container.getGroup(name, [ 'Total', 'a' ]).items.length).toEqual(1);
6257
+ expect(container.getGroup(name, [ 'totals', 'My First Portfolio' ]).items.length).toEqual(1);
6233
6258
  });
6234
6259
 
6235
6260
  it('The "b" portfolio group should have two child groups', () => {
6236
- expect(container.getGroups(name, [ 'Total', 'b' ]).length).toEqual(2);
6261
+ expect(container.getGroups(name, [ 'totals', 'My Second Portfolio' ]).length).toEqual(2);
6237
6262
  });
6238
6263
 
6239
6264
  it('the "b" portfolio group should have two items', () => {
6240
- expect(container.getGroup(name, [ 'Total', 'b' ]).items.length).toEqual(2);
6265
+ expect(container.getGroup(name, [ 'totals', 'My Second Portfolio' ]).items.length).toEqual(2);
6241
6266
  });
6242
6267
  });
6243
6268
  });
@@ -41,18 +41,18 @@ describe('When a position container data is gathered', () => {
41
41
  beforeEach(() => {
42
42
  portfolios = [
43
43
  {
44
- portfolio: 'a',
44
+ portfolio: 'My First Portfolio',
45
45
  name: 'a'
46
46
  }, {
47
- portfolio: 'b',
47
+ portfolio: 'My Second Portfolio',
48
48
  name: 'b'
49
49
  }
50
50
  ];
51
51
 
52
52
  positions = [
53
- getPosition('a', 'AAPL'),
54
- getPosition('b', 'AAPL'),
55
- getPosition('b', 'TSLA')
53
+ getPosition('My First Portfolio', 'AAPL'),
54
+ getPosition('My Second Portfolio', 'AAPL'),
55
+ getPosition('My Second Portfolio', 'TSLA')
56
56
  ];
57
57
 
58
58
  summaries = [ ];
@@ -66,7 +66,7 @@ describe('When a position container data is gathered', () => {
66
66
  beforeEach(() => {
67
67
  definitions = [
68
68
  new PositionTreeDefinition(name = 'the only tree', [
69
- new PositionLevelDefinition('Total', x => true, x => 'Total', x => Currency.CAD),
69
+ new PositionLevelDefinition('Total', x => 'totals', x => 'Total', x => Currency.CAD),
70
70
  new PositionLevelDefinition('Portfolio', x => x.portfolio.portfolio, x => x.portfolio.name, x => Currency.CAD),
71
71
  new PositionLevelDefinition('Position', x => x.position.position, x => x.position.instrument.symbol.barchart, x => x.position.instrument.currency)
72
72
  ])
@@ -80,27 +80,27 @@ describe('When a position container data is gathered', () => {
80
80
  });
81
81
 
82
82
  it('the "Total" group should have two children groups', () => {
83
- expect(container.getGroups(name, [ 'Total' ]).length).toEqual(2);
83
+ expect(container.getGroups(name, [ 'totals' ]).length).toEqual(2);
84
84
  });
85
85
 
86
86
  it('the "Total" group should have three items', () => {
87
- expect(container.getGroup(name, [ 'Total' ]).items.length).toEqual(3);
87
+ expect(container.getGroup(name, [ 'totals' ]).items.length).toEqual(3);
88
88
  });
89
89
 
90
90
  it('The "a" portfolio group should have one child group', () => {
91
- expect(container.getGroups(name, [ 'Total', 'a' ]).length).toEqual(1);
91
+ expect(container.getGroups(name, [ 'totals', 'My First Portfolio' ]).length).toEqual(1);
92
92
  });
93
93
 
94
94
  it('the "a" portfolio group should have one item', () => {
95
- expect(container.getGroup(name, [ 'Total', 'a' ]).items.length).toEqual(1);
95
+ expect(container.getGroup(name, [ 'totals', 'My First Portfolio' ]).items.length).toEqual(1);
96
96
  });
97
97
 
98
98
  it('The "b" portfolio group should have two child groups', () => {
99
- expect(container.getGroups(name, [ 'Total', 'b' ]).length).toEqual(2);
99
+ expect(container.getGroups(name, [ 'totals', 'My Second Portfolio' ]).length).toEqual(2);
100
100
  });
101
101
 
102
102
  it('the "b" portfolio group should have two items', () => {
103
- expect(container.getGroup(name, [ 'Total', 'b' ]).items.length).toEqual(2);
103
+ expect(container.getGroup(name, [ 'totals', 'My Second Portfolio' ]).items.length).toEqual(2);
104
104
  });
105
105
  });
106
106
  });