@barchart/portfolio-api-common 1.2.30 → 1.2.34
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,9 +1,13 @@
|
|
|
1
1
|
const assert = require('@barchart/common-js/lang/assert'),
|
|
2
|
+
ComparatorBuilder = require('@barchart/common-js/collections/sorting/ComparatorBuilder'),
|
|
3
|
+
comparators = require('@barchart/common-js/collections/sorting/comparators'),
|
|
4
|
+
Day = require('@barchart/common-js/lang/Day'),
|
|
2
5
|
Decimal = require('@barchart/common-js/lang/Decimal'),
|
|
3
6
|
is = require('@barchart/common-js/lang/is'),
|
|
4
7
|
formatter = require('@barchart/common-js/lang/formatter');
|
|
5
8
|
|
|
6
|
-
const
|
|
9
|
+
const InstrumentType = require('./../data/InstrumentType'),
|
|
10
|
+
TransactionType = require('./../data/TransactionType');
|
|
7
11
|
|
|
8
12
|
module.exports = (() => {
|
|
9
13
|
'use strict';
|
|
@@ -19,33 +23,30 @@ module.exports = (() => {
|
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
/**
|
|
22
|
-
* Maps transaction objects into new objects whose properties are human-readable or
|
|
23
|
-
*
|
|
24
|
-
* each transaction.
|
|
26
|
+
* Maps transaction objects into new objects whose properties are human-readable (or
|
|
27
|
+
* mutates the original objects, adding a "formatted" property to each transaction).
|
|
25
28
|
*
|
|
26
29
|
* @public
|
|
27
30
|
* @static
|
|
28
|
-
* @param {Array
|
|
29
|
-
* @param {Array
|
|
30
|
-
* @param {Boolean=}
|
|
31
|
+
* @param {Array.<Object>} transactions
|
|
32
|
+
* @param {Array.<Object>} positions
|
|
33
|
+
* @param {Boolean=} mutate
|
|
31
34
|
* @returns {Array}
|
|
32
35
|
*/
|
|
33
|
-
static format(transactions, positions,
|
|
36
|
+
static format(transactions, positions, mutate) {
|
|
34
37
|
assert.argumentIsArray(transactions, 'transactions');
|
|
35
38
|
assert.argumentIsArray(positions, 'positions');
|
|
36
|
-
assert.argumentIsOptional(
|
|
39
|
+
assert.argumentIsOptional(mutate, 'mutate', Boolean);
|
|
37
40
|
|
|
38
41
|
const instruments = positions.reduce((map, p) => {
|
|
39
42
|
const instrument = Object.assign({ }, p.instrument || { });
|
|
40
43
|
|
|
41
|
-
delete instrument.id;
|
|
42
|
-
|
|
43
44
|
map[p.position] = instrument;
|
|
44
45
|
|
|
45
46
|
return map;
|
|
46
47
|
}, { });
|
|
47
48
|
|
|
48
|
-
|
|
49
|
+
const a = transactions.reduce((list, transaction) => {
|
|
49
50
|
const position = transaction.position;
|
|
50
51
|
|
|
51
52
|
if (instruments.hasOwnProperty(position)) {
|
|
@@ -69,7 +70,7 @@ module.exports = (() => {
|
|
|
69
70
|
|
|
70
71
|
let transactionToInsert;
|
|
71
72
|
|
|
72
|
-
if (
|
|
73
|
+
if (mutate) {
|
|
73
74
|
transaction.formatted = formatted;
|
|
74
75
|
|
|
75
76
|
transactionToInsert = transaction;
|
|
@@ -82,6 +83,26 @@ module.exports = (() => {
|
|
|
82
83
|
|
|
83
84
|
return list;
|
|
84
85
|
}, [ ]);
|
|
86
|
+
|
|
87
|
+
a.sort(comparator);
|
|
88
|
+
|
|
89
|
+
a.forEach((t) => {
|
|
90
|
+
delete t.instrument.id;
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
return a;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Sorts an array of formatted transaction objects.
|
|
98
|
+
*
|
|
99
|
+
* @public
|
|
100
|
+
* @static
|
|
101
|
+
* @param {Array.<Object>} transactions
|
|
102
|
+
* @returns {Array}
|
|
103
|
+
*/
|
|
104
|
+
sort(transactions) {
|
|
105
|
+
return transactions.sort(comparator);
|
|
85
106
|
}
|
|
86
107
|
|
|
87
108
|
toString() {
|
|
@@ -237,5 +258,23 @@ module.exports = (() => {
|
|
|
237
258
|
return formatted;
|
|
238
259
|
});
|
|
239
260
|
|
|
261
|
+
function getInstrumentTypePriority(type) {
|
|
262
|
+
if (type === InstrumentType.CASH) {
|
|
263
|
+
return 1;
|
|
264
|
+
} else {
|
|
265
|
+
return 0;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const comparator = ComparatorBuilder.startWith((a, b) => {
|
|
270
|
+
return Day.compareDays(b.date, a.date);
|
|
271
|
+
}).thenBy((a, b) => {
|
|
272
|
+
return comparators.compareNumbers(getInstrumentTypePriority(a.instrument.type), getInstrumentTypePriority(b.instrument.type));
|
|
273
|
+
}).thenBy((a, b) => {
|
|
274
|
+
return comparators.compareStrings(a.instrument.id, b.instrument.id);
|
|
275
|
+
}).thenBy((a, b) => {
|
|
276
|
+
return comparators.compareNumbers(a.sequence, b.sequence);
|
|
277
|
+
}).toComparator();
|
|
278
|
+
|
|
240
279
|
return TransactionFormatter;
|
|
241
280
|
})();
|
|
@@ -101,7 +101,7 @@ module.exports = (() => {
|
|
|
101
101
|
.withField('name', DataType.STRING)
|
|
102
102
|
.withField('timezone', DataType.forEnum(Timezones, 'Timezone'))
|
|
103
103
|
.withField('dates.create', DataType.DAY)
|
|
104
|
-
.withField('
|
|
104
|
+
.withField('defaults.cash', DataType.BOOLEAN, true)
|
|
105
105
|
.withField('defaults.currency', DataType.forEnum(Currency, 'Currency'))
|
|
106
106
|
.withField('defaults.reinvest', DataType.BOOLEAN, true)
|
|
107
107
|
.withField('defaults.valuation', DataType.forEnum(ValuationType, 'ValuationType'))
|
|
@@ -123,7 +123,7 @@ module.exports = (() => {
|
|
|
123
123
|
.withField('name', DataType.STRING)
|
|
124
124
|
.withField('timezone', DataType.forEnum(Timezones, 'Timezone'))
|
|
125
125
|
.withField('dates.create', DataType.DAY)
|
|
126
|
-
.withField('
|
|
126
|
+
.withField('defaults.cash', DataType.BOOLEAN, true)
|
|
127
127
|
.withField('defaults.currency', DataType.forEnum(Currency, 'Currency'))
|
|
128
128
|
.withField('defaults.reinvest', DataType.BOOLEAN, true)
|
|
129
129
|
.withField('defaults.valuation', DataType.forEnum(ValuationType, 'ValuationType'))
|
|
@@ -146,7 +146,7 @@ module.exports = (() => {
|
|
|
146
146
|
const create = new PortfolioSchema(SchemaBuilder.withName('create')
|
|
147
147
|
.withField('name', DataType.STRING)
|
|
148
148
|
.withField('timezone', DataType.forEnum(Timezones, 'Timezone'))
|
|
149
|
-
.withField('
|
|
149
|
+
.withField('defaults.cash', DataType.BOOLEAN, true)
|
|
150
150
|
.withField('defaults.currency', DataType.forEnum(Currency, 'Currency'), true)
|
|
151
151
|
.withField('defaults.reinvest', DataType.BOOLEAN, true)
|
|
152
152
|
.withField('defaults.valuation', DataType.forEnum(ValuationType, 'ValuationType'), true)
|
|
@@ -158,6 +158,7 @@ module.exports = (() => {
|
|
|
158
158
|
.withField('portfolio', DataType.STRING)
|
|
159
159
|
.withField('name', DataType.STRING)
|
|
160
160
|
.withField('timezone', DataType.forEnum(Timezones, 'Timezone'), true)
|
|
161
|
+
.withField('defaults.cash', DataType.BOOLEAN, true)
|
|
161
162
|
.withField('defaults.currency', DataType.forEnum(Currency, 'Currency'), true)
|
|
162
163
|
.withField('defaults.reinvest', DataType.BOOLEAN, true)
|
|
163
164
|
.withField('miscellany', DataType.AD_HOC, true)
|
|
@@ -87,8 +87,9 @@ module.exports = (() => {
|
|
|
87
87
|
.withField('position', DataType.STRING)
|
|
88
88
|
.withField('open', DataType.BOOLEAN, true)
|
|
89
89
|
.withField('transaction', DataType.NUMBER)
|
|
90
|
-
.withField('
|
|
90
|
+
.withField('cash', DataType.BOOLEAN, true)
|
|
91
91
|
.withField('reinvest', DataType.BOOLEAN, true)
|
|
92
|
+
.withField('valuation', DataType.forEnum(ValuationType, 'ValuationType'))
|
|
92
93
|
.withField('snapshot.date', DataType.DAY)
|
|
93
94
|
.withField('snapshot.open', DataType.DECIMAL)
|
|
94
95
|
.withField('snapshot.direction', DataType.forEnum(PositionDirection, 'PositionDirection'))
|
|
@@ -120,8 +121,9 @@ module.exports = (() => {
|
|
|
120
121
|
.withField('position', DataType.STRING)
|
|
121
122
|
.withField('open', DataType.BOOLEAN, true)
|
|
122
123
|
.withField('transaction', DataType.NUMBER)
|
|
123
|
-
.withField('
|
|
124
|
+
.withField('cash', DataType.BOOLEAN, true)
|
|
124
125
|
.withField('reinvest', DataType.BOOLEAN, true)
|
|
126
|
+
.withField('valuation', DataType.forEnum(ValuationType, 'ValuationType'))
|
|
125
127
|
.withField('snapshot.date', DataType.DAY)
|
|
126
128
|
.withField('snapshot.open', DataType.DECIMAL)
|
|
127
129
|
.withField('snapshot.direction', DataType.forEnum(PositionDirection, 'PositionDirection'))
|
|
@@ -143,6 +145,7 @@ module.exports = (() => {
|
|
|
143
145
|
.withField('mapping.currency', DataType.forEnum(Currency, 'Currency'), true)
|
|
144
146
|
.withField('mapping.symbol.barchart', DataType.STRING, true)
|
|
145
147
|
.withField('mapping.symbol.display', DataType.STRING, true)
|
|
148
|
+
.withField('cash', DataType.BOOLEAN, true)
|
|
146
149
|
.withField('reinvest', DataType.BOOLEAN, true)
|
|
147
150
|
.schema
|
|
148
151
|
);
|