@barchart/portfolio-api-common 1.2.39 → 1.2.43

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.
@@ -4,6 +4,15 @@ const Enum = require('@barchart/common-js/lang/Enum'),
4
4
  module.exports = (() => {
5
5
  'use strict';
6
6
 
7
+ /**
8
+ * Types of corporate actions.
9
+ *
10
+ * @public
11
+ * @extends {Enum}
12
+ * @param {String} code
13
+ * @param {String} description
14
+ * @param {Boolean} internal
15
+ */
7
16
  class CorporateActionType extends Enum {
8
17
  constructor(code, description, internal) {
9
18
  super(code, description);
@@ -11,23 +20,75 @@ module.exports = (() => {
11
20
  this._internal = is.boolean(internal) && internal;
12
21
  }
13
22
 
23
+ /**
24
+ * If true, the corporate action is fictitious -- used only for internal
25
+ * system purposes.
26
+ *
27
+ * @public
28
+ * @returns {Boolean}
29
+ */
14
30
  get internal() {
15
31
  return this._internal;
16
32
  }
17
33
 
34
+ /**
35
+ * A symbol change.
36
+ *
37
+ * @public
38
+ * @static
39
+ * @returns {CorporateActionType}
40
+ */
41
+ static get SYMBOL_CHANGE() {
42
+ return symbolChange;
43
+ }
44
+
45
+ /**
46
+ * A name change.
47
+ *
48
+ * @public
49
+ * @static
50
+ * @returns {CorporateActionType}
51
+ */
52
+ static get NAME_CHANGE() {
53
+ return nameChange;
54
+ }
55
+
56
+ /**
57
+ * A dividend.
58
+ *
59
+ * @public
60
+ * @static
61
+ * @returns {CorporateActionType}
62
+ */
18
63
  static get DIVIDEND() {
19
64
  return dividend;
20
65
  }
21
66
 
67
+ /**
68
+ * A split.
69
+ *
70
+ * @public
71
+ * @static
72
+ * @returns {CorporateActionType}
73
+ */
22
74
  static get SPLIT() {
23
75
  return split;
24
76
  }
25
77
 
78
+ /**
79
+ * A fictitious event, used for internal system purposes.
80
+ *
81
+ * @public
82
+ * @static
83
+ * @returns {CorporateActionType}
84
+ */
26
85
  static get JOB() {
27
86
  return job;
28
87
  }
29
88
  }
30
89
 
90
+ const symbolChange = new CorporateActionType('SYMBOL_CHANGE', 'Symbol Change', false);
91
+ const nameChange = new CorporateActionType('NAME_CHANGE', 'Name Change', false);
31
92
  const dividend = new CorporateActionType('DIVIDEND', 'Dividend', false);
32
93
  const split = new CorporateActionType('SPLIT', 'Split', false);
33
94
  const job = new CorporateActionType('JOB', 'Job', true);
@@ -1,5 +1,6 @@
1
1
  const assert = require('@barchart/common-js/lang/assert'),
2
- array = require('@barchart/common-js/lang/array');
2
+ array = require('@barchart/common-js/lang/array'),
3
+ is = require('@barchart/common-js/lang/is');
3
4
 
4
5
  const InstrumentType = require('./InstrumentType'),
5
6
  PositionDirection = require('./PositionDirection'),
@@ -19,7 +20,7 @@ module.exports = (() => {
19
20
  }
20
21
 
21
22
  /**
22
- * Given a set of transaction, ensures that all sequence numbers and dates
23
+ * Given an array of transactions, ensures that all sequence numbers and dates
23
24
  * are properly ordered.
24
25
  *
25
26
  * @public
@@ -32,7 +33,45 @@ module.exports = (() => {
32
33
  }
33
34
 
34
35
  /**
35
- * Given a set of transaction, returns the index of the first transaction that with an invalid
36
+ * Given an array of transactions, when transaction references are present, ensures
37
+ * that no transactions within the set reference the same transaction.
38
+ *
39
+ * @public
40
+ * @static
41
+ * @param {Array.<Object>} transactions
42
+ * @returns {Boolean}
43
+ */
44
+ static validateReferences(transactions) {
45
+ assert.argumentIsArray(transactions, 'transactions');
46
+
47
+ const references = { };
48
+
49
+ return transactions.every((t) => {
50
+ let valid = true;
51
+
52
+ if (is.object(t.reference) && is.string(t.reference.root) && is.number(t.reference.sequence)) {
53
+ const root = t.reference.root;
54
+ const sequence = t.reference.sequence;
55
+
56
+ if (!references.hasOwnProperty(root)) {
57
+ references[root] = [ ];
58
+ }
59
+
60
+ const sequences = references[root];
61
+
62
+ if (sequences.some(s => s === sequence)) {
63
+ valid = false;
64
+ } else {
65
+ sequences.push(sequence);
66
+ }
67
+ }
68
+
69
+ return valid;
70
+ });
71
+ }
72
+
73
+ /**
74
+ * Given an array of transactions, returns the index of the first transaction that with an invalid
36
75
  * sequence number or date.
37
76
  *
38
77
  * @public
@@ -216,7 +216,7 @@ module.exports = (() => {
216
216
  .withField('instrument.symbol.barchart', DataType.STRING, true)
217
217
  .withField('instrument.symbol.display', DataType.STRING, true)
218
218
  .withField('date', DataType.DAY)
219
- .withField('price', DataType.DECIMAL)
219
+ .withField('price', DataType.DECIMAL, true)
220
220
  .withField('quantity', DataType.DECIMAL)
221
221
  .withField('fee', DataType.DECIMAL, true)
222
222
  .withField('reinvest', DataType.BOOLEAN, true)
@@ -231,7 +231,7 @@ module.exports = (() => {
231
231
  .withField('sequence', DataType.NUMBER, true)
232
232
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
233
233
  .withField('date', DataType.DAY)
234
- .withField('price', DataType.DECIMAL)
234
+ .withField('price', DataType.DECIMAL, true)
235
235
  .withField('quantity', DataType.DECIMAL)
236
236
  .withField('fee', DataType.DECIMAL, true)
237
237
  .withField('force', DataType.BOOLEAN, true)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.39",
3
+ "version": "1.2.43",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -1027,7 +1027,8 @@ module.exports = (() => {
1027
1027
 
1028
1028
  },{"@barchart/common-js/lang/Enum":21,"@barchart/common-js/lang/assert":24}],5:[function(require,module,exports){
1029
1029
  const assert = require('@barchart/common-js/lang/assert'),
1030
- array = require('@barchart/common-js/lang/array');
1030
+ array = require('@barchart/common-js/lang/array'),
1031
+ is = require('@barchart/common-js/lang/is');
1031
1032
 
1032
1033
  const InstrumentType = require('./InstrumentType'),
1033
1034
  PositionDirection = require('./PositionDirection'),
@@ -1047,7 +1048,7 @@ module.exports = (() => {
1047
1048
  }
1048
1049
 
1049
1050
  /**
1050
- * Given a set of transaction, ensures that all sequence numbers and dates
1051
+ * Given an array of transactions, ensures that all sequence numbers and dates
1051
1052
  * are properly ordered.
1052
1053
  *
1053
1054
  * @public
@@ -1060,7 +1061,45 @@ module.exports = (() => {
1060
1061
  }
1061
1062
 
1062
1063
  /**
1063
- * Given a set of transaction, returns the index of the first transaction that with an invalid
1064
+ * Given an array of transactions, when transaction references are present, ensures
1065
+ * that no transactions within the set reference the same transaction.
1066
+ *
1067
+ * @public
1068
+ * @static
1069
+ * @param {Array.<Object>} transactions
1070
+ * @returns {Boolean}
1071
+ */
1072
+ static validateReferences(transactions) {
1073
+ assert.argumentIsArray(transactions, 'transactions');
1074
+
1075
+ const references = { };
1076
+
1077
+ return transactions.every((t) => {
1078
+ let valid = true;
1079
+
1080
+ if (is.object(t.reference) && is.string(t.reference.root) && is.number(t.reference.sequence)) {
1081
+ const root = t.reference.root;
1082
+ const sequence = t.reference.sequence;
1083
+
1084
+ if (!references.hasOwnProperty(root)) {
1085
+ references[root] = [ ];
1086
+ }
1087
+
1088
+ const sequences = references[root];
1089
+
1090
+ if (sequences.some(s => s === sequence)) {
1091
+ valid = false;
1092
+ } else {
1093
+ sequences.push(sequence);
1094
+ }
1095
+ }
1096
+
1097
+ return valid;
1098
+ });
1099
+ }
1100
+
1101
+ /**
1102
+ * Given an array of transactions, returns the index of the first transaction that with an invalid
1064
1103
  * sequence number or date.
1065
1104
  *
1066
1105
  * @public
@@ -1258,7 +1297,7 @@ module.exports = (() => {
1258
1297
  return TransactionValidator;
1259
1298
  })();
1260
1299
 
1261
- },{"./InstrumentType":1,"./PositionDirection":2,"./TransactionType":4,"@barchart/common-js/lang/array":23,"@barchart/common-js/lang/assert":24}],6:[function(require,module,exports){
1300
+ },{"./InstrumentType":1,"./PositionDirection":2,"./TransactionType":4,"@barchart/common-js/lang/array":23,"@barchart/common-js/lang/assert":24,"@barchart/common-js/lang/is":26}],6:[function(require,module,exports){
1262
1301
  const array = require('@barchart/common-js/lang/array'),
1263
1302
  assert = require('@barchart/common-js/lang/assert'),
1264
1303
  ComparatorBuilder = require('@barchart/common-js/collections/sorting/ComparatorBuilder'),
@@ -9317,6 +9356,30 @@ describe('When validating transaction order', () => {
9317
9356
  });
9318
9357
  });
9319
9358
 
9359
+ describe('When validating transaction references', () => {
9360
+ 'use strict';
9361
+
9362
+ const build = (root, sequence) => {
9363
+ return { reference: { root: root, sequence: sequence } };
9364
+ };
9365
+
9366
+ it('An array of zero transactions should be valid', () => {
9367
+ expect(TransactionValidator.validateReferences([])).toEqual(true);
9368
+ });
9369
+
9370
+ it('An array with no references should be valid', () => {
9371
+ expect(TransactionValidator.validateReferences([ { }, { } ])).toEqual(true);
9372
+ });
9373
+
9374
+ it('An array with distinct references should be valid', () => {
9375
+ expect(TransactionValidator.validateReferences([ build('a', 1), build('a', 2), build('b', 1) ])).toEqual(true);
9376
+ });
9377
+
9378
+ it('An array with non-distinct references should be not valid', () => {
9379
+ expect(TransactionValidator.validateReferences([ build('a', 1), build('a', 2), build('b', 1), build('a', 2) ])).toEqual(false);
9380
+ });
9381
+ });
9382
+
9320
9383
  describe('When requesting all the user-initiated transaction types', () => {
9321
9384
  'use strict';
9322
9385
 
@@ -9330,13 +9393,6 @@ describe('When requesting all the user-initiated transaction types', () => {
9330
9393
  expect(userInitiated.length).toEqual(9);
9331
9394
  });
9332
9395
  });
9333
-
9334
- describe('When validating direction', () => {
9335
- 'use strict';
9336
-
9337
-
9338
- });
9339
-
9340
9396
  },{"./../../../lib/data/TransactionValidator":5,"@barchart/common-js/lang/Day":18}],37:[function(require,module,exports){
9341
9397
  const Currency = require('@barchart/common-js/lang/Currency'),
9342
9398
  Decimal = require('@barchart/common-js/lang/Decimal');
@@ -42,6 +42,30 @@ describe('When validating transaction order', () => {
42
42
  });
43
43
  });
44
44
 
45
+ describe('When validating transaction references', () => {
46
+ 'use strict';
47
+
48
+ const build = (root, sequence) => {
49
+ return { reference: { root: root, sequence: sequence } };
50
+ };
51
+
52
+ it('An array of zero transactions should be valid', () => {
53
+ expect(TransactionValidator.validateReferences([])).toEqual(true);
54
+ });
55
+
56
+ it('An array with no references should be valid', () => {
57
+ expect(TransactionValidator.validateReferences([ { }, { } ])).toEqual(true);
58
+ });
59
+
60
+ it('An array with distinct references should be valid', () => {
61
+ expect(TransactionValidator.validateReferences([ build('a', 1), build('a', 2), build('b', 1) ])).toEqual(true);
62
+ });
63
+
64
+ it('An array with non-distinct references should be not valid', () => {
65
+ expect(TransactionValidator.validateReferences([ build('a', 1), build('a', 2), build('b', 1), build('a', 2) ])).toEqual(false);
66
+ });
67
+ });
68
+
45
69
  describe('When requesting all the user-initiated transaction types', () => {
46
70
  'use strict';
47
71
 
@@ -54,10 +78,4 @@ describe('When requesting all the user-initiated transaction types', () => {
54
78
  it('Only nine types should be returned', () => {
55
79
  expect(userInitiated.length).toEqual(9);
56
80
  });
57
- });
58
-
59
- describe('When validating direction', () => {
60
- 'use strict';
61
-
62
-
63
- });
81
+ });