@barchart/portfolio-api-common 1.2.90 → 1.2.94

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.
@@ -86,17 +86,52 @@ module.exports = (() => {
86
86
  return this._rangeCalculator(getFilteredTransactions(transactions));
87
87
  }
88
88
 
89
+ /**
90
+ * Returns the range which contains a given date and all subsequent ranges.
91
+ *
92
+ * @public
93
+ * @param {Day} date
94
+ * @return {Array.<PositionSummaryRange>}
95
+ */
96
+ getRangesFromDate(date) {
97
+ assert.argumentIsRequired(date, 'date', Day, 'Day');
98
+
99
+ const transaction = { date: date, snapshot: { open: Decimal.ONE } };
100
+
101
+ return this.getRanges([ transaction ]);
102
+ }
103
+
104
+ /**
105
+ * Returns the range immediately prior to the range containing the
106
+ * date supplied.
107
+ *
108
+ * @public
109
+ * @param {Day} date
110
+ * @param {Number} periods
111
+ */
112
+ getPriorRanges(date, periods) {
113
+ assert.argumentIsRequired(date, 'date', Day, 'Day');
114
+ assert.argumentIsRequired(periods, 'periods', Number, 'Number');
115
+
116
+ const transactionOne = { date: this.getStartDate((periods - 1), date), snapshot: { open: Decimal.ONE } };
117
+ const transactionTwo = { date: date, snapshot: { open: Decimal.ZERO } };
118
+
119
+ return this._rangeCalculator([ transactionOne, transactionTwo ]);
120
+ }
121
+
89
122
  /**
90
123
  * Returns the start date for a frame, a given number of periods ago.
91
124
  *
92
125
  * @public
93
126
  * @param {Number} periods
127
+ * @param {Day=} start
94
128
  * @returns {Day}
95
129
  */
96
- getStartDate(periods) {
130
+ getStartDate(periods, start) {
97
131
  assert.argumentIsRequired(periods, 'periods', Number);
132
+ assert.argumentIsOptional(start, 'start', Day, 'Day');
98
133
 
99
- return this._startDateCalculator(periods);
134
+ return this._startDateCalculator(periods, start);
100
135
  }
101
136
 
102
137
  /**
@@ -158,6 +193,16 @@ module.exports = (() => {
158
193
  * @property {Day} end
159
194
  */
160
195
 
196
+ /**
197
+ * The start and and date for a {@link PositionSummaryFrame} along with the frame type.
198
+ *
199
+ * @typedef PositionSummaryDefinition
200
+ * @type {Object}
201
+ * @property {Day} start
202
+ * @property {Day} end
203
+ * @property {PositionSummaryFrame} frame
204
+ */
205
+
161
206
  function getRange(start, end) {
162
207
  return {
163
208
  start: start,
@@ -219,29 +264,29 @@ module.exports = (() => {
219
264
  return ranges;
220
265
  }
221
266
 
222
- function getYearlyStartDate(periods) {
223
- const today = Day.getToday();
267
+ function getYearlyStartDate(periods, date) {
268
+ const today = date || Day.getToday();
224
269
 
225
- return Day.getToday()
270
+ return today
226
271
  .subtractMonths(today.month - 1)
227
272
  .subtractDays(today.day)
228
273
  .subtractYears(periods);
229
274
  }
230
275
 
231
- function getQuarterlyStartDate(periods) {
276
+ function getQuarterlyStartDate(periods, date) {
232
277
  return null;
233
278
  }
234
279
 
235
- function getMonthlyStartDate(periods) {
280
+ function getMonthlyStartDate(periods, date) {
236
281
  return null;
237
282
  }
238
283
 
239
- function getYearToDateStartDate(periods) {
284
+ function getYearToDateStartDate(periods, date) {
240
285
  return null;
241
286
  }
242
287
 
243
288
  function getYearlyRangeDescription(start, end) {
244
- return end.year.toString();
289
+ return `Year ended ${end.year.toString()}`;
245
290
  }
246
291
 
247
292
  function getQuarterlyRangeDescription(start, end) {
@@ -42,7 +42,7 @@ module.exports = (() => {
42
42
  * @param {Array.<Object>} portfolios - The portfolios.
43
43
  * @param {Array.<Object>} positions - The positions (for all of the portfolios).
44
44
  * @param {Array.<Object>} summaries - The positions summaries (for all of the positions).
45
- * @param {PositionSummaryFrame} - If specified, locks the current (and previous) periods to a specific frame, use for reporting.
45
+ * @param {PositionSummaryFrame=} - If specified, locks the current (and previous) periods to a specific frame, use for reporting.
46
46
  */
47
47
  class PositionContainer {
48
48
  constructor(definitions, portfolios, positions, summaries, frame) {
@@ -11,6 +11,7 @@ module.exports = (() => {
11
11
  * @public
12
12
  * @param {String} name
13
13
  * @param {Array.<PositionLevelDefinition>} definitions
14
+ * @oaram {Array.<String>=} exclusionDependencies
14
15
  */
15
16
  class PositionTreeDefinitions {
16
17
  constructor(name, definitions, exclusionDependencies) {
@@ -0,0 +1,58 @@
1
+ const DataType = require('@barchart/common-js/serialization/json/DataType'),
2
+ Enum = require('@barchart/common-js/lang/Enum'),
3
+ Schema = require('@barchart/common-js/serialization/json/Schema'),
4
+ SchemaBuilder = require('@barchart/common-js/serialization/json/builders/SchemaBuilder');
5
+
6
+ const PositionSummaryFrame = require('./../data/PositionSummaryFrame');
7
+
8
+ module.exports = (() => {
9
+ 'use strict';
10
+
11
+ /**
12
+ * The schemas which can be used to represent position summary objects.
13
+ *
14
+ * @public
15
+ * @extends {Enum}
16
+ */
17
+ class PositionSummaryDefinitionSchema extends Enum {
18
+ constructor(schema) {
19
+ super(schema.name, schema.name);
20
+
21
+ this._schema = schema;
22
+ }
23
+
24
+ /**
25
+ * The actual {@link Schema}.
26
+ *
27
+ * @public
28
+ * @returns {Schema}
29
+ */
30
+ get schema() {
31
+ return this._schema;
32
+ }
33
+
34
+ /**
35
+ * The complete position summary definition schema.
36
+ *
37
+ * @static
38
+ * @public
39
+ * @returns {PositionSummaryDefinitionSchema}
40
+ */
41
+ static get COMPLETE() {
42
+ return complete;
43
+ }
44
+
45
+ toString() {
46
+ return '[PositionSummaryDefinitionSchema]';
47
+ }
48
+ }
49
+
50
+ const complete = new PositionSummaryDefinitionSchema(SchemaBuilder.withName('complete')
51
+ .withField('start', DataType.DAY)
52
+ .withField('end', DataType.DAY)
53
+ .withField('frame', DataType.forEnum(PositionSummaryFrame, 'PositionSummaryFrame'))
54
+ .schema
55
+ );
56
+
57
+ return PositionSummaryDefinitionSchema;
58
+ })();
@@ -1,5 +1,4 @@
1
- const Currency = require('@barchart/common-js/lang/Currency'),
2
- DataType = require('@barchart/common-js/serialization/json/DataType'),
1
+ const DataType = require('@barchart/common-js/serialization/json/DataType'),
3
2
  Enum = require('@barchart/common-js/lang/Enum'),
4
3
  Schema = require('@barchart/common-js/serialization/json/Schema'),
5
4
  SchemaBuilder = require('@barchart/common-js/serialization/json/builders/SchemaBuilder');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.90",
3
+ "version": "1.2.94",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -477,17 +477,52 @@ module.exports = (() => {
477
477
  return this._rangeCalculator(getFilteredTransactions(transactions));
478
478
  }
479
479
 
480
+ /**
481
+ * Returns the range which contains a given date and all subsequent ranges.
482
+ *
483
+ * @public
484
+ * @param {Day} date
485
+ * @return {Array.<PositionSummaryRange>}
486
+ */
487
+ getRangesFromDate(date) {
488
+ assert.argumentIsRequired(date, 'date', Day, 'Day');
489
+
490
+ const transaction = { date: date, snapshot: { open: Decimal.ONE } };
491
+
492
+ return this.getRanges([ transaction ]);
493
+ }
494
+
495
+ /**
496
+ * Returns the range immediately prior to the range containing the
497
+ * date supplied.
498
+ *
499
+ * @public
500
+ * @param {Day} date
501
+ * @param {Number} periods
502
+ */
503
+ getPriorRanges(date, periods) {
504
+ assert.argumentIsRequired(date, 'date', Day, 'Day');
505
+ assert.argumentIsRequired(periods, 'periods', Number, 'Number');
506
+
507
+ const transactionOne = { date: this.getStartDate((periods - 1), date), snapshot: { open: Decimal.ONE } };
508
+ const transactionTwo = { date: date, snapshot: { open: Decimal.ZERO } };
509
+
510
+ return this._rangeCalculator([ transactionOne, transactionTwo ]);
511
+ }
512
+
480
513
  /**
481
514
  * Returns the start date for a frame, a given number of periods ago.
482
515
  *
483
516
  * @public
484
517
  * @param {Number} periods
518
+ * @param {Day=} start
485
519
  * @returns {Day}
486
520
  */
487
- getStartDate(periods) {
521
+ getStartDate(periods, start) {
488
522
  assert.argumentIsRequired(periods, 'periods', Number);
523
+ assert.argumentIsOptional(start, 'start', Day, 'Day');
489
524
 
490
- return this._startDateCalculator(periods);
525
+ return this._startDateCalculator(periods, start);
491
526
  }
492
527
 
493
528
  /**
@@ -549,6 +584,16 @@ module.exports = (() => {
549
584
  * @property {Day} end
550
585
  */
551
586
 
587
+ /**
588
+ * The start and and date for a {@link PositionSummaryFrame} along with the frame type.
589
+ *
590
+ * @typedef PositionSummaryDefinition
591
+ * @type {Object}
592
+ * @property {Day} start
593
+ * @property {Day} end
594
+ * @property {PositionSummaryFrame} frame
595
+ */
596
+
552
597
  function getRange(start, end) {
553
598
  return {
554
599
  start: start,
@@ -610,29 +655,29 @@ module.exports = (() => {
610
655
  return ranges;
611
656
  }
612
657
 
613
- function getYearlyStartDate(periods) {
614
- const today = Day.getToday();
658
+ function getYearlyStartDate(periods, date) {
659
+ const today = date || Day.getToday();
615
660
 
616
- return Day.getToday()
661
+ return today
617
662
  .subtractMonths(today.month - 1)
618
663
  .subtractDays(today.day)
619
664
  .subtractYears(periods);
620
665
  }
621
666
 
622
- function getQuarterlyStartDate(periods) {
667
+ function getQuarterlyStartDate(periods, date) {
623
668
  return null;
624
669
  }
625
670
 
626
- function getMonthlyStartDate(periods) {
671
+ function getMonthlyStartDate(periods, date) {
627
672
  return null;
628
673
  }
629
674
 
630
- function getYearToDateStartDate(periods) {
675
+ function getYearToDateStartDate(periods, date) {
631
676
  return null;
632
677
  }
633
678
 
634
679
  function getYearlyRangeDescription(start, end) {
635
- return end.year.toString();
680
+ return `Year ended ${end.year.toString()}`;
636
681
  }
637
682
 
638
683
  function getQuarterlyRangeDescription(start, end) {
@@ -1421,7 +1466,7 @@ module.exports = (() => {
1421
1466
  * @param {Array.<Object>} portfolios - The portfolios.
1422
1467
  * @param {Array.<Object>} positions - The positions (for all of the portfolios).
1423
1468
  * @param {Array.<Object>} summaries - The positions summaries (for all of the positions).
1424
- * @param {PositionSummaryFrame} - If specified, locks the current (and previous) periods to a specific frame, use for reporting.
1469
+ * @param {PositionSummaryFrame=} - If specified, locks the current (and previous) periods to a specific frame, use for reporting.
1425
1470
  */
1426
1471
  class PositionContainer {
1427
1472
  constructor(definitions, portfolios, positions, summaries, frame) {
@@ -4171,6 +4216,7 @@ module.exports = (() => {
4171
4216
  * @public
4172
4217
  * @param {String} name
4173
4218
  * @param {Array.<PositionLevelDefinition>} definitions
4219
+ * @oaram {Array.<String>=} exclusionDependencies
4174
4220
  */
4175
4221
  class PositionTreeDefinitions {
4176
4222
  constructor(name, definitions, exclusionDependencies) {
@@ -17609,6 +17655,38 @@ describe('After the PositionSummaryFrame enumeration is initialized', () => {
17609
17655
  });
17610
17656
  });
17611
17657
  });
17658
+
17659
+ describe('and prior ranges are calculated', () => {
17660
+ describe('for YEARLY ranges', () => {
17661
+ describe('from 2017-10-10, including one previous ranges', () => {
17662
+ let ranges;
17663
+
17664
+ beforeEach(() => {
17665
+ ranges = PositionSummaryFrame.YEARLY.getPriorRanges(new Day(2015, 4, 20), 1);
17666
+ });
17667
+
17668
+ it('should return two ranges', () => {
17669
+ expect(ranges.length).toEqual(2);
17670
+ });
17671
+
17672
+ it('the first range should begin on 2013-12-31', () => {
17673
+ expect(ranges[0].start.getIsEqual(new Day(2013, 12, 31))).toEqual(true);
17674
+ });
17675
+
17676
+ it('the first range should end on 2014-12-31', () => {
17677
+ expect(ranges[0].end.getIsEqual(new Day(2014, 12, 31))).toEqual(true);
17678
+ });
17679
+
17680
+ it('the second range should begin on 2014-12-31', () => {
17681
+ expect(ranges[1].start.getIsEqual(new Day(2014, 12, 31))).toEqual(true);
17682
+ });
17683
+
17684
+ it('the second range should end on 2015-12-31', () => {
17685
+ expect(ranges[1].end.getIsEqual(new Day(2015, 12, 31))).toEqual(true);
17686
+ });
17687
+ });
17688
+ });
17689
+ });
17612
17690
  });
17613
17691
 
17614
17692
  },{"./../../../lib/data/PositionSummaryFrame":3,"./../../../lib/data/TransactionType":4,"@barchart/common-js/lang/Day":21,"@barchart/common-js/lang/Decimal":22}],53:[function(require,module,exports){
@@ -422,4 +422,36 @@ describe('After the PositionSummaryFrame enumeration is initialized', () => {
422
422
  });
423
423
  });
424
424
  });
425
+
426
+ describe('and prior ranges are calculated', () => {
427
+ describe('for YEARLY ranges', () => {
428
+ describe('from 2017-10-10, including one previous ranges', () => {
429
+ let ranges;
430
+
431
+ beforeEach(() => {
432
+ ranges = PositionSummaryFrame.YEARLY.getPriorRanges(new Day(2015, 4, 20), 1);
433
+ });
434
+
435
+ it('should return two ranges', () => {
436
+ expect(ranges.length).toEqual(2);
437
+ });
438
+
439
+ it('the first range should begin on 2013-12-31', () => {
440
+ expect(ranges[0].start.getIsEqual(new Day(2013, 12, 31))).toEqual(true);
441
+ });
442
+
443
+ it('the first range should end on 2014-12-31', () => {
444
+ expect(ranges[0].end.getIsEqual(new Day(2014, 12, 31))).toEqual(true);
445
+ });
446
+
447
+ it('the second range should begin on 2014-12-31', () => {
448
+ expect(ranges[1].start.getIsEqual(new Day(2014, 12, 31))).toEqual(true);
449
+ });
450
+
451
+ it('the second range should end on 2015-12-31', () => {
452
+ expect(ranges[1].end.getIsEqual(new Day(2015, 12, 31))).toEqual(true);
453
+ });
454
+ });
455
+ });
456
+ });
425
457
  });