@barchart/portfolio-api-common 1.0.14 → 1.0.18
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,5 +1,8 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
1
|
+
const array = require('@barchart/common-js/lang/array'),
|
|
2
|
+
assert = require('@barchart/common-js/lang/assert'),
|
|
3
|
+
Day = require('@barchart/common-js/lang/Day'),
|
|
4
|
+
Enum = require('@barchart/common-js/lang/Enum'),
|
|
5
|
+
is = require('@barchart/common-js/lang/is');
|
|
3
6
|
|
|
4
7
|
module.exports = (() => {
|
|
5
8
|
'use strict';
|
|
@@ -9,13 +12,23 @@ module.exports = (() => {
|
|
|
9
12
|
*
|
|
10
13
|
* @public
|
|
11
14
|
* @extends {Enum}
|
|
12
|
-
* @param {String} description
|
|
13
15
|
* @param {String} code
|
|
14
|
-
* @param {
|
|
16
|
+
* @param {String} description
|
|
17
|
+
* @param {Function} rangeCalculator
|
|
15
18
|
*/
|
|
16
19
|
class PositionSummaryFrame extends Enum {
|
|
17
|
-
constructor(code, description) {
|
|
20
|
+
constructor(code, description, rangeCalculator) {
|
|
18
21
|
super(code, description);
|
|
22
|
+
|
|
23
|
+
assert.argumentIsRequired(rangeCalculator, 'rangeCalculator', Function);
|
|
24
|
+
|
|
25
|
+
this._rangeCalculator = rangeCalculator;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
getRanges(transactions) {
|
|
29
|
+
assert.argumentIsArray(transactions, 'transactions');
|
|
30
|
+
|
|
31
|
+
return this._rangeCalculator(transactions);
|
|
19
32
|
}
|
|
20
33
|
|
|
21
34
|
/**
|
|
@@ -63,10 +76,71 @@ module.exports = (() => {
|
|
|
63
76
|
}
|
|
64
77
|
}
|
|
65
78
|
|
|
66
|
-
const yearly = new PositionSummaryFrame('
|
|
67
|
-
const quarterly = new PositionSummaryFrame('QUARTER', 'quarter');
|
|
68
|
-
const monthly = new PositionSummaryFrame('MONTH', 'month');
|
|
69
|
-
const ytd = new PositionSummaryFrame('YTD', 'year-to-date');
|
|
79
|
+
const yearly = new PositionSummaryFrame('YEARLY', 'year', getYearlyRanges);
|
|
80
|
+
const quarterly = new PositionSummaryFrame('QUARTER', 'quarter', getQuarterlyRanges);
|
|
81
|
+
const monthly = new PositionSummaryFrame('MONTH', 'month', getMonthlyRanges);
|
|
82
|
+
const ytd = new PositionSummaryFrame('YTD', 'year-to-date', getYearToDateRanges);
|
|
83
|
+
|
|
84
|
+
function getRange(start, end) {
|
|
85
|
+
return {
|
|
86
|
+
start: start,
|
|
87
|
+
end: end
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function getYearlyRanges(transactions) {
|
|
92
|
+
const ranges = [ ];
|
|
93
|
+
|
|
94
|
+
if (transactions.length !== 0) {
|
|
95
|
+
const first = array.first(transactions);
|
|
96
|
+
const last = array.last(transactions);
|
|
97
|
+
|
|
98
|
+
const firstDate = first.date;
|
|
99
|
+
const lastDate = last.date;
|
|
100
|
+
|
|
101
|
+
let lastYear;
|
|
102
|
+
|
|
103
|
+
if (last.snapshot.open.getIsZero()) {
|
|
104
|
+
lastYear = last.date.year + 1;
|
|
105
|
+
} else {
|
|
106
|
+
lastYear = Day.getToday().year;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
for (let end = new Day(firstDate.year, 12, 31); end.year < lastYear; end = end.addYears(1)) {
|
|
110
|
+
ranges.push(getRange(end.subtractYears(1), end));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return ranges;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function getQuarterlyRanges(transactions) {
|
|
118
|
+
return [ ];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function getMonthlyRanges(transactions) {
|
|
122
|
+
return [ ];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function getYearToDateRanges(transactions) {
|
|
126
|
+
const ranges = [ ];
|
|
127
|
+
|
|
128
|
+
if (transactions.length !== 0) {
|
|
129
|
+
const first = array.first(transactions);
|
|
130
|
+
const last = array.last(transactions);
|
|
131
|
+
|
|
132
|
+
const currentYear = Day.getToday().year;
|
|
133
|
+
|
|
134
|
+
if (!last.snapshot.open.getIsZero() || last.date.year === currentYear) {
|
|
135
|
+
let end = new Day(Day.getToday().year, 12, 31);
|
|
136
|
+
let start = end.subtractYears(1);
|
|
137
|
+
|
|
138
|
+
ranges.push(getRange(start, end));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return ranges;
|
|
143
|
+
}
|
|
70
144
|
|
|
71
145
|
return PositionSummaryFrame;
|
|
72
146
|
})();
|
|
@@ -61,6 +61,7 @@ module.exports = (() => {
|
|
|
61
61
|
.withField('legacy.warnings', DataType.NUMBER, true)
|
|
62
62
|
.withField('legacy.drops', DataType.NUMBER, true)
|
|
63
63
|
.withField('system.version', DataType.NUMBER, true)
|
|
64
|
+
.withField('data', DataType.OBJECT, true)
|
|
64
65
|
.schema
|
|
65
66
|
);
|
|
66
67
|
|
|
@@ -72,6 +73,7 @@ module.exports = (() => {
|
|
|
72
73
|
.withField('defaults.currency', DataType.forEnum(Currency, 'Currency'))
|
|
73
74
|
.withField('defaults.reinvest', DataType.BOOLEAN, true)
|
|
74
75
|
.withField('defaults.valuation', DataType.forEnum(ValuationType, 'ValuationType'))
|
|
76
|
+
.withField('data', DataType.OBJECT, true)
|
|
75
77
|
.schema
|
|
76
78
|
);
|
|
77
79
|
|
|
@@ -82,6 +84,7 @@ module.exports = (() => {
|
|
|
82
84
|
.withField('defaults.currency', DataType.forEnum(Currency, 'Currency'))
|
|
83
85
|
.withField('defaults.reinvest', DataType.BOOLEAN, true)
|
|
84
86
|
.withField('defaults.valuation', DataType.forEnum(ValuationType, 'ValuationType'))
|
|
87
|
+
.withField('data', DataType.OBJECT, true)
|
|
85
88
|
.schema
|
|
86
89
|
);
|
|
87
90
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@barchart/portfolio-api-common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.18",
|
|
4
4
|
"description": "Common classes used by the Portfolio system",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Bryan Ingle",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"babel-core": "^6.26.0",
|
|
16
16
|
"babel-preset-es2015": "^6.24.1",
|
|
17
|
+
"babelify": "^8.0.0",
|
|
17
18
|
"browserify": "^14.5.0",
|
|
18
19
|
"git-get-status": "^1.0.5",
|
|
19
20
|
"glob": "^6.0.1",
|