@barchart/portfolio-api-common 1.2.0 → 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.
@@ -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
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -1,4 +1,4 @@
1
- (function(){function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}return e})()({1:[function(require,module,exports){
1
+ (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
2
2
  const uuid = require('uuid');
3
3
 
4
4
  const assert = require('@barchart/common-js/lang/assert'),
@@ -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
  /**
@@ -3951,14 +3964,31 @@ module.exports = function () {
3951
3964
  }
3952
3965
 
3953
3966
  /**
3954
- * Returns the parent node. If this is the root node, a null value is returned.
3967
+ * Gets the root node.
3955
3968
  *
3956
3969
  * @public
3957
- * @returns {Tree|null}
3970
+ * @returns {Tree}
3958
3971
  */
3959
3972
 
3960
3973
 
3961
3974
  _createClass(Tree, [{
3975
+ key: 'getRoot',
3976
+ value: function getRoot() {
3977
+ if (this.getIsRoot()) {
3978
+ return this;
3979
+ } else {
3980
+ return this._parent.getRoot();
3981
+ }
3982
+ }
3983
+
3984
+ /**
3985
+ * Returns the parent node. If this is the root node, a null value is returned.
3986
+ *
3987
+ * @public
3988
+ * @returns {Tree|null}
3989
+ */
3990
+
3991
+ }, {
3962
3992
  key: 'getParent',
3963
3993
  value: function getParent() {
3964
3994
  return this._parent;
@@ -4059,6 +4089,23 @@ module.exports = function () {
4059
4089
  }
4060
4090
  }
4061
4091
 
4092
+ /**
4093
+ * Removes the current node from the parent tree. Use on a root node
4094
+ * has no effect.
4095
+ *
4096
+ * @public
4097
+ */
4098
+
4099
+ }, {
4100
+ key: 'sever',
4101
+ value: function sever() {
4102
+ if (this.getIsRoot()) {
4103
+ return;
4104
+ }
4105
+
4106
+ this.getParent().removeChild(this);
4107
+ }
4108
+
4062
4109
  /**
4063
4110
  * Searches the children nodes for the first child node that matches the
4064
4111
  * predicate.
@@ -4163,6 +4210,33 @@ module.exports = function () {
4163
4210
  }
4164
4211
  }
4165
4212
 
4213
+ /**
4214
+ * Climbs the tree, evaluating each parent until a predicate is matched. Once matched,
4215
+ * the {@link Tree} node is returned. Otherwise, if the predicate cannot be matched,
4216
+ * a null value is returned.
4217
+ *
4218
+ * @public
4219
+ * @param {Tree~nodePredicate} predicate - A predicate that tests each child node. The predicate takes two arguments -- the node's value, and the node itself.
4220
+ * @param {boolean=} includeCurrentNode - If true, the predicate will be applied to the current node.
4221
+ * @returns {Tree|null}
4222
+ */
4223
+
4224
+ }, {
4225
+ key: 'findParent',
4226
+ value: function findParent(predicate, includeCurrentNode) {
4227
+ var returnRef = void 0;
4228
+
4229
+ if (is.boolean(includeCurrentNode) && includeCurrentNode && predicate(this.getValue(), this)) {
4230
+ returnRef = this;
4231
+ } else if (this._parent !== null) {
4232
+ returnRef = this._parent.findParent(predicate, true);
4233
+ } else {
4234
+ returnRef = null;
4235
+ }
4236
+
4237
+ return returnRef;
4238
+ }
4239
+
4166
4240
  /**
4167
4241
  * Creates a representation of the tree using JavaScript objects and arrays.
4168
4242
  *
@@ -5453,6 +5527,20 @@ module.exports = function () {
5453
5527
  return this._big.gt(getBig(other));
5454
5528
  }
5455
5529
 
5530
+ /**
5531
+ * Returns true if the current instance is greater than or equal to the value.
5532
+ *
5533
+ * @public
5534
+ * @param {Decimal|Number|String} other - The value to compare.
5535
+ * @returns {Boolean}
5536
+ */
5537
+
5538
+ }, {
5539
+ key: 'getIsGreaterThanOrEqual',
5540
+ value: function getIsGreaterThanOrEqual(other) {
5541
+ return this._big.gte(getBig(other));
5542
+ }
5543
+
5456
5544
  /**
5457
5545
  * Returns true if the current instance is less than the value.
5458
5546
  *
@@ -5467,6 +5555,20 @@ module.exports = function () {
5467
5555
  return this._big.lt(getBig(other));
5468
5556
  }
5469
5557
 
5558
+ /**
5559
+ * Returns true if the current instance is less than or equal to the value.
5560
+ *
5561
+ * @public
5562
+ * @param {Decimal|Number|String} other - The value to compare.
5563
+ * @returns {Boolean}
5564
+ */
5565
+
5566
+ }, {
5567
+ key: 'getIsLessThanOrEqual',
5568
+ value: function getIsLessThanOrEqual(other) {
5569
+ return this._big.lte(getBig(other));
5570
+ }
5571
+
5470
5572
  /**
5471
5573
  * Returns true if the current instance is equal to the value.
5472
5574
  *
@@ -5666,9 +5768,9 @@ module.exports = function () {
5666
5768
  assert.argumentIsRequired(a, 'a', Decimal, 'Decimal');
5667
5769
  assert.argumentIsRequired(b, 'b', Decimal, 'Decimal');
5668
5770
 
5669
- if (a._big.gt(b)) {
5771
+ if (a._big.gt(b._big)) {
5670
5772
  return 1;
5671
- } else if (a._big.lt(b)) {
5773
+ } else if (a._big.lt(b._big)) {
5672
5774
  return -1;
5673
5775
  } else {
5674
5776
  return 0;
@@ -6129,12 +6231,10 @@ var _createClass = function () { function defineProperties(target, props) { for
6129
6231
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6130
6232
 
6131
6233
  var assert = require('./assert'),
6132
- is = require('./is'),
6133
6234
  memoize = require('./memoize');
6134
6235
 
6135
6236
  var Currency = require('./Currency'),
6136
- Decimal = require('./Decimal'),
6137
- Enum = require('./Enum');
6237
+ Decimal = require('./Decimal');
6138
6238
 
6139
6239
  module.exports = function () {
6140
6240
  'use strict';
@@ -6314,12 +6414,7 @@ module.exports = function () {
6314
6414
  assert.argumentIsRequired(amount, 'amount', Decimal, 'Decimal');
6315
6415
  assert.argumentIsRequired(currency, 'currency', Currency, 'Currency');
6316
6416
  assert.argumentIsRequired(desiredCurrency, 'desiredCurrency', Currency, 'Currency');
6317
-
6318
- for (var _len = arguments.length, rates = Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
6319
- rates[_key - 3] = arguments[_key];
6320
- }
6321
-
6322
- assert.argumentIsArray(rates, 'rates', Rate, 'Rate');
6417
+ //assert.argumentIsArray(rates, 'rates', Rate, 'Rate');
6323
6418
 
6324
6419
  var converted = void 0;
6325
6420
 
@@ -6329,6 +6424,10 @@ module.exports = function () {
6329
6424
  var numerator = desiredCurrency;
6330
6425
  var denominator = currency;
6331
6426
 
6427
+ for (var _len = arguments.length, rates = Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
6428
+ rates[_key - 3] = arguments[_key];
6429
+ }
6430
+
6332
6431
  var rate = rates.find(function (r) {
6333
6432
  return r.numerator === numerator && r.denominator === denominator || r.numerator === denominator && r.denominator === numerator;
6334
6433
  });
@@ -6379,7 +6478,7 @@ module.exports = function () {
6379
6478
  return Rate;
6380
6479
  }();
6381
6480
 
6382
- },{"./Currency":17,"./Decimal":19,"./Enum":21,"./assert":24,"./is":26,"./memoize":27}],23:[function(require,module,exports){
6481
+ },{"./Currency":17,"./Decimal":19,"./assert":24,"./memoize":27}],23:[function(require,module,exports){
6383
6482
  'use strict';
6384
6483
 
6385
6484
  var assert = require('./assert'),
@@ -6407,8 +6506,8 @@ module.exports = function () {
6407
6506
  unique: function unique(a) {
6408
6507
  assert.argumentIsArray(a, 'a');
6409
6508
 
6410
- return a.filter(function (item, index, array) {
6411
- return array.indexOf(item) === index;
6509
+ return this.uniqueBy(a, function (item) {
6510
+ return item;
6412
6511
  });
6413
6512
  },
6414
6513
 
@@ -6419,7 +6518,7 @@ module.exports = function () {
6419
6518
  *
6420
6519
  * @static
6421
6520
  * @param {Array} a
6422
- * @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.
6423
6522
  * @returns {Array}
6424
6523
  */
6425
6524
  uniqueBy: function uniqueBy(a, keySelector) {
@@ -6437,12 +6536,12 @@ module.exports = function () {
6437
6536
 
6438
6537
  /**
6439
6538
  * Splits array into groups and returns an object (where the properties have
6440
- * arrays). Unlike the indexBy function, there can be many items
6441
- * which share the same key.
6539
+ * arrays). Unlike the indexBy function, there can be many items which share
6540
+ * the same key.
6442
6541
  *
6443
6542
  * @static
6444
6543
  * @param {Array} a
6445
- * @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.
6446
6545
  * @returns {Object}
6447
6546
  */
6448
6547
  groupBy: function groupBy(a, keySelector) {
@@ -6469,7 +6568,7 @@ module.exports = function () {
6469
6568
  *
6470
6569
  * @static
6471
6570
  * @param {Array} a
6472
- * @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.
6473
6572
  * @returns {Array}
6474
6573
  */
6475
6574
  batchBy: function batchBy(a, keySelector) {
@@ -6498,12 +6597,11 @@ module.exports = function () {
6498
6597
 
6499
6598
  /**
6500
6599
  * Splits array into groups and returns an object (where the properties are items from the
6501
- * original array). Unlike the groupBy, Only one item can have a given key
6502
- * value.
6600
+ * original array). Unlike the groupBy, only one item can have a given key value.
6503
6601
  *
6504
6602
  * @static
6505
6603
  * @param {Array} a
6506
- * @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.
6507
6605
  * @returns {Object}
6508
6606
  */
6509
6607
  indexBy: function indexBy(a, keySelector) {
@@ -6669,14 +6767,31 @@ module.exports = function () {
6669
6767
  * @returns {Array}
6670
6768
  */
6671
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) {
6672
6786
  assert.argumentIsArray(a, 'a');
6673
6787
  assert.argumentIsArray(b, 'b');
6788
+ assert.argumentIsRequired(keySelector, 'keySelector', Function);
6674
6789
 
6675
6790
  var returnRef = [];
6676
6791
 
6677
6792
  a.forEach(function (candidate) {
6678
6793
  var exclude = b.some(function (comparison) {
6679
- return candidate === comparison;
6794
+ return keySelector(candidate) === keySelector(comparison);
6680
6795
  });
6681
6796
 
6682
6797
  if (!exclude) {
@@ -6699,7 +6814,23 @@ module.exports = function () {
6699
6814
  * @returns {Array}
6700
6815
  */
6701
6816
  differenceSymmetric: function differenceSymmetric(a, b) {
6702
- 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);
6703
6834
  },
6704
6835
 
6705
6836
 
@@ -6712,14 +6843,31 @@ module.exports = function () {
6712
6843
  * @returns {Array}
6713
6844
  */
6714
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) {
6715
6862
  assert.argumentIsArray(a, 'a');
6716
6863
  assert.argumentIsArray(b, 'b');
6864
+ assert.argumentIsRequired(keySelector, 'keySelector', Function);
6717
6865
 
6718
6866
  var returnRef = a.slice();
6719
6867
 
6720
6868
  b.forEach(function (candidate) {
6721
6869
  var exclude = returnRef.some(function (comparison) {
6722
- return candidate === comparison;
6870
+ return keySelector(candidate) === keySelector(comparison);
6723
6871
  });
6724
6872
 
6725
6873
  if (!exclude) {
@@ -6740,6 +6888,22 @@ module.exports = function () {
6740
6888
  * @returns {Array}
6741
6889
  */
6742
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) {
6743
6907
  assert.argumentIsArray(a, 'a');
6744
6908
  assert.argumentIsArray(b, 'b');
6745
6909
 
@@ -6747,7 +6911,7 @@ module.exports = function () {
6747
6911
 
6748
6912
  a.forEach(function (candidate) {
6749
6913
  var include = b.some(function (comparison) {
6750
- return candidate === comparison;
6914
+ return keySelector(candidate) === comparison;
6751
6915
  });
6752
6916
 
6753
6917
  if (include) {
@@ -6756,6 +6920,30 @@ module.exports = function () {
6756
6920
  });
6757
6921
 
6758
6922
  return returnRef;
6923
+ },
6924
+
6925
+
6926
+ /**
6927
+ * Removes the first item from an array which matches a predicate.
6928
+ *
6929
+ * @static
6930
+ * @public
6931
+ * @param {Array} a
6932
+ * @param {Function} predicate
6933
+ * @returns {Boolean}
6934
+ */
6935
+ remove: function remove(a, predicate) {
6936
+ assert.argumentIsArray(a, 'a');
6937
+ assert.argumentIsRequired(predicate, 'predicate', Function);
6938
+
6939
+ var index = a.findIndex(predicate);
6940
+ var found = !(index < 0);
6941
+
6942
+ if (found) {
6943
+ a.splice(index, 1);
6944
+ }
6945
+
6946
+ return found;
6759
6947
  }
6760
6948
  };
6761
6949
  }();