@barchart/portfolio-api-common 1.1.0 → 1.2.3

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
  /**
@@ -458,6 +458,9 @@ module.exports = (() => {
458
458
  if (this._single) {
459
459
  const precision = sender.position.instrument.currency.precision;
460
460
 
461
+ this._dataActual.invalid = this._definition.type === PositionLevelType.POSITION && item.invalid;
462
+ this._dataFormat.invalid = this._dataActual.invalid;
463
+
461
464
  this._dataActual.currentPrice = quote.lastPrice;
462
465
  this._dataFormat.currentPrice = formatNumber(this._dataActual.currentPrice, precision);
463
466
 
@@ -27,7 +27,11 @@ module.exports = (() => {
27
27
 
28
28
  this._portfolio = portfolio;
29
29
  this._position = position;
30
- this._currency = position.instrument.currency || Currency.CAD;
30
+
31
+ const instrument = position.instrument;
32
+
33
+ this._currency = instrument.currency || Currency.CAD;
34
+ this._invalid = instrument.type.usesSymbols && (!is.object(instrument.symbol) || !is.string(instrument.symbol.barchart));
31
35
 
32
36
  this._currentSummary = currentSummary || null;
33
37
  this._previousSummaries = previousSummaries || [ ];
@@ -103,6 +103,7 @@ module.exports = (() => {
103
103
  .withField('legacy.portfolio', DataType.STRING, true)
104
104
  .withField('legacy.position', DataType.STRING, true)
105
105
  .withField('system.version', DataType.NUMBER, true)
106
+ .withField('root', DataType.STRING, true)
106
107
  .schema
107
108
  );
108
109
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.1.0",
3
+ "version": "1.2.3",
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
  /**
@@ -2547,6 +2560,9 @@ module.exports = (() => {
2547
2560
  if (this._single) {
2548
2561
  const precision = sender.position.instrument.currency.precision;
2549
2562
 
2563
+ this._dataActual.invalid = this._definition.type === PositionLevelType.POSITION && item.invalid;
2564
+ this._dataFormat.invalid = this._dataActual.invalid;
2565
+
2550
2566
  this._dataActual.currentPrice = quote.lastPrice;
2551
2567
  this._dataFormat.currentPrice = formatNumber(this._dataActual.currentPrice, precision);
2552
2568
 
@@ -2975,7 +2991,11 @@ module.exports = (() => {
2975
2991
 
2976
2992
  this._portfolio = portfolio;
2977
2993
  this._position = position;
2978
- this._currency = position.instrument.currency || Currency.CAD;
2994
+
2995
+ const instrument = position.instrument;
2996
+
2997
+ this._currency = instrument.currency || Currency.CAD;
2998
+ this._invalid = instrument.type.usesSymbols && (!is.object(instrument.symbol) || !is.string(instrument.symbol.barchart));
2979
2999
 
2980
3000
  this._currentSummary = currentSummary || null;
2981
3001
  this._previousSummaries = previousSummaries || [ ];
@@ -3951,14 +3971,31 @@ module.exports = function () {
3951
3971
  }
3952
3972
 
3953
3973
  /**
3954
- * Returns the parent node. If this is the root node, a null value is returned.
3974
+ * Gets the root node.
3955
3975
  *
3956
3976
  * @public
3957
- * @returns {Tree|null}
3977
+ * @returns {Tree}
3958
3978
  */
3959
3979
 
3960
3980
 
