@barchart/portfolio-api-common 1.0.270 → 1.2.1

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,5 @@
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
3
 
4
4
  const InstrumentType = require('./InstrumentType'),
5
5
  PositionDirection = require('./PositionDirection'),
@@ -19,18 +19,31 @@ module.exports = (() => {
19
19
  }
20
20
 
21
21
  /**
22
- * Given a set of transaction, ensures that sequence numbers and dates
22
+ * Given a set of transaction, ensures that all sequence numbers and dates
23
23
  * are properly ordered.
24
24
  *
25
25
  * @public
26
26
  * @static
27
27
  * @param {Array.<Object>} transactions
28
- * @return {boolean}
28
+ * @return {Boolean}
29
29
  */
30
30
  static validateOrder(transactions) {
31
+ return TransactionValidator.getInvalidIndex(transactions) < 0;
32
+ }
33
+
34
+ /**
35
+ * Given a set of transaction, returns the index of the first transaction that with an invalid
36
+ * sequence number or date.
37
+ *
38
+ * @public
39
+ * @static
40
+ * @param {Array.<Object>} transactions
41
+ * @return {Number}
42
+ */
43
+ static getInvalidIndex(transactions) {
31
44
  assert.argumentIsArray(transactions, 'transactions');
32
45
 
33
- return transactions.every((t, i) => t.sequence === (i + 1) && (i === 0 || !t.date.getIsBefore(transactions[i - 1].date)));
46
+ return transactions.findIndex((t, i) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(transactions[i - 1].date)));
34
47
  }
35
48
 
36
49
  /**
@@ -410,7 +410,7 @@ module.exports = (() => {
410
410
  }
411
411
 
412
412
  Object.keys(this._trees).forEach((key) => {
413
- this._trees[key].walk(group => group.setForexRates(this._forexQuotes), true, false)
413
+ this._trees[key].walk(group => group.setForexRates(this._forexQuotes), true, false);
414
414
  });
415
415
 
416
416
  recalculatePercentages.call(this);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.270",
3
+ "version": "1.2.1",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -957,7 +957,7 @@ module.exports = (() => {
957
957
 
958
958
  },{"@barchart/common-js/lang/Enum":21,"@barchart/common-js/lang/assert":24}],5:[function(require,module,exports){
959
959
  const assert = require('@barchart/common-js/lang/assert'),
960
- array = require('@barchart/common-js/lang/array')
960
+ array = require('@barchart/common-js/lang/array');
961
961
 
962
962
  const InstrumentType = require('./InstrumentType'),
963
963
  PositionDirection = require('./PositionDirection'),
@@ -977,18 +977,31 @@ module.exports = (() => {
977
977
  }
978
978
 
979
979
  /**
980
- * Given a set of transaction, ensures that sequence numbers and dates
980
+ * Given a set of transaction, ensures that all sequence numbers and dates
981
981
  * are properly ordered.
982
982
  *
983
983
  * @public
984
984
  * @static
985
985
  * @param {Array.<Object>} transactions
986
- * @return {boolean}
986
+ * @return {Boolean}
987
987
  */
988
988
  static validateOrder(transactions) {
989
+ return TransactionValidator.getInvalidIndex(transactions) < 0;
990
+ }
991
+
992
+ /**
993
+ * Given a set of transaction, returns the index of the first transaction that with an invalid
994
+ * sequence number or date.
995
+ *
996
+ * @public
997
+ * @static
998
+ * @param {Array.<Object>} transactions
999
+ * @return {Number}
1000
+ */
1001
+ static getInvalidIndex(transactions) {
989
1002
  assert.argumentIsArray(transactions, 'transactions');
990
1003
 
991
- return transactions.every((t, i) => t.sequence === (i + 1) && (i === 0 || !t.date.getIsBefore(transactions[i - 1].date)));
1004
+ return transactions.findIndex((t, i) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(transactions[i - 1].date)));
992
1005
  }
993
1006
 
