@barchart/portfolio-api-common 1.2.40 → 1.2.44

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
@@ -211,6 +211,8 @@ module.exports = (() => {
211
211
  .withField('sequence', DataType.NUMBER, true)
212
212
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
213
213
  .withField('instrument.name', DataType.STRING, true)
214
+ .withField('instrument.exchange', DataType.STRING, true)
215
+ .withField('instrument.code', DataType.NUMBER, true)
214
216
  .withField('instrument.type', DataType.forEnum(InstrumentType, 'InstrumentType'), true)
215
217
  .withField('instrument.currency', DataType.forEnum(Currency, 'Currency'), true)
216
218
  .withField('instrument.symbol.barchart', DataType.STRING, true)
@@ -257,6 +259,8 @@ module.exports = (() => {
257
259
  .withField('sequence', DataType.NUMBER, true)
258
260
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
259
261
  .withField('instrument.name', DataType.STRING, true)
262
+ .withField('instrument.exchange', DataType.STRING, true)
263
+ .withField('instrument.code', DataType.NUMBER, true)
260
264
  .withField('instrument.type', DataType.forEnum(InstrumentType, 'InstrumentType'), true)
261
265
  .withField('instrument.currency', DataType.forEnum(Currency, 'Currency'), true)
262
266
  .withField('instrument.symbol.barchart', DataType.STRING, true)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.40",
3
+ "version": "1.2.44",
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'),
@@ -4635,8 +4674,8 @@ module.exports = function () {
4635
4674
  * Compares two strings (in ascending order), using {@link String#localeCompare}.
4636
4675
  *
4637
4676
  * @static
4638
- * @param {Number} a
4639
- * @param {Number} b
4677
+ * @param {String} a
4678
+ * @param {String} b
4640
4679
  * @returns {Number}
4641
4680
  */
4642
4681
  compareStrings: function compareStrings(a, b) {
@@ -5240,7 +5279,7 @@ module.exports = function () {
5240
5279
  * @returns {String}
5241
5280
  */
5242
5281
  value: function format() {
5243
- return this._year + '-' + leftPad(this._month) + '-' + leftPad(this._day);
5282
+ return leftPad(this._year, 4, '0') + '-' + leftPad(this._month, 2, '0') + '-' + leftPad(this._day, 2, '0');
5244
5283
  }
5245
5284
 
5246
5285
  /**
@@ -5357,7 +5396,7 @@ module.exports = function () {
5357
5396
  *
5358
5397
  * @static
5359
5398
  * @public
5360
- * @return {Day}
5399
+ * @returns {Day}
5361
5400
  */
5362
5401
 
5363
5402
  }, {
@@ -5449,8 +5488,11 @@ module.exports = function () {
5449
5488
 
5450
5489
  var dayRegex = /^([0-9]{4}).?([0-9]{2}).?([0-9]{2})$/;
5451
5490
 
5452
- function leftPad(value) {
5453
- return value < 10 ? '0' + value : '' + value;
5491
+ function leftPad(value, digits, character) {
5492
+ var string = value.toString();
5493
+ var padding = digits - string.length;
5494
+
5495
+ return '' + character.repeat(padding) + string;
5454
5496
  }
5455
5497
 
5456
5498
  var comparator = ComparatorBuilder.startWith(function (a, b) {
@@ -5805,7 +5847,7 @@ module.exports = function () {
5805
5847
  *
5806
5848
  * @public
5807
5849
  * @param {Decimal} instance
5808
- * @return {Boolean}
5850
+ * @returns {Boolean}
5809
5851
  */
5810
5852
  value: function getIsZero(instance) {
5811
5853
  assert.argumentIsRequired(instance, 'instance', Decimal, 'Decimal');
@@ -5818,7 +5860,7 @@ module.exports = function () {
5818
5860
  *
5819
5861
  * @public
5820
5862
  * @param {Decimal} instance
5821
- * @return {Boolean}
5863
+ * @returns {Boolean}
5822
5864
  */
5823
5865
 
5824
5866
  }, {
@@ -5834,7 +5876,7 @@ module.exports = function () {
5834
5876
  *
5835
5877
  * @public
5836
5878
  * @param {Decimal} instance
5837
- * @return {Boolean}
5879
+ * @returns {Boolean}
5838
5880
  */
5839
5881
 
5840
5882
  }, {
@@ -5850,7 +5892,7 @@ module.exports = function () {
5850
5892
  *
5851
5893
  * @public
5852
5894
  * @param {Decimal} instance
5853
- * @return {Boolean}
5895
+ * @returns {Boolean}
5854
5896
  */
5855
5897
 
5856
5898
  }, {
@@ -5866,7 +5908,7 @@ module.exports = function () {
5866
5908
  *
5867
5909
  * @public
5868
5910
  * @param {Decimal} instance
5869
- * @return {Boolean}
5911
+ * @returns {Boolean}
5870
5912
  */
5871
5913
 
5872
5914
  }, {
@@ -5882,7 +5924,7 @@ module.exports = function () {
5882
5924
  *
5883
5925
  * @public
5884
5926
  * @param {Decimal} instance
5885
- * @return {Boolean}
5927
+ * @returns {Boolean}
5886
5928
  */
5887
5929
 
5888
5930
  }, {
@@ -6272,6 +6314,7 @@ module.exports = function () {
6272
6314
  /**
6273
6315
  * The unique code.
6274
6316
  *
6317
+ * @public
6275
6318
  * @returns {String}
6276
6319
  */
6277
6320
 
@@ -6284,6 +6327,7 @@ module.exports = function () {
6284
6327
  * Returns true if the provided {@link Enum} argument is equal
6285
6328
  * to the instance.
6286
6329
  *
6330
+ * @public
6287
6331
  * @param {Enum} other
6288
6332
  * @returns {boolean}
6289
6333
  */
@@ -6308,6 +6352,7 @@ module.exports = function () {
6308
6352
  * Looks up a enumeration item; given the enumeration type and the enumeration
6309
6353
  * item's value. If no matching item can be found, a null value is returned.
6310
6354
  *
6355
+ * @public
6311
6356
  * @param {Function} type - The enumeration type.
6312
6357
  * @param {String} code - The enumeration item's code.
6313
6358
  * @returns {*|null}
@@ -6327,6 +6372,7 @@ module.exports = function () {
6327
6372
  /**
6328
6373
  * The description.
6329
6374
  *
6375
+ * @public
6330
6376
  * @returns {String}
6331
6377
  */
6332
6378
 
@@ -6346,6 +6392,7 @@ module.exports = function () {
6346
6392
  /**
6347
6393
  * Returns all of the enumeration's items (given an enumeration type).
6348
6394
  *
6395
+ * @public
6349
6396
  * @param {Function} type - The enumeration to list.
6350
6397
  * @returns {Array}
6351
6398
  */
@@ -7193,7 +7240,7 @@ module.exports = function () {
7193
7240
 
7194
7241
  if (typeof itemConstraint === 'function' && itemConstraint !== Function) {
7195
7242
  itemValidator = function itemValidator(value, index) {
7196
- return value instanceof itemConstraint || itemConstraint(value, variableName + '[' + index + ']');
7243
+ return itemConstraint.prototype !== undefined && value instanceof itemConstraint || itemConstraint(value, variableName + '[' + index + ']');
7197
7244
  };
7198
7245
  } else {
7199
7246
  itemValidator = function itemValidator(value, index) {
@@ -9317,6 +9364,30 @@ describe('When validating transaction order', () => {
9317
9364
  });
9318
9365
  });
9319
9366
 
9367
+ describe('When validating transaction references', () => {
9368
+ 'use strict';
9369
+
9370
+ const build = (root, sequence) => {
9371
+ return { reference: { root: root, sequence: sequence } };
9372
+ };
9373
+
9374
+ it('An array of zero transactions should be valid', () => {
9375
+ expect(TransactionValidator.validateReferences([])).toEqual(true);
9376
+ });
9377
+
9378
+ it('An array with no references should be valid', () => {
9379
+ expect(TransactionValidator.validateReferences([ { }, { } ])).toEqual(true);
9380
+ });
9381
+
9382
+ it('An array with distinct references should be valid', () => {
9383
+ expect(TransactionValidator.validateReferences([ build('a', 1), build('a', 2), build('b', 1) ])).toEqual(true);
9384
+ });
9385
+
9386
+ it('An array with non-distinct references should be not valid', () => {
9387
+ expect(TransactionValidator.validateReferences([ build('a', 1), build('a', 2), build('b', 1), build('a', 2) ])).toEqual(false);
9388
+ });
9389
+ });
9390
+
9320
9391
  describe('When requesting all the user-initiated transaction types', () => {
9321
9392
  'use strict';
9322
9393
 
@@ -9330,13 +9401,6 @@ describe('When requesting all the user-initiated transaction types', () => {
9330
9401
  expect(userInitiated.length).toEqual(9);
9331
9402
  });
9332
9403
  });
9333
-
9334
- describe('When validating direction', () => {
9335
- 'use strict';
9336
-
9337
-
9338
- });
9339
-
9340
9404
  },{"./../../../lib/data/TransactionValidator":5,"@barchart/common-js/lang/Day":18}],37:[function(require,module,exports){
9341
9405
  const Currency = require('@barchart/common-js/lang/Currency'),
9342
9406
  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
+ });