@barchart/portfolio-api-common 1.2.41 → 1.2.42

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.
@@ -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'),
@@ -31,6 +32,44 @@ module.exports = (() => {
31
32
  return TransactionValidator.getInvalidIndex(transactions) < 0;
32
33
  }
33
34
 
35
+ /**
36
+ * Given a set of transaction, 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
+
34
73
  /**
35
74
  * Given a set of transaction, returns the index of the first transaction that with an invalid
36
75
  * sequence number or date.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.41",
3
+ "version": "1.2.42",
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'),
@@ -1059,6 +1060,44 @@ module.exports = (() => {
1059
1060
  return TransactionValidator.getInvalidIndex(transactions) < 0;
1060
1061
  }
1061
1062
 
1063
+ /**
1064
+ * Given a set of transaction, 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
+
1062
1101
  /**
1063
1102
  * Given a set of transaction, returns the index of the first transaction that with an invalid
1064
1103
  * sequence number or date.
@@ -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
+ });