@barchart/portfolio-api-common 1.2.113 → 1.2.114

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.
@@ -241,7 +241,32 @@ module.exports = (() => {
241
241
  }
242
242
 
243
243
  function getMonthlyRanges(transactions) {
244
- return [ ];
244
+ const ranges = [ ];
245
+
246
+ if (!transactions.length) {
247
+ return ranges;
248
+ }
249
+
250
+ const first = array.first(transactions);
251
+ const last = array.last(transactions);
252
+
253
+ const firstDate = first.date;
254
+ let lastDate;
255
+
256
+ lastDate = last.snapshot.open.getIsZero()
257
+ ? new Day(last.date.year, last.date.month, last.date.day).addMonths(1)
258
+ : Day.getToday();
259
+ lastDate = lastDate.getEndOfMonth();
260
+
261
+ for (
262
+ let end = firstDate.getEndOfMonth();
263
+ end.format() <= lastDate.format();
264
+ end = end.addMonths(1).getEndOfMonth()
265
+ ) {
266
+ ranges.push(getRange(end.subtractMonths(1).getEndOfMonth(), end));
267
+ }
268
+
269
+ return ranges;
245
270
  }
246
271
 
247
272
  function getYearToDateRanges(transactions) {
@@ -278,7 +303,11 @@ module.exports = (() => {
278
303
  }
279
304
 
280
305
  function getMonthlyStartDate(periods, date) {
281
- return null;
306
+ const today = date || Day.getToday();
307
+
308
+ return today
309
+ .subtractMonths(periods)
310
+ .subtractDays(today.day);
282
311
  }
283
312
 
284
313
  function getYearToDateStartDate(periods, date) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.113",
3
+ "version": "1.2.114",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -632,7 +632,32 @@ module.exports = (() => {
632
632
  }
633
633
 
634
634
  function getMonthlyRanges(transactions) {
635
- return [ ];
635
+ const ranges = [ ];
636
+
637
+ if (!transactions.length) {
638
+ return ranges;
639
+ }
640
+
641
+ const first = array.first(transactions);
642
+ const last = array.last(transactions);
643
+
644
+ const firstDate = first.date;
645
+ let lastDate;
646
+
647
+ lastDate = last.snapshot.open.getIsZero()
648
+ ? new Day(last.date.year, last.date.month, last.date.day).addMonths(1)
649
+ : Day.getToday();
650
+ lastDate = lastDate.getEndOfMonth();
651
+
652
+ for (
653
+ let end = firstDate.getEndOfMonth();
654
+ end.format() <= lastDate.format();
655
+ end = end.addMonths(1).getEndOfMonth()
656
+ ) {
657
+ ranges.push(getRange(end.subtractMonths(1).getEndOfMonth(), end));
658
+ }
659
+
660
+ return ranges;
636
661
  }
637
662
 
638
663
  function getYearToDateRanges(transactions) {
@@ -669,7 +694,11 @@ module.exports = (() => {
669
694
  }
670
695
 
671
696
  function getMonthlyStartDate(periods, date) {
672
- return null;
697
+ const today = date || Day.getToday();
698
+
699
+ return today
700
+ .subtractMonths(periods)
701
+ .subtractDays(today.day);
673
702
  }
674
703
 
675
704
  function getYearToDateStartDate(periods, date) {
@@ -6311,6 +6340,19 @@ module.exports = function () {
6311
6340
  return this.addYears(years, true);
6312
6341
  }
6313
6342
 
6343
+ /**
6344
+ * Returns a new Day instance for the end of the month of the current instance.
6345
+ *
6346
+ * @public
6347
+ * @returns {Day}
6348
+ */
6349
+
6350
+ }, {
6351
+ key: 'getEndOfMonth',
6352
+ value: function getEndOfMonth() {
6353
+ return new Day(this.year, this.month, Day.getDaysInMonth(this.year, this.month));
6354
+ }
6355
+
6314
6356
  /**
6315
6357
  * Indicates if another {@link Day} occurs before the current instance.
6316
6358
  *
@@ -10103,7 +10145,7 @@ module.exports = function () {
10103
10145
  * the schema.
10104
10146
  *
10105
10147
  * @public
10106
- * @param {data} data
10148
+ * @param {Object} data
10107
10149
  * @returns {Object}
10108
10150
  */
10109
10151
 
@@ -10185,7 +10227,13 @@ module.exports = function () {
10185
10227
  };
10186
10228
 
10187
10229
  return function (key, value) {
10188
- return advance(key).reviver(value);
10230
+ var item = advance(key);
10231
+
10232
+ if (key === '') {
10233
+ return value;
10234
+ } else {
10235
+ return item.reviver(value);
10236
+ }
10189
10237
  };
10190
10238
  }
10191
10239
 
@@ -17827,6 +17875,53 @@ describe('After the PositionSummaryFrame enumeration is initialized', () => {
17827
17875
  });
17828
17876
  });
17829
17877
 
17878
+
17879
+
17880
+ /////
17881
+
17882
+ describe('and month position summary ranges are processed for a transaction set that does not close', () => {
17883
+ let ranges;
17884
+
17885
+ beforeEach(() => {
17886
+ const transactions = [
17887
+ {
17888
+ date: new Day(2018, 10, 20),
17889
+ snapshot: {
17890
+ open: new Decimal(1)
17891
+ },
17892
+ type: TransactionType.BUY
17893
+ },
17894
+ {
17895
+ date: new Day(2018, 11, 21),
17896
+ snapshot: {
17897
+ open: new Decimal(1)
17898
+ },
17899
+ type: TransactionType.BUY
17900
+ }
17901
+ ];
17902
+
17903
+ ranges = PositionSummaryFrame.MONTHLY.getRanges(transactions);
17904
+ });
17905
+
17906
+ it('should have 2 ranges (assuming the current year is 2018 and the current month is November)', () => {
17907
+ expect(ranges.length).toEqual(2);
17908
+ });
17909
+
17910
+ it('the first range should be from 2018-09-30 to 2018-10-31', () => {
17911
+ expect(ranges[0].start.format()).toEqual('2018-09-30');
17912
+ expect(ranges[0].end.format()).toEqual('2018-10-31');
17913
+ });
17914
+
17915
+ it('the second range should be from 2018-10-31 to 2018-11-30', () => {
17916
+ expect(ranges[1].start.format()).toEqual('2018-10-31');
17917
+ expect(ranges[1].end.format()).toEqual('2018-11-30');
17918
+ });
17919
+ });
17920
+
17921
+ ///////
17922
+
17923
+
17924
+
17830
17925
  describe('and getting the start date for yearly frames', () => {
17831
17926
  describe('for one year ago', function() {
17832
17927
  let start;
@@ -17869,6 +17964,55 @@ describe('After the PositionSummaryFrame enumeration is initialized', () => {
17869
17964
  });
17870
17965
  });
17871
17966
 
17967
+
17968
+
17969
+
17970
+ ////
17971
+
17972
+ describe('and getting the start date for monthly frames', () => {
17973
+ describe('for one month ago', function () {
17974
+ let start;
17975
+
17976
+ beforeEach(() => {
17977
+ start = PositionSummaryFrame.MONTHLY.getStartDate(1);
17978
+ });
17979
+
17980
+ it('should be on the last day of month', () => {
17981
+ const today = Day.getToday();
17982
+
17983
+ expect(start.day).toEqual(Day.getDaysInMonth(today.year, today.month - 2));
17984
+ });
17985
+
17986
+ it('should be month ago', () => {
17987
+ expect(start.month).toEqual(Day.getToday().month - 2);
17988
+ });
17989
+ });
17990
+
17991
+ describe('for three months ago', function () {
17992
+ let start;
17993
+
17994
+ beforeEach(() => {
17995
+ start = PositionSummaryFrame.MONTHLY.getStartDate(3);
17996
+ });
17997
+
17998
+ it('should be on the last day of month', () => {
17999
+ const today = Day.getToday();
18000
+
18001
+ expect(start.day).toEqual(Day.getDaysInMonth(today.year, today.month - 4));
18002
+ });
18003
+
18004
+ it('should be 3 month ago', () => {
18005
+ expect(start.month).toEqual(Day.getToday().month - 4);
18006
+ });
18007
+ });
18008
+ });
18009
+
18010
+ ////
18011
+
18012
+
18013
+
18014
+
18015
+
17872
18016
  describe('and recent ranges are calculated', () => {
17873
18017
  let todayYear;
17874
18018
  let todayMonth;
@@ -311,6 +311,53 @@ describe('After the PositionSummaryFrame enumeration is initialized', () => {
311
311
  });
312
312
  });
313
313
 
314
+
315
+
316
+ /////
317
+
318
+ describe('and month position summary ranges are processed for a transaction set that does not close', () => {
319
+ let ranges;
320
+
321
+ beforeEach(() => {
322
+ const transactions = [
323
+ {
324
+ date: new Day(2018, 10, 20),
325
+ snapshot: {
326
+ open: new Decimal(1)
327
+ },
328
+ type: TransactionType.BUY
329
+ },
330
+ {
331
+ date: new Day(2018, 11, 21),
332
+ snapshot: {
333
+ open: new Decimal(1)
334
+ },
335
+ type: TransactionType.BUY
336
+ }
337
+ ];
338
+
339
+ ranges = PositionSummaryFrame.MONTHLY.getRanges(transactions);
340
+ });
341
+
342
+ it('should have 2 ranges (assuming the current year is 2018 and the current month is November)', () => {
343
+ expect(ranges.length).toEqual(2);
344
+ });
345
+
346
+ it('the first range should be from 2018-09-30 to 2018-10-31', () => {
347
+ expect(ranges[0].start.format()).toEqual('2018-09-30');
348
+ expect(ranges[0].end.format()).toEqual('2018-10-31');
349
+ });
350
+
351
+ it('the second range should be from 2018-10-31 to 2018-11-30', () => {
352
+ expect(ranges[1].start.format()).toEqual('2018-10-31');
353
+ expect(ranges[1].end.format()).toEqual('2018-11-30');
354
+ });
355
+ });
356
+
357
+ ///////
358
+
359
+
360
+
314
361
  describe('and getting the start date for yearly frames', () => {
315
362
  describe('for one year ago', function() {
316
363
  let start;
@@ -353,6 +400,55 @@ describe('After the PositionSummaryFrame enumeration is initialized', () => {
353
400
  });
354
401
  });
355
402
 
403
+
404
+
405
+
406
+ ////
407
+
408
+ describe('and getting the start date for monthly frames', () => {
409
+ describe('for one month ago', function () {
410
+ let start;
411
+
412
+ beforeEach(() => {
413
+ start = PositionSummaryFrame.MONTHLY.getStartDate(1);
414
+ });
415
+
416
+ it('should be on the last day of month', () => {
417
+ const today = Day.getToday();
418
+
419
+ expect(start.day).toEqual(Day.getDaysInMonth(today.year, today.month - 2));
420
+ });
421
+
422
+ it('should be month ago', () => {
423
+ expect(start.month).toEqual(Day.getToday().month - 2);
424
+ });
425
+ });
426
+
427
+ describe('for three months ago', function () {
428
+ let start;
429
+
430
+ beforeEach(() => {
431
+ start = PositionSummaryFrame.MONTHLY.getStartDate(3);
432
+ });
433
+
434
+ it('should be on the last day of month', () => {
435
+ const today = Day.getToday();
436
+
437
+ expect(start.day).toEqual(Day.getDaysInMonth(today.year, today.month - 4));
438
+ });
439
+
440
+ it('should be 3 month ago', () => {
441
+ expect(start.month).toEqual(Day.getToday().month - 4);
442
+ });
443
+ });
444
+ });
445
+
446
+ ////
447
+
448
+
449
+
450
+
451
+
356
452
  describe('and recent ranges are calculated', () => {
357
453
  let todayYear;
358
454
  let todayMonth;