994
1007
  /**
@@ -1556,7 +1569,7 @@ module.exports = (() => {
1556
1569
  }
1557
1570
 
1558
1571
  Object.keys(this._trees).forEach((key) => {
1559
- this._trees[key].walk(group => group.setForexRates(this._forexQuotes), true, false)
1572
+ this._trees[key].walk(group => group.setForexRates(this._forexQuotes), true, false);
1560
1573
  });
1561
1574
 
1562
1575
  recalculatePercentages.call(this);
@@ -6493,8 +6506,8 @@ module.exports = function () {
6493
6506
  unique: function unique(a) {
6494
6507
  assert.argumentIsArray(a, 'a');
6495
6508
 
6496
- return a.filter(function (item, index, array) {
6497
- return array.indexOf(item) === index;
6509
+ return this.uniqueBy(a, function (item) {
6510
+ return item;
6498
6511
  });
6499
6512
  },
6500
6513
 
@@ -6505,7 +6518,7 @@ module.exports = function () {
6505
6518
  *
6506
6519
  * @static
6507
6520
  * @param {Array} a
6508
- * @param {Function} keySelector - The function, when applied to an item yields a unique key.
6521
+ * @param {Function} keySelector - A function that returns a unique key for an item.
6509
6522
  * @returns {Array}
6510
6523
  */