3961
3981
  _createClass(Tree, [{
3982
+ key: 'getRoot',
3983
+ value: function getRoot() {
3984
+ if (this.getIsRoot()) {
3985
+ return this;
3986
+ } else {
3987
+ return this._parent.getRoot();
3988
+ }
3989
+ }
3990
+
3991
+ /**
3992
+ * Returns the parent node. If this is the root node, a null value is returned.
3993
+ *
3994
+ * @public
3995
+ * @returns {Tree|null}
3996
+ */
3997
+
3998
+ }, {
3962
3999
  key: 'getParent',
3963
4000
  value: function getParent() {
3964
4001
  return this._parent;
@@ -4059,6 +4096,23 @@ module.exports = function () {
4059
4096
  }
4060
4097
  }
4061
4098
 
4099
+ /**
4100
+ * Removes the current node from the parent tree. Use on a root node
4101
+ * has no effect.
4102
+ *
4103
+ * @public
4104
+ */
4105
+
4106
+ }, {
4107
+ key: 'sever',
4108
+ value: function sever() {
4109
+ if (this.getIsRoot()) {
4110
+ return;
4111
+ }
4112
+
4113
+ this.getParent().removeChild(this);
4114
+ }
4115
+
4062
4116
  /**
4063
4117
  * Searches the children nodes for the first child node that matches the
4064
4118
  * predicate.
@@ -4163,6 +4217,33 @@ module.exports = function () {
4163
4217
  }
4164
4218
  }
4165
4219
 
4220
+ /**
4221
+ * Climbs the tree, evaluating each parent until a predicate is matched. Once matched,
4222
+ * the {@link Tree} node is returned. Otherwise, if the predicate cannot be matched,
4223
+ * a null value is returned.
4224
+ *
4225
+ * @public
4226
+ * @param {Tree~nodePredicate} predicate - A predicate that tests each child node. The predicate takes two arguments -- the node's value, and the node itself.
4227
+ * @param {boolean=} includeCurrentNode - If true, the predicate will be applied to the current node.
4228
+ * @returns {Tree|null}
4229
+ */
4230
+
4231
+ }, {
4232
+ key: 'findParent',
4233
+ value: function findParent(predicate, includeCurrentNode) {
4234
+ var returnRef = void 0;
4235
+
4236
+ if (is.boolean(includeCurrentNode) && includeCurrentNode && predicate(this.getValue(), this)) {
4237
+ returnRef = this;
4238
+ } else if (this._parent !== null) {
4239
+ returnRef = this._parent.findParent(predicate, true);
4240
+ } else {
4241
+ returnRef = null;
4242
+ }
4243
+
4244
+ return returnRef;
4245
+ }
4246
+
4166
4247
  /**
4167
4248
  * Creates a representation of the tree using JavaScript objects and arrays.
4168
4249
  *
@@ -5453,6 +5534,20 @@ module.exports = function () {
5453
5534
  return this._big.gt(getBig(other));
5454
5535
  }
5455
5536
 
5537
+ /**
5538
+ * Returns true if the current instance is greater than or equal to the value.
5539
+ *
5540
+ * @public
5541
+ * @param {Decimal|Number|String} other - The value to compare.
5542
+ * @returns {Boolean}
5543
+ */
5544
+
5545
+ }, {
5546
+ key: 'getIsGreaterThanOrEqual',
5547
+ value: function getIsGreaterThanOrEqual(other) {
5548
+ return this._big.gte(getBig(other));
5549
+ }
5550
+
5456
5551
  /**
5457
5552
  * Returns true if the current instance is less than the value.
5458
5553
  *
@@ -5467,6 +5562,20 @@ module.exports = function () {
5467
5562
  return this._big.lt(getBig(other));
5468
5563
  }
5469
5564
 
5565
+ /**
5566
+ * Returns true if the current instance is less than or equal to the value.
5567
+ *
5568
+ * @public
5569
+ * @param {Decimal|Number|String} other - The value to compare.
5570
+ * @returns {Boolean}
5571
+ */
5572
+
5573
+ }, {
5574
+ key: 'getIsLessThanOrEqual',
5575
+ value: function getIsLessThanOrEqual(other) {
5576
+ return this._big.lte(getBig(other));
5577
+ }
5578
+
5470
5579
  /**
5471
5580
  * Returns true if the current instance is equal to the value.
5472
5581
  *
@@ -5666,9 +5775,9 @@ module.exports = function () {
5666
5775
  assert.argumentIsRequired(a, 'a', Decimal, 'Decimal');
5667
5776
  assert.argumentIsRequired(b, 'b', Decimal, 'Decimal');
5668
5777
 
5669
- if (a._big.gt(b)) {
5778
+ if (a._big.gt(b._big)) {
5670
5779
  return 1;
5671
- } else if (a._big.lt(b)) {
5780
+ } else if (a._big.lt(b._big)) {
5672
5781
  return -1;
5673
5782
  } else {
5674
5783
  return 0;
@@ -6129,12 +6238,10 @@ var _createClass = function () { function defineProperties(target, props) { for
6129
6238
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6130
6239
 
6131
6240
  var assert = require('./assert'),
6132
- is = require('./is'),
6133
6241
  memoize = require('./memoize');
6134
6242
 
6135
6243
  var Currency = require('./Currency'),
6136
- Decimal = require('./Decimal'),
6137
- Enum = require('./Enum');
6244
+ Decimal = require('./Decimal');
6138
6245
 
6139
6246
  module.exports = function () {
6140
6247
  'use strict';
@@ -6314,12 +6421,7 @@ module.exports = function () {
6314
6421
  assert.argumentIsRequired(amount, 'amount', Decimal, 'Decimal');
6315
6422
  assert.argumentIsRequired(currency, 'currency', Currency, 'Currency');
6316
6423
  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');
6424
+ //assert.argumentIsArray(rates, 'rates', Rate, 'Rate');
6323
6425
 
6324
6426
  var converted = void 0;
6325
6427
 
@@ -6329,6 +6431,10 @@ module.exports = function () {
6329
6431
  var numerator = desiredCurrency;
6330
6432
  var denominator = currency;
6331
6433
 
6434
+ for (var _len = arguments.length, rates = Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
6435
+ rates[_key - 3] = arguments[_key];
6436
+ }
6437
+
6332
6438
  var rate = rates.find(function (r) {
6333
6439
  return r.numerator === numerator && r.denominator === denominator || r.numerator === denominator && r.denominator === numerator;
6334
6440
  });
@@ -6379,7 +6485,7 @@ module.exports = function () {
6379
6485
  return Rate;
6380
6486
  }();
6381
6487
 
6382
- },{"./Currency":17,"./Decimal":19,"./Enum":21,"./assert":24,"./is":26,"./memoize":27}],23:[function(require,module,exports){
6488
+ },{"./Currency":17,"./Decimal":19,"./assert":24,"./memoize":27}],23:[function(require,module,exports){
6383
6489
  'use strict';
6384
6490
 
6385
6491
  var assert = require('./assert'),
@@ -6407,8 +6513,8 @@ module.exports = function () {
6407
6513
  unique: function unique(a) {
6408
6514
  assert.argumentIsArray(a, 'a');
6409
6515
 
6410
- return a.filter(function (item, index, array) {
6411
- return array.indexOf(item) === index;
6516
+ return this.uniqueBy(a, function (item) {
6517
+ return item;
6412
6518
  });
6413
6519
  },
6414
6520
 
@@ -6419,7 +6525,7 @@ module.exports = function () {
6419
6525
  *
6420
6526
  * @static
6421
6527
  * @param {Array} a
6422
- * @param {Function} keySelector - The function, when applied to an item yields a unique key.
6528
+ * @param {Function} keySelector - A function that returns a unique key for an item.
6423
6529
  * @returns {Array}
6424
6530
  */
6425
6531
  uniqueBy: function uniqueBy(a, keySelector) {
@@ -6437,12 +6543,12 @@ module.exports = function () {
6437
6543
 
6438
6544
  /**
6439
6545
  * 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.
6546
+ * arrays). Unlike the indexBy function, there can be many items which share
6547
+ * the same key.
6442
6548
  *
6443
6549
  * @static
6444
6550
  * @param {Array} a
6445
- * @param {Function} keySelector - The function, when applied to an item yields a key.
6551
+ * @param {Function} keySelector - A function that returns a unique key for an item.
6446
6552
  * @returns {Object}
6447
6553
  */
6448
6554
  groupBy: function groupBy(a, keySelector) {
@@ -6469,7 +6575,7 @@ module.exports = function () {
6469
6575
  *
6470
6576
  * @static
6471
6577
  * @param {Array} a
6472
- * @param {Function} keySelector - The function, when applied to an item yields a key.
6578
+ * @param {Function} keySelector - A function that returns a unique key for an item.
6473
6579
  * @returns {Array}
6474
6580
  */
6475
6581
  batchBy: function batchBy(a, keySelector) {
@@ -6498,12 +6604,11 @@ module.exports = function () {
6498
6604
 
6499
6605
  /**
6500
6606
  * 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.
6607
+ * original array). Unlike the groupBy, only one item can have a given key value.
6503
6608
  *
6504
6609
  * @static
6505
6610
  * @param {Array} a
6506
- * @param {Function} keySelector - The function, when applied to an item yields a unique key.
6611
+ * @param {Function} keySelector - A function that returns a unique key for an item.
6507
6612
  * @returns {Object}
6508
6613
  */
6509
6614
  indexBy: function indexBy(a, keySelector) {
@@ -6669,14 +6774,31 @@ module.exports = function () {
6669
6774
  * @returns {Array}
6670
6775
  */
6671
6776
  difference: function difference(a, b) {
6777
+ return this.differenceBy(a, b, function (item) {
6778
+ return item;
6779
+ });
6780
+ },
6781
+
6782
+
6783
+ /**
6784
+ * Set difference operation, where the uniqueness is determined by a delegate.
6785
+ *
6786
+ * @static
6787
+ * @param {Array} a
6788
+ * @param {Array} b
6789
+ * @param {Function} keySelector - A function that returns a unique key for an item.
6790
+ * @returns {Array}
6791
+ */
6792
+ differenceBy: function differenceBy(a, b, keySelector) {
6672
6793
  assert.argumentIsArray(a, 'a');
6673
6794
  assert.argumentIsArray(b, 'b');
6795
+ assert.argumentIsRequired(keySelector, 'keySelector', Function);
6674
6796
 
6675
6797
  var returnRef = [];
6676
6798
 
6677
6799
  a.forEach(function (candidate) {
6678
6800
  var exclude = b.some(function (comparison) {
6679
- return candidate === comparison;
6801
+ return keySelector(candidate) === keySelector(comparison);
6680
6802
  });
6681
6803
 
6682
6804
  if (!exclude) {
@@ -6699,7 +6821,23 @@ module.exports = function () {
6699
6821
  * @returns {Array}
6700
6822
  */
6701
6823
  differenceSymmetric: function differenceSymmetric(a, b) {
6702
- return this.union(this.difference(a, b), this.difference(b, a));
6824
+ return this.differenceSymmetricBy(a, b, function (item) {
6825
+ return item;
6826
+ });
6827
+ },
6828
+
6829
+
6830
+ /**
6831
+ * Set symmetric difference operation, where the uniqueness is determined by a delegate.
6832
+ *
6833
+ * @static
6834
+ * @param {Array} a
6835
+ * @param {Array} b
6836
+ * @param {Function} keySelector - A function that returns a unique key for an item.
6837
+ * @returns {Array}
6838
+ */
6839
+ differenceSymmetricBy: function differenceSymmetricBy(a, b, keySelector) {
6840
+ return this.unionBy(this.differenceBy(a, b, keySelector), this.differenceBy(b, a, keySelector), keySelector);
6703
6841
  },
6704
6842
 
6705
6843
 
@@ -6712,14 +6850,31 @@ module.exports = function () {
6712
6850
  * @returns {Array}
6713
6851
  */
6714
6852
  union: function union(a, b) {
6853
+ return this.unionBy(a, b, function (item) {
6854
+ return item;
6855
+ });
6856
+ },
6857
+
6858
+
6859
+ /**
6860
+ * Set union operation, where the uniqueness is determined by a delegate.
6861
+ *
6862
+ * @static
6863
+ * @param {Array} a
6864
+ * @param {Array} b
6865
+ * @param {Function} keySelector - A function that returns a unique key for an item.
6866
+ * @returns {Array}
6867
+ */
6868
+ unionBy: function unionBy(a, b, keySelector) {
6715
6869
  assert.argumentIsArray(a, 'a');
6716
6870
  assert.argumentIsArray(b, 'b');
6871
+ assert.argumentIsRequired(keySelector, 'keySelector', Function);
6717
6872
 
6718
6873
  var returnRef = a.slice();
6719
6874
 
6720
6875
  b.forEach(function (candidate) {
6721
6876
  var exclude = returnRef.some(function (comparison) {
6722
- return candidate === comparison;
6877
+ return keySelector(candidate) === keySelector(comparison);
6723
6878
  });
6724
6879
 
6725
6880
  if (!exclude) {
@@ -6740,6 +6895,22 @@ module.exports = function () {
6740
6895
  * @returns {Array}
6741
6896
  */
6742
6897
  intersection: function intersection(a, b) {
6898
+ return this.intersectionBy(a, b, function (item) {
6899
+ return item;
6900
+ });
6901
+ },
6902
+
6903
+
6904
+ /**
6905
+ * Set intersection operation, where the uniqueness is determined by a delegate.
6906
+ *
6907
+ * @static
6908
+ * @param {Array} a
6909
+ * @param {Array} b
6910
+ * @param {Function} keySelector - A function that returns a unique key for an item.
6911
+ * @returns {Array}
6912
+ */
6913
+ intersectionBy: function intersectionBy(a, b, keySelector) {
6743
6914
  assert.argumentIsArray(a, 'a');
6744
6915
  assert.argumentIsArray(b, 'b');
6745
6916
 
@@ -6747,7 +6918,7 @@ module.exports = function () {
6747
6918
 
6748
6919
  a.forEach(function (candidate) {
6749
6920
  var include = b.some(function (comparison) {
6750
- return candidate === comparison;
6921
+ return keySelector(candidate) === comparison;
6751
6922
  });
6752
6923
 
6753
6924
  if (include) {
@@ -6756,6 +6927,30 @@ module.exports = function () {
6756
6927
  });
6757
6928
 
6758
6929
  return returnRef;
6930
+ },
6931
+
6932
+
6933
+ /**
6934
+ * Removes the first item from an array which matches a predicate.
6935
+ *
6936
+ * @static
6937
+ * @public
6938
+ * @param {Array} a
6939
+ * @param {Function} predicate
6940
+ * @returns {Boolean}
6941
+ */
6942
+ remove: function remove(a, predicate) {
6943
+ assert.argumentIsArray(a, 'a');
6944
+ assert.argumentIsRequired(predicate, 'predicate', Function);
6945
+
6946
+ var index = a.findIndex(predicate);
6947
+ var found = !(index < 0);
6948
+
6949
+ if (found) {
6950
+ a.splice(index, 1);
6951
+ }
6952
+
6953
+ return found;
6759
6954
  }
6760
6955
  };
6761
6956
  }();