@barchart/portfolio-api-common 1.0.204 → 1.0.205

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,3 +1,5 @@
1
+ const uuid = require('uuid');
2
+
1
3
  const assert = require('@barchart/common-js/lang/assert'),
2
4
  Enum = require('@barchart/common-js/lang/Enum');
3
5
 
@@ -16,16 +18,19 @@ module.exports = (() => {
16
18
  * @param {Boolean} usesSymbols
17
19
  */
18
20
  class InstrumentType extends Enum {
19
- constructor(code, description, alternateDescription, canReinvest, usesSymbols) {
21
+ constructor(code, description, alternateDescription, canReinvest, usesSymbols, generator) {
20
22
  super(code, description);
21
23
 
22
24
  assert.argumentIsRequired(alternateDescription, 'alternateDescription', String);
23
25
  assert.argumentIsRequired(canReinvest, 'canReinvest', Boolean);
24
26
  assert.argumentIsRequired(usesSymbols, 'usesSymbols', Boolean);
27
+ assert.argumentIsRequired(generator, 'generator', Function);
25
28
 
26
29
  this._alternateDescription = alternateDescription;
27
30
  this._canReinvest = canReinvest;
28
31
  this._usesSymbols = usesSymbols;
32
+
33
+ this._generator = generator;
29
34
  }
30
35
 
31
36
  /**
@@ -58,6 +63,23 @@ module.exports = (() => {
58
63
  return this._usesSymbols;
59
64
  }
60
65
 
66
+ /**
67
+ * Generates an identifier for the instrument.
68
+ *
69
+ * @public
70
+ * @param instrument
71
+ * @returns {String}
72
+ */
73
+ generateIdentifier(instrument) {
74
+ assert.argumentIsRequired(instrument, 'instrument');
75
+
76
+ if (instrument.type !== this) {
77
+ throw new Error('Unable to generate instrument identifier for incompatible type.');
78
+ }
79
+
80
+ return this._generator(instrument);
81
+ }
82
+
61
83
  /**
62
84
  * Cash.
63
85
  *
@@ -102,15 +124,34 @@ module.exports = (() => {
102
124
  return other;
103
125
  }
104
126
 
127
+ /**
128
+ * Generates an identifier for the instrument.
129
+ *
130
+ * @static
131
+ * @public
132
+ * @param instrument
133
+ * @returns {String}
134
+ */
135
+ static generateIdentifier(instrument) {
136
+ return map[instrument.type.code].generateIdentifier(instrument);
137
+ }
138
+
105
139
  toString() {
106
140
  return '[InstrumentType]';
107
141
  }
108
142
  }
109
143
 
110
- const cash = new InstrumentType('CASH', 'cash', 'Cash', false, false);
111
- const equity = new InstrumentType('EQUITY', 'equity', 'Equities', true, true);
112
- const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', true, true);
113
- const other = new InstrumentType('OTHER', 'other', 'Other', false, false);
144
+ const cash = new InstrumentType('CASH', 'cash', 'Cash', false, false, (instrument) => `BARCHART-${instrument.type.code}-${instrument.currency.code}`);
145
+ const equity = new InstrumentType('EQUITY', 'equity', 'Equities', true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
146
+ const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
147
+ const other = new InstrumentType('OTHER', 'other', 'Other', false, false, (instrument) => `BARCHART-${instrument.type.code}-${uuid.v4()}`);
148
+
149
+ const map = { };
150
+
151
+ map[cash.code] = cash;
152
+ map[equity.code] = equity;
153
+ map[fund.code] = fund;
154
+ map[other.code] = other;
114
155
 
115
156
  return InstrumentType;
116
157
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.204",
3
+ "version": "1.0.205",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -9,7 +9,8 @@
9
9
  },
10
10
  "scripts": {},
11
11
  "dependencies": {
12
- "@barchart/common-js": "~3.2.0"
12
+ "@barchart/common-js": "~3.2.0",
13
+ "uuid": "3.1.0"
13
14
  },
14
15
  "devDependencies": {
15
16
  "babel-core": "^6.26.0",
@@ -1,4 +1,6 @@
1
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
+ const uuid = require('uuid');
3
+
2
4
  const assert = require('@barchart/common-js/lang/assert'),
3
5
  Enum = require('@barchart/common-js/lang/Enum');
4
6
 
@@ -17,16 +19,19 @@ module.exports = (() => {
17
19
  * @param {Boolean} usesSymbols
18
20
  */
19
21
  class InstrumentType extends Enum {
20
- constructor(code, description, alternateDescription, canReinvest, usesSymbols) {
22
+ constructor(code, description, alternateDescription, canReinvest, usesSymbols, generator) {
21
23
  super(code, description);
22
24
 
23
25
  assert.argumentIsRequired(alternateDescription, 'alternateDescription', String);
24
26
  assert.argumentIsRequired(canReinvest, 'canReinvest', Boolean);
25
27
  assert.argumentIsRequired(usesSymbols, 'usesSymbols', Boolean);
28
+ assert.argumentIsRequired(generator, 'generator', Function);
26
29
 
27
30
  this._alternateDescription = alternateDescription;
28
31
  this._canReinvest = canReinvest;
29
32
  this._usesSymbols = usesSymbols;
33
+
34
+ this._generator = generator;
30
35
  }
31
36
 
32
37
  /**
@@ -59,6 +64,23 @@ module.exports = (() => {
59
64
  return this._usesSymbols;
60
65
  }
61
66
 
67
+ /**
68
+ * Generates an identifier for the instrument.
69
+ *
70
+ * @public
71
+ * @param instrument
72
+ * @returns {String}
73
+ */
74
+ generateIdentifier(instrument) {
75
+ assert.argumentIsRequired(instrument, 'instrument');
76
+
77
+ if (instrument.type !== this) {
78
+ throw new Error('Unable to generate instrument identifier for incompatible type.');
79
+ }
80
+
81
+ return this._generator(instrument);
82
+ }
83
+
62
84
  /**
63
85
  * Cash.
64
86
  *
@@ -103,20 +125,39 @@ module.exports = (() => {
103
125
  return other;
104
126
  }
105
127
 
128
+ /**
129
+ * Generates an identifier for the instrument.
130
+ *
131
+ * @static
132
+ * @public
133
+ * @param instrument
134
+ * @returns {String}
135
+ */
136
+ static generateIdentifier(instrument) {
137
+ return map[instrument.type.code].generateIdentifier(instrument);
138
+ }
139
+
106
140
  toString() {
107
141
  return '[InstrumentType]';
108
142
  }
109
143
  }
110
144
 
111
- const cash = new InstrumentType('CASH', 'cash', 'Cash', false, false);
112
- const equity = new InstrumentType('EQUITY', 'equity', 'Equities', true, true);
113
- const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', true, true);
114
- const other = new InstrumentType('OTHER', 'other', 'Other', false, false);
145
+ const cash = new InstrumentType('CASH', 'cash', 'Cash', false, false, (instrument) => `BARCHART-${instrument.type.code}-${instrument.currency.code}`);
146
+ const equity = new InstrumentType('EQUITY', 'equity', 'Equities', true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
147
+ const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
148
+ const other = new InstrumentType('OTHER', 'other', 'Other', false, false, (instrument) => `BARCHART-${instrument.type.code}-${uuid.v4()}`);
149
+
150
+ const map = { };
151
+
152
+ map[cash.code] = cash;
153
+ map[equity.code] = equity;
154
+ map[fund.code] = fund;
155
+ map[other.code] = other;
115
156
 
116
157
  return InstrumentType;
117
158
  })();
118
159
 
119
- },{"@barchart/common-js/lang/Enum":19,"@barchart/common-js/lang/assert":22}],2:[function(require,module,exports){
160
+ },{"@barchart/common-js/lang/Enum":19,"@barchart/common-js/lang/assert":22,"uuid":28}],2:[function(require,module,exports){
120
161
  const array = require('@barchart/common-js/lang/array'),
121
162
  assert = require('@barchart/common-js/lang/assert'),
122
163
  Day = require('@barchart/common-js/lang/Day'),
@@ -3350,14 +3391,31 @@ module.exports = function () {
3350
3391
  }
3351
3392
 
3352
3393
  /**
3353
- * Returns the parent node. If this is the root node, a null value is returned.
3394
+ * Gets the root node.
3354
3395
  *
3355
3396
  * @public
3356
- * @returns {Tree|null}
3397
+ * @returns {Tree}
3357
3398
  */
3358
3399
 
3359
3400
 
3360
3401
  _createClass(Tree, [{
3402
+ key: 'getRoot',
3403
+ value: function getRoot() {
3404
+ if (this.getIsRoot()) {
3405
+ return this;
3406
+ } else {
3407
+ return this._parent.getRoot();
3408
+ }
3409
+ }
3410
+
3411
+ /**
3412
+ * Returns the parent node. If this is the root node, a null value is returned.
3413
+ *
3414
+ * @public
3415
+ * @returns {Tree|null}
3416
+ */
3417
+
3418
+ }, {
3361
3419
  key: 'getParent',
3362
3420
  value: function getParent() {
3363
3421
  return this._parent;
@@ -7844,6 +7902,211 @@ module.exports = function () {
7844
7902
  })(this);
7845
7903
 
7846
7904
  },{}],28:[function(require,module,exports){
7905
+ var v1 = require('./v1');
7906
+ var v4 = require('./v4');
7907
+
7908
+ var uuid = v4;
7909
+ uuid.v1 = v1;
7910
+ uuid.v4 = v4;
7911
+
7912
+ module.exports = uuid;
7913
+
7914
+ },{"./v1":31,"./v4":32}],29:[function(require,module,exports){
7915
+ /**
7916
+ * Convert array of 16 byte values to UUID string format of the form:
7917
+ * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
7918
+ */
7919
+ var byteToHex = [];
7920
+ for (var i = 0; i < 256; ++i) {
7921
+ byteToHex[i] = (i + 0x100).toString(16).substr(1);
7922
+ }
7923
+
7924
+ function bytesToUuid(buf, offset) {
7925
+ var i = offset || 0;
7926
+ var bth = byteToHex;
7927
+ return bth[buf[i++]] + bth[buf[i++]] +
7928
+ bth[buf[i++]] + bth[buf[i++]] + '-' +
7929
+ bth[buf[i++]] + bth[buf[i++]] + '-' +
7930
+ bth[buf[i++]] + bth[buf[i++]] + '-' +
7931
+ bth[buf[i++]] + bth[buf[i++]] + '-' +
7932
+ bth[buf[i++]] + bth[buf[i++]] +
7933
+ bth[buf[i++]] + bth[buf[i++]] +
7934
+ bth[buf[i++]] + bth[buf[i++]];
7935
+ }
7936
+
7937
+ module.exports = bytesToUuid;
7938
+
7939
+ },{}],30:[function(require,module,exports){
7940
+ (function (global){
7941
+ // Unique ID creation requires a high quality random # generator. In the
7942
+ // browser this is a little complicated due to unknown quality of Math.random()
7943
+ // and inconsistent support for the `crypto` API. We do the best we can via
7944
+ // feature-detection
7945
+ var rng;
7946
+
7947
+ var crypto = global.crypto || global.msCrypto; // for IE 11
7948
+ if (crypto && crypto.getRandomValues) {
7949
+ // WHATWG crypto RNG - http://wiki.whatwg.org/wiki/Crypto
7950
+ var rnds8 = new Uint8Array(16); // eslint-disable-line no-undef
7951
+ rng = function whatwgRNG() {
7952
+ crypto.getRandomValues(rnds8);
7953
+ return rnds8;
7954
+ };
7955
+ }
7956
+
7957
+ if (!rng) {
7958
+ // Math.random()-based (RNG)
7959
+ //
7960
+ // If all else fails, use Math.random(). It's fast, but is of unspecified
7961
+ // quality.
7962
+ var rnds = new Array(16);
7963
+ rng = function() {
7964
+ for (var i = 0, r; i < 16; i++) {
7965
+ if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
7966
+ rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
7967
+ }
7968
+
7969
+ return rnds;
7970
+ };
7971
+ }
7972
+
7973
+ module.exports = rng;
7974
+
7975
+ }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
7976
+ },{}],31:[function(require,module,exports){
7977
+ var rng = require('./lib/rng');
7978
+ var bytesToUuid = require('./lib/bytesToUuid');
7979
+
7980
+ // **`v1()` - Generate time-based UUID**
7981
+ //
7982
+ // Inspired by https://github.com/LiosK/UUID.js
7983
+ // and http://docs.python.org/library/uuid.html
7984
+
7985
+ // random #'s we need to init node and clockseq
7986
+ var _seedBytes = rng();
7987
+
7988
+ // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
7989
+ var _nodeId = [
7990
+ _seedBytes[0] | 0x01,
7991
+ _seedBytes[1], _seedBytes[2], _seedBytes[3], _seedBytes[4], _seedBytes[5]
7992
+ ];
7993
+
7994
+ // Per 4.2.2, randomize (14 bit) clockseq
7995
+ var _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff;
7996
+
7997
+ // Previous uuid creation time
7998
+ var _lastMSecs = 0, _lastNSecs = 0;
7999
+
8000
+ // See https://github.com/broofa/node-uuid for API details
8001
+ function v1(options, buf, offset) {
8002
+ var i = buf && offset || 0;
8003
+ var b = buf || [];
8004
+
8005
+ options = options || {};
8006
+
8007
+ var clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq;
8008
+
8009
+ // UUID timestamps are 100 nano-second units since the Gregorian epoch,
8010
+ // (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
8011
+ // time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
8012
+ // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
8013
+ var msecs = options.msecs !== undefined ? options.msecs : new Date().getTime();
8014
+
8015
+ // Per 4.2.1.2, use count of uuid's generated during the current clock
8016
+ // cycle to simulate higher resolution clock
8017
+ var nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1;
8018
+
8019
+ // Time since last uuid creation (in msecs)
8020
+ var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000;
8021
+
8022
+ // Per 4.2.1.2, Bump clockseq on clock regression
8023
+ if (dt < 0 && options.clockseq === undefined) {
8024
+ clockseq = clockseq + 1 & 0x3fff;
8025
+ }
8026
+
8027
+ // Reset nsecs if clock regresses (new clockseq) or we've moved onto a new
8028
+ // time interval
8029
+ if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) {
8030
+ nsecs = 0;
8031
+ }
8032
+
8033
+ // Per 4.2.1.2 Throw error if too many uuids are requested
8034
+ if (nsecs >= 10000) {
8035
+ throw new Error('uuid.v1(): Can\'t create more than 10M uuids/sec');
8036
+ }
8037
+
8038
+ _lastMSecs = msecs;
8039
+ _lastNSecs = nsecs;
8040
+ _clockseq = clockseq;
8041
+
8042
+ // Per 4.1.4 - Convert from unix epoch to Gregorian epoch
8043
+ msecs += 12219292800000;
8044
+
8045
+ // `time_low`
8046
+ var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
8047
+ b[i++] = tl >>> 24 & 0xff;
8048
+ b[i++] = tl >>> 16 & 0xff;
8049
+ b[i++] = tl >>> 8 & 0xff;
8050
+ b[i++] = tl & 0xff;
8051
+
8052
+ // `time_mid`
8053
+ var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff;
8054
+ b[i++] = tmh >>> 8 & 0xff;
8055
+ b[i++] = tmh & 0xff;
8056
+
8057
+ // `time_high_and_version`
8058
+ b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
8059
+ b[i++] = tmh >>> 16 & 0xff;
8060
+
8061
+ // `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
8062
+ b[i++] = clockseq >>> 8 | 0x80;
8063
+
8064
+ // `clock_seq_low`
8065
+ b[i++] = clockseq & 0xff;
8066
+
8067
+ // `node`
8068
+ var node = options.node || _nodeId;
8069
+ for (var n = 0; n < 6; ++n) {
8070
+ b[i + n] = node[n];
8071
+ }
8072
+
8073
+ return buf ? buf : bytesToUuid(b);
8074
+ }
8075
+
8076
+ module.exports = v1;
8077
+
8078
+ },{"./lib/bytesToUuid":29,"./lib/rng":30}],32:[function(require,module,exports){
8079
+ var rng = require('./lib/rng');
8080
+ var bytesToUuid = require('./lib/bytesToUuid');
8081
+
8082
+ function v4(options, buf, offset) {
8083
+ var i = buf && offset || 0;
8084
+
8085
+ if (typeof(options) == 'string') {
8086
+ buf = options == 'binary' ? new Array(16) : null;
8087
+ options = null;
8088
+ }
8089
+ options = options || {};
8090
+
8091
+ var rnds = options.random || (options.rng || rng)();
8092
+
8093
+ // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
8094
+ rnds[6] = (rnds[6] & 0x0f) | 0x40;
8095
+ rnds[8] = (rnds[8] & 0x3f) | 0x80;
8096
+
8097
+ // Copy bytes to buffer, if provided
8098
+ if (buf) {
8099
+ for (var ii = 0; ii < 16; ++ii) {
8100
+ buf[i + ii] = rnds[ii];
8101
+ }
8102
+ }
8103
+
8104
+ return buf || bytesToUuid(rnds);
8105
+ }
8106
+
8107
+ module.exports = v4;
8108
+
8109
+ },{"./lib/bytesToUuid":29,"./lib/rng":30}],33:[function(require,module,exports){
7847
8110
  const Day = require('@barchart/common-js/lang/Day'),
7848
8111
  Decimal = require('@barchart/common-js/lang/Decimal');
7849
8112
 
@@ -8200,7 +8463,7 @@ describe('After the PositionSummaryFrame enumeration is initialized', () => {
8200
8463
  });
8201
8464
  });
8202
8465
 
8203
- },{"./../../../lib/data/PositionSummaryFrame":2,"./../../../lib/data/TransactionType":3,"@barchart/common-js/lang/Day":16,"@barchart/common-js/lang/Decimal":17}],29:[function(require,module,exports){
8466
+ },{"./../../../lib/data/PositionSummaryFrame":2,"./../../../lib/data/TransactionType":3,"@barchart/common-js/lang/Day":16,"@barchart/common-js/lang/Decimal":17}],34:[function(require,module,exports){
8204
8467
  const Currency = require('@barchart/common-js/lang/Currency'),
8205
8468
  Decimal = require('@barchart/common-js/lang/Decimal');
8206
8469
 
@@ -8310,4 +8573,4 @@ describe('When a position container data is gathered', () => {
8310
8573
  });
8311
8574
  });
8312
8575
 
8313
- },{"./../../../lib/data/InstrumentType":1,"./../../../lib/processing/PositionContainer":4,"./../../../lib/processing/definitions/PositionLevelDefinition":7,"./../../../lib/processing/definitions/PositionLevelType":8,"./../../../lib/processing/definitions/PositionTreeDefinition":9,"@barchart/common-js/lang/Currency":15,"@barchart/common-js/lang/Decimal":17}]},{},[28,29]);
8576
+ },{"./../../../lib/data/InstrumentType":1,"./../../../lib/processing/PositionContainer":4,"./../../../lib/processing/definitions/PositionLevelDefinition":7,"./../../../lib/processing/definitions/PositionLevelType":8,"./../../../lib/processing/definitions/PositionTreeDefinition":9,"@barchart/common-js/lang/Currency":15,"@barchart/common-js/lang/Decimal":17}]},{},[33,34]);