6511
6524
  uniqueBy: function uniqueBy(a, keySelector) {
@@ -6523,12 +6536,12 @@ module.exports = function () {
6523
6536
 
6524
6537
  /**
6525
6538
  * Splits array into groups and returns an object (where the properties have
6526
- * arrays). Unlike the indexBy function, there can be many items
6527
- * which share the same key.
6539
+ * arrays). Unlike the indexBy function, there can be many items which share
6540
+ * the same key.
6528
6541
  *
6529
6542
  * @static
6530
6543
  * @param {Array} a
6531
- * @param {Function} keySelector - The function, when applied to an item yields a key.
6544
+ * @param {Function} keySelector - A function that returns a unique key for an item.
6532
6545
  * @returns {Object}
6533
6546
  */
6534
6547
  groupBy: function groupBy(a, keySelector) {
@@ -6555,7 +6568,7 @@ module.exports = function () {
6555
6568
  *
6556
6569
  * @static
6557
6570
  * @param {Array} a
6558
- * @param {Function} keySelector - The function, when applied to an item yields a key.
6571
+ * @param {Function} keySelector - A function that returns a unique key for an item.
6559
6572
  * @returns {Array}
6560
6573
  */
6561
6574
  batchBy: function batchBy(a, keySelector) {
@@ -6584,12 +6597,11 @@ module.exports = function () {
6584
6597
 
6585
6598
  /**
6586
6599
  * Splits array into groups and returns an object (where the properties are items from the
6587
- * original array). Unlike the groupBy, Only one item can have a given key
6588
- * value.
6600
+ * original array). Unlike the groupBy, only one item can have a given key value.
6589
6601
  *
6590
6602
  * @static
6591
6603
  * @param {Array} a
6592
- * @param {Function} keySelector - The function, when applied to an item yields a unique key.
6604
+ * @param {Function} keySelector - A function that returns a unique key for an item.
6593
6605
  * @returns {Object}
6594
6606
  */
6595
6607
  indexBy: function indexBy(a, keySelector) {
@@ -6755,14 +6767,31 @@ module.exports = function () {
6755
6767
  * @returns {Array}
6756
6768
  */
6757
6769
  difference: function difference(a, b) {
6770
+ return this.differenceBy(a, b, function (item) {
6771
+ return item;
6772
+ });
6773
+ },
6774
+
6775
+
6776
+ /**
6777
+ * Set difference operation, where the uniqueness is determined by a delegate.
6778
+ *
6779
+ * @static
6780
+ * @param {Array} a
6781
+ * @param {Array} b
6782
+ * @param {Function} keySelector - A function that returns a unique key for an item.
6783
+ * @returns {Array}
6784
+ */
6785
+ differenceBy: function differenceBy(a, b, keySelector) {
6758
6786
  assert.argumentIsArray(a, 'a');
6759
6787
  assert.argumentIsArray(b, 'b');
6788
+ assert.argumentIsRequired(keySelector, 'keySelector', Function);
6760
6789
 
6761
6790
  var returnRef = [];
6762
6791
 
6763
6792
  a.forEach(function (candidate) {
6764
6793
  var exclude = b.some(function (comparison) {
6765
- return candidate === comparison;
6794
+ return keySelector(candidate) === keySelector(comparison);
6766
6795
  });
6767
6796
 
6768
6797
  if (!exclude) {
@@ -6785,7 +6814,23 @@ module.exports = function () {
6785
6814
  * @returns {Array}
6786
6815
  */
6787
6816
  differenceSymmetric: function differenceSymmetric(a, b) {
6788
- return this.union(this.difference(a, b), this.difference(b, a));
6817
+ return this.differenceSymmetricBy(a, b, function (item) {
6818
+ return item;
6819
+ });
6820
+ },
6821
+
6822
+
6823
+ /**
6824
+ * Set symmetric difference operation, where the uniqueness is determined by a delegate.
6825
+ *
6826
+ * @static
6827
+ * @param {Array} a
6828
+ * @param {Array} b
6829
+ * @param {Function} keySelector - A function that returns a unique key for an item.
6830
+ * @returns {Array}
6831
+ */
6832
+ differenceSymmetricBy: function differenceSymmetricBy(a, b, keySelector) {
6833
+ return this.unionBy(this.differenceBy(a, b, keySelector), this.differenceBy(b, a, keySelector), keySelector);
6789
6834
  },
6790
6835
 
6791
6836
 
@@ -6798,14 +6843,31 @@ module.exports = function () {
6798
6843
  * @returns {Array}
6799
6844
  */
6800
6845
  union: function union(a, b) {
6846
+ return this.unionBy(a, b, function (item) {
6847
+ return item;
6848
+ });
6849
+ },
6850
+
6851
+
6852
+ /**
6853
+ * Set union operation, where the uniqueness is determined by a delegate.
6854
+ *
6855
+ * @static
6856
+ * @param {Array} a
6857
+ * @param {Array} b
6858
+ * @param {Function} keySelector - A function that returns a unique key for an item.
6859
+ * @returns {Array}
6860
+ */
6861
+ unionBy: function unionBy(a, b, keySelector) {
6801
6862
  assert.argumentIsArray(a, 'a');
6802
6863
  assert.argumentIsArray(b, 'b');
6864
+ assert.argumentIsRequired(keySelector, 'keySelector', Function);
6803
6865
 
6804
6866
  var returnRef = a.slice();
6805
6867
 
6806
6868
  b.forEach(function (candidate) {
6807
6869
  var exclude = returnRef.some(function (comparison) {
6808
- return candidate === comparison;
6870
+ return keySelector(candidate) === keySelector(comparison);
6809
6871
  });
6810
6872
 
6811
6873
  if (!exclude) {
@@ -6826,6 +6888,22 @@ module.exports = function () {
6826
6888
  * @returns {Array}
6827
6889
  */
6828
6890
  intersection: function intersection(a, b) {
6891
+ return this.intersectionBy(a, b, function (item) {
6892
+ return item;
6893
+ });
6894
+ },
6895
+
6896
+
6897
+ /**
6898
+ * Set intersection operation, where the uniqueness is determined by a delegate.
6899
+ *
6900
+ * @static
6901
+ * @param {Array} a
6902
+ * @param {Array} b
6903
+ * @param {Function} keySelector - A function that returns a unique key for an item.
6904
+ * @returns {Array}
6905
+ */
6906
+ intersectionBy: function intersectionBy(a, b, keySelector) {
6829
6907
  assert.argumentIsArray(a, 'a');
6830
6908
  assert.argumentIsArray(b, 'b');
6831
6909
 
@@ -6833,7 +6911,7 @@ module.exports = function () {
6833
6911
 
6834
6912
  a.forEach(function (candidate) {
6835
6913
  var include = b.some(function (comparison) {
6836
- return candidate === comparison;
6914
+ return keySelector(candidate) === comparison;
6837
6915
  });
6838
6916
 
6839
6917
  if (include) {
@@ -6852,16 +6930,20 @@ module.exports = function () {
6852
6930
  * @public
6853
6931
  * @param {Array} a
6854
6932
  * @param {Function} predicate
6933
+ * @returns {Boolean}
6855
6934
  */
6856
6935
  remove: function remove(a, predicate) {
6857
6936
  assert.argumentIsArray(a, 'a');
6858
6937
  assert.argumentIsRequired(predicate, 'predicate', Function);
6859
6938
 
6860
6939
  var index = a.findIndex(predicate);
6940
+ var found = !(index < 0);
6861
6941
 
6862
- if (!(index < 0)) {
6942
+ if (found) {
6863
6943
  a.splice(index, 1);
6864
6944
  }
6945
+
6946
+ return found;
6865
6947
  }
6866
6948
  };
6867
6949
  }();