@barchart/portfolio-api-common 1.2.50 → 1.2.54

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.
@@ -192,7 +192,7 @@ module.exports = (() => {
192
192
  } else if (code === 5 || code == 15) {
193
193
  return InstrumentType.FUND;
194
194
  } else {
195
- throw new Error('Unable to determine InstrumentType for [', code, ']');
195
+ throw new Error(`Unable to determine InstrumentType for [ ${code} ]`);
196
196
  }
197
197
  }
198
198
 
@@ -12,6 +12,7 @@ module.exports = (() => {
12
12
  * @param {String} code
13
13
  * @param {String} description
14
14
  * @param {String} display
15
+ * @param {Number} sequence
15
16
  * @param {Boolean} purchase
16
17
  * @param {Boolean} sale
17
18
  * @param {Boolean} income
@@ -21,12 +22,14 @@ module.exports = (() => {
21
22
  * @param {Boolean} corporateAction
22
23
  * @param {Boolean} initial
23
24
  * @param {Boolean} significant
24
- */
25
+ * @param {Boolean} eod
26
+ */
25
27
  class TransactionType extends Enum {
26
- constructor(code, description, display, purchase, sale, income, opening, closing, fee, corporateAction, initial, significant) {
28
+ constructor(code, description, display, sequence, purchase, sale, income, opening, closing, fee, corporateAction, initial, significant) {
27
29
  super(code, description);
28
30
 
29
31
  assert.argumentIsRequired(display, 'display', String);
32
+ assert.argumentIsRequired(sequence, 'sequence', Number);
30
33
  assert.argumentIsRequired(purchase, 'purchase', Boolean);
31
34
  assert.argumentIsRequired(sale, 'sale', Boolean);
32
35
  assert.argumentIsRequired(income, 'income', Boolean);
@@ -38,6 +41,7 @@ module.exports = (() => {
38
41
  assert.argumentIsRequired(significant, 'significant', Boolean);
39
42
 
40
43
  this._display = display;
44
+ this._sequence = sequence;
41
45
  this._purchase = purchase;
42
46
  this._sale = sale;
43
47
  this._income = income;
@@ -59,6 +63,17 @@ module.exports = (() => {
59
63
  return this._display;
60
64
  }
61
65
 
66
+ /**
67
+ * Specifies ordering when multiple transactions occur on the same day, for
68
+ * the same position.
69
+ *
70
+ * @public
71
+ * @returns {Number}
72
+ */
73
+ get sequence() {
74
+ return this._sequence;
75
+ }
76
+
62
77
  /**
63
78
  * Indicates if the transaction was a trade.
64
79
  *
@@ -376,28 +391,28 @@ module.exports = (() => {
376
391
  }
377
392
  }
378
393
 
379
- const buy = new TransactionType('B', 'Buy', 'Buy', true, false, false, true, false, false, false, true, true);
380
- const sell = new TransactionType('S', 'Sell', 'Sell', false, true, false, false, true, false, false, false, true);
381
- const buyShort = new TransactionType('BS', 'Buy To Cover', 'Buy To Cover', true, false, false, false, true, false, false, false, true);
382
- const sellShort = new TransactionType('SS', 'Sell Short', 'Sell Short', false, true, false, true, false, false, false, true, true);
383
- const dividend = new TransactionType('DV', 'Dividend', 'Dividend', false, false, true, false, false, false, true, false, false);
384
- const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', 'Dividend Reinvest', false, false, false, true, false, false, true, false, false);
385
- const dividendStock = new TransactionType('DS', 'Dividend (Stock)', 'Dividend Stock', false, false, false, true, false, false, true, false, false);
386
- const split = new TransactionType('SP', 'Split', 'Split', false, false, false, true, false, false, true, false, false);
387
- const fee = new TransactionType('F', 'Fee', 'Fee', false, false, false, false, false, true, false, false, false);
388
- const feeUnits = new TransactionType('FU', 'Fee Units', 'Fee', false, false, false, false, true, false, false, false, false);
389
-
390
- const distributionCash = new TransactionType('DC', 'Distribution (Cash)', 'Cash Distribution', false, false, true, false, false, false, true, false, false);
391
- const distributionReinvest = new TransactionType('DY', 'Distribution (Reinvested)', 'Distribution Reinvest', false, false, false, true, false, false, true, false, false);
392
- const distributionFund = new TransactionType('DF', 'Distribution (Units)', 'Unit Distribution', false, false, false, true, false, false, true, false, false);
393
-
394
- const deposit = new TransactionType('D', 'Deposit', 'Deposit', false, false, false, false, false, false, false, true, true);
395
- const withdrawal = new TransactionType('W', 'Withdrawal', 'Withdrawal', false, false, false, false, false, false, false, true, true);
396
- const debit = new TransactionType('DR', 'Debit', 'Debit', false, false, false, false, false, false, false, true, true);
397
- const credit = new TransactionType('CR', 'Credit', 'Credit', false, false, false, false, false, false, false, true, true);
398
-
399
- const valuation = new TransactionType('V', 'Valuation', 'Valuation', false, false, false, false, false, false, false, false, false);
400
- const income = new TransactionType('I', 'Income', 'Income', false, false, true, false, false, false, false, false, false);
394
+ const buy = new TransactionType('B', 'Buy', 'Buy', 0, true, false, false, true, false, false, false, true, true);
395
+ const sell = new TransactionType('S', 'Sell', 'Sell', 0, false, true, false, false, true, false, false, false, true);
396
+ const buyShort = new TransactionType('BS', 'Buy To Cover', 'Buy To Cover', 0, true, false, false, false, true, false, false, false, true);
397
+ const sellShort = new TransactionType('SS', 'Sell Short', 'Sell Short', 0, false, true, false, true, false, false, false, true, true);
398
+ const dividend = new TransactionType('DV', 'Dividend', 'Dividend', 1, false, false, true, false, false, false, true, false, false);
399
+ const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', 'Dividend Reinvest', 1, false, false, false, true, false, false, true, false, false);
400
+ const dividendStock = new TransactionType('DS', 'Dividend (Stock)', 'Dividend Stock', 1, false, false, false, true, false, false, true, false, false);
401
+ const split = new TransactionType('SP', 'Split', 'Split', 1, false, false, false, true, false, false, true, false, false);
402
+ const fee = new TransactionType('F', 'Fee', 'Fee', 0, false, false, false, false, false, true, false, false, false);
403
+ const feeUnits = new TransactionType('FU', 'Fee Units', 'Fee', 0, false, false, false, false, true, false, false, false, false);
404
+
405
+ const distributionCash = new TransactionType('DC', 'Distribution (Cash)', 'Cash Distribution', 1, false, false, true, false, false, false, true, false, false);
406
+ const distributionReinvest = new TransactionType('DY', 'Distribution (Reinvested)', 'Distribution Reinvest', 1, false, false, false, true, false, false, true, false, false);
407
+ const distributionFund = new TransactionType('DF', 'Distribution (Units)', 'Unit Distribution', 1, false, false, false, true, false, false, true, false, false);
408
+
409
+ const deposit = new TransactionType('D', 'Deposit', 'Deposit', 0, false, false, false, false, false, false, false, true, true);
410
+ const withdrawal = new TransactionType('W', 'Withdrawal', 'Withdrawal', 0, false, false, false, false, false, false, false, true, true);
411
+ const debit = new TransactionType('DR', 'Debit', 'Debit', 0, false, false, false, false, false, false, false, true, true);
412
+ const credit = new TransactionType('CR', 'Credit', 'Credit', 0, false, false, false, false, false, false, false, true, true);
413
+
414
+ const valuation = new TransactionType('V', 'Valuation', 'Valuation', 0, false, false, false, false, false, false, false, false, false);
415
+ const income = new TransactionType('I', 'Income', 'Income', 0, false, false, true, false, false, false, false, false, false);
401
416
 
402
417
  return TransactionType;
403
418
  })();
@@ -82,7 +82,7 @@ module.exports = (() => {
82
82
  static getInvalidIndex(transactions) {
83
83
  assert.argumentIsArray(transactions, 'transactions');
84
84
 
85
- return transactions.findIndex((t, i) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(transactions[i - 1].date)));
85
+ return transactions.findIndex((t, i, a) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(a[ i - 1 ].date)) || (i !== 0 && t.date.getIsEqual(a[i - 1].date) && t.type.sequence < a[i - 1].type.sequence));
86
86
  }
87
87
 
88
88
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.50",
3
+ "version": "1.2.54",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -193,7 +193,7 @@ module.exports = (() => {
193
193
  } else if (code === 5 || code == 15) {
194
194
  return InstrumentType.FUND;
195
195
  } else {
196
- throw new Error('Unable to determine InstrumentType for [', code, ']');
196
+ throw new Error(`Unable to determine InstrumentType for [ ${code} ]`);
197
197
  }
198
198
  }
199
199
 
@@ -635,6 +635,7 @@ module.exports = (() => {
635
635
  * @param {String} code
636
636
  * @param {String} description
637
637
  * @param {String} display
638
+ * @param {Number} sequence
638
639
  * @param {Boolean} purchase
639
640
  * @param {Boolean} sale
640
641
  * @param {Boolean} income
@@ -644,12 +645,14 @@ module.exports = (() => {
644
645
  * @param {Boolean} corporateAction
645
646
  * @param {Boolean} initial
646
647
  * @param {Boolean} significant
647
- */
648
+ * @param {Boolean} eod
649
+ */
648
650
  class TransactionType extends Enum {
649
- constructor(code, description, display, purchase, sale, income, opening, closing, fee, corporateAction, initial, significant) {
651
+ constructor(code, description, display, sequence, purchase, sale, income, opening, closing, fee, corporateAction, initial, significant) {
650
652
  super(code, description);
651
653
 
652
654
  assert.argumentIsRequired(display, 'display', String);
655
+ assert.argumentIsRequired(sequence, 'sequence', Number);
653
656
  assert.argumentIsRequired(purchase, 'purchase', Boolean);
654
657
  assert.argumentIsRequired(sale, 'sale', Boolean);
655
658
  assert.argumentIsRequired(income, 'income', Boolean);
@@ -661,6 +664,7 @@ module.exports = (() => {
661
664
  assert.argumentIsRequired(significant, 'significant', Boolean);
662
665
 
663
666
  this._display = display;
667
+ this._sequence = sequence;
664
668
  this._purchase = purchase;
665
669
  this._sale = sale;
666
670
  this._income = income;
@@ -682,6 +686,17 @@ module.exports = (() => {
682
686
  return this._display;
683
687
  }
684
688
 
689
+ /**
690
+ * Specifies ordering when multiple transactions occur on the same day, for
691
+ * the same position.
692
+ *
693
+ * @public
694
+ * @returns {Number}
695
+ */
696
+ get sequence() {
697
+ return this._sequence;
698
+ }
699
+
685
700
  /**
686
701
  * Indicates if the transaction was a trade.
687
702
  *
@@ -999,28 +1014,28 @@ module.exports = (() => {
999
1014
  }
1000
1015
  }
1001
1016
 
1002
- const buy = new TransactionType('B', 'Buy', 'Buy', true, false, false, true, false, false, false, true, true);
1003
- const sell = new TransactionType('S', 'Sell', 'Sell', false, true, false, false, true, false, false, false, true);
1004
- const buyShort = new TransactionType('BS', 'Buy To Cover', 'Buy To Cover', true, false, false, false, true, false, false, false, true);
1005
- const sellShort = new TransactionType('SS', 'Sell Short', 'Sell Short', false, true, false, true, false, false, false, true, true);
1006
- const dividend = new TransactionType('DV', 'Dividend', 'Dividend', false, false, true, false, false, false, true, false, false);
1007
- const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', 'Dividend Reinvest', false, false, false, true, false, false, true, false, false);
1008
- const dividendStock = new TransactionType('DS', 'Dividend (Stock)', 'Dividend Stock', false, false, false, true, false, false, true, false, false);
1009
- const split = new TransactionType('SP', 'Split', 'Split', false, false, false, true, false, false, true, false, false);
1010
- const fee = new TransactionType('F', 'Fee', 'Fee', false, false, false, false, false, true, false, false, false);
1011
- const feeUnits = new TransactionType('FU', 'Fee Units', 'Fee', false, false, false, false, true, false, false, false, false);
1012
-
1013
- const distributionCash = new TransactionType('DC', 'Distribution (Cash)', 'Cash Distribution', false, false, true, false, false, false, true, false, false);
1014
- const distributionReinvest = new TransactionType('DY', 'Distribution (Reinvested)', 'Distribution Reinvest', false, false, false, true, false, false, true, false, false);
1015
- const distributionFund = new TransactionType('DF', 'Distribution (Units)', 'Unit Distribution', false, false, false, true, false, false, true, false, false);
1016
-
1017
- const deposit = new TransactionType('D', 'Deposit', 'Deposit', false, false, false, false, false, false, false, true, true);
1018
- const withdrawal = new TransactionType('W', 'Withdrawal', 'Withdrawal', false, false, false, false, false, false, false, true, true);
1019
- const debit = new TransactionType('DR', 'Debit', 'Debit', false, false, false, false, false, false, false, true, true);
1020
- const credit = new TransactionType('CR', 'Credit', 'Credit', false, false, false, false, false, false, false, true, true);
1021
-
1022
- const valuation = new TransactionType('V', 'Valuation', 'Valuation', false, false, false, false, false, false, false, false, false);
1023
- const income = new TransactionType('I', 'Income', 'Income', false, false, true, false, false, false, false, false, false);
1017
+ const buy = new TransactionType('B', 'Buy', 'Buy', 0, true, false, false, true, false, false, false, true, true);
1018
+ const sell = new TransactionType('S', 'Sell', 'Sell', 0, false, true, false, false, true, false, false, false, true);
1019
+ const buyShort = new TransactionType('BS', 'Buy To Cover', 'Buy To Cover', 0, true, false, false, false, true, false, false, false, true);
1020
+ const sellShort = new TransactionType('SS', 'Sell Short', 'Sell Short', 0, false, true, false, true, false, false, false, true, true);
1021
+ const dividend = new TransactionType('DV', 'Dividend', 'Dividend', 1, false, false, true, false, false, false, true, false, false);
1022
+ const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', 'Dividend Reinvest', 1, false, false, false, true, false, false, true, false, false);
1023
+ const dividendStock = new TransactionType('DS', 'Dividend (Stock)', 'Dividend Stock', 1, false, false, false, true, false, false, true, false, false);
1024
+ const split = new TransactionType('SP', 'Split', 'Split', 1, false, false, false, true, false, false, true, false, false);
1025
+ const fee = new TransactionType('F', 'Fee', 'Fee', 0, false, false, false, false, false, true, false, false, false);
1026
+ const feeUnits = new TransactionType('FU', 'Fee Units', 'Fee', 0, false, false, false, false, true, false, false, false, false);
1027
+
1028
+ const distributionCash = new TransactionType('DC', 'Distribution (Cash)', 'Cash Distribution', 1, false, false, true, false, false, false, true, false, false);
1029
+ const distributionReinvest = new TransactionType('DY', 'Distribution (Reinvested)', 'Distribution Reinvest', 1, false, false, false, true, false, false, true, false, false);
1030
+ const distributionFund = new TransactionType('DF', 'Distribution (Units)', 'Unit Distribution', 1, false, false, false, true, false, false, true, false, false);
1031
+
1032
+ const deposit = new TransactionType('D', 'Deposit', 'Deposit', 0, false, false, false, false, false, false, false, true, true);
1033
+ const withdrawal = new TransactionType('W', 'Withdrawal', 'Withdrawal', 0, false, false, false, false, false, false, false, true, true);
1034
+ const debit = new TransactionType('DR', 'Debit', 'Debit', 0, false, false, false, false, false, false, false, true, true);
1035
+ const credit = new TransactionType('CR', 'Credit', 'Credit', 0, false, false, false, false, false, false, false, true, true);
1036
+
1037
+ const valuation = new TransactionType('V', 'Valuation', 'Valuation', 0, false, false, false, false, false, false, false, false, false);
1038
+ const income = new TransactionType('I', 'Income', 'Income', 0, false, false, true, false, false, false, false, false, false);
1024
1039
 
1025
1040
  return TransactionType;
1026
1041
  })();
@@ -1110,7 +1125,7 @@ module.exports = (() => {
1110
1125
  static getInvalidIndex(transactions) {
1111
1126
  assert.argumentIsArray(transactions, 'transactions');
1112
1127
 
1113
- return transactions.findIndex((t, i) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(transactions[i - 1].date)));
1128
+ return transactions.findIndex((t, i, a) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(a[ i - 1 ].date)) || (i !== 0 && t.date.getIsEqual(a[i - 1].date) && t.type.sequence < a[i - 1].type.sequence));
1114
1129
  }
1115
1130
 
1116
1131
  /**
@@ -17197,13 +17212,14 @@ describe('After the PositionSummaryFrame enumeration is initialized', () => {
17197
17212
  },{"./../../../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){
17198
17213
  const Day = require('@barchart/common-js/lang/Day');
17199
17214
 
17200
- const TransactionValidator = require('./../../../lib/data/TransactionValidator');
17215
+ const TransactionType = require('./../../../lib/data/TransactionType'),
17216
+ TransactionValidator = require('./../../../lib/data/TransactionValidator');
17201
17217
 
17202
17218
  describe('When validating transaction order', () => {
17203
17219
  'use strict';
17204
17220
 
17205
- const build = (sequence, day) => {
17206
- return { sequence: sequence, date: Day.parse(day) };
17221
+ const build = (sequence, day, type) => {
17222
+ return { sequence: sequence, date: Day.parse(day), type: (type || TransactionType.BUY ) };
17207
17223
  };
17208
17224
 
17209
17225
  it('An array of zero transactions should be valid', () => {
@@ -17214,8 +17230,16 @@ describe('When validating transaction order', () => {
17214
17230
  expect(TransactionValidator.validateOrder([ build(1, '2018-04-30'), build(2, '2018-04-30'), build(3, '2018-04-30') ])).toEqual(true);
17215
17231
  });
17216
17232
 
17233
+ it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs last, should be valid', () => {
17234
+ expect(TransactionValidator.validateOrder([ build(1, '2018-04-30'), build(2, '2018-04-30', TransactionType.DIVIDEND) ])).toEqual(true);
17235
+ });
17236
+
17237
+ it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs first, should not be valid', () => {
17238
+ expect(TransactionValidator.validateOrder([ build(1, '2018-04-30', TransactionType.DIVIDEND), build(2, '2018-04-30') ])).toEqual(false);
17239
+ });
17240
+
17217
17241
  it('An array of transactions with ordered sequences, on the sequential days should be valid', () => {
17218
- expect(TransactionValidator.validateOrder([ build(1, '2018-04-30'), build(2, '2018-05-01'), build(3, '2018-05-02') ])).toEqual(true);
17242
+ expect(TransactionValidator.validateOrder([ build(1, '2018-04-30'), build(2, '2018-05-01'), build(3, '2018-05-02', TransactionType.DIVIDEND) ])).toEqual(true);
17219
17243
  });
17220
17244
 
17221
17245
  it('An array of transactions with ordered sequences (starting after one), on the same day should not be valid', () => {
@@ -17276,7 +17300,7 @@ describe('When requesting all the user-initiated transaction types', () => {
17276
17300
  expect(userInitiated.length).toEqual(9);
17277
17301
  });
17278
17302
  });
17279
- },{"./../../../lib/data/TransactionValidator":5,"@barchart/common-js/lang/Day":21}],54:[function(require,module,exports){
17303
+ },{"./../../../lib/data/TransactionType":4,"./../../../lib/data/TransactionValidator":5,"@barchart/common-js/lang/Day":21}],54:[function(require,module,exports){
17280
17304
  const Currency = require('@barchart/common-js/lang/Currency'),
17281
17305
  Decimal = require('@barchart/common-js/lang/Decimal');
17282
17306
 
@@ -1,12 +1,13 @@
1
1
  const Day = require('@barchart/common-js/lang/Day');
2
2
 
3
- const TransactionValidator = require('./../../../lib/data/TransactionValidator');
3
+ const TransactionType = require('./../../../lib/data/TransactionType'),
4
+ TransactionValidator = require('./../../../lib/data/TransactionValidator');
4
5
 
5
6
  describe('When validating transaction order', () => {
6
7
  'use strict';
7
8
 
8
- const build = (sequence, day) => {
9
- return { sequence: sequence, date: Day.parse(day) };
9
+ const build = (sequence, day, type) => {
10
+ return { sequence: sequence, date: Day.parse(day), type: (type || TransactionType.BUY ) };
10
11
  };
11
12
 
12
13
  it('An array of zero transactions should be valid', () => {
@@ -17,8 +18,16 @@ describe('When validating transaction order', () => {
17
18
  expect(TransactionValidator.validateOrder([ build(1, '2018-04-30'), build(2, '2018-04-30'), build(3, '2018-04-30') ])).toEqual(true);
18
19
  });
19
20
 
21
+ it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs last, should be valid', () => {
22
+ expect(TransactionValidator.validateOrder([ build(1, '2018-04-30'), build(2, '2018-04-30', TransactionType.DIVIDEND) ])).toEqual(true);
23
+ });
24
+
25
+ it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs first, should not be valid', () => {
26
+ expect(TransactionValidator.validateOrder([ build(1, '2018-04-30', TransactionType.DIVIDEND), build(2, '2018-04-30') ])).toEqual(false);
27
+ });
28
+
20
29
  it('An array of transactions with ordered sequences, on the sequential days should be valid', () => {
21
- expect(TransactionValidator.validateOrder([ build(1, '2018-04-30'), build(2, '2018-05-01'), build(3, '2018-05-02') ])).toEqual(true);
30
+ expect(TransactionValidator.validateOrder([ build(1, '2018-04-30'), build(2, '2018-05-01'), build(3, '2018-05-02', TransactionType.DIVIDEND) ])).toEqual(true);
22
31
  });
23
32
 
24
33
  it('An array of transactions with ordered sequences (starting after one), on the same day should not be valid', () => {