@barchart/portfolio-api-common 1.2.68 → 1.2.72

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.
@@ -30,13 +30,13 @@ module.exports = (() => {
30
30
  * @static
31
31
  * @param {Array.<Object>} transactions
32
32
  * @param {Array.<Object>} positions
33
- * @param {Boolean=} mutate
33
+ * @param {Boolean=} descending
34
34
  * @returns {Array}
35
35
  */
36
- static format(transactions, positions, mutate) {
36
+ static format(transactions, positions, descending) {
37
37
  assert.argumentIsArray(transactions, 'transactions');
38
38
  assert.argumentIsArray(positions, 'positions');
39
- assert.argumentIsOptional(mutate, 'mutate', Boolean);
39
+ assert.argumentIsOptional(descending, 'descending', Boolean);
40
40
 
41
41
  const instruments = positions.reduce((map, p) => {
42
42
  const instrument = Object.assign({ }, p.instrument || { });
@@ -68,22 +68,20 @@ module.exports = (() => {
68
68
  formatted = Object.assign({}, formatted, formattedTransaction);
69
69
  }
70
70
 
71
- let transactionToInsert;
72
-
73
- if (mutate) {
74
- transaction.formatted = formatted;
75
-
76
- transactionToInsert = transaction;
77
- } else {
78
- transactionToInsert = formatted;
79
- }
80
-
81
- list.push(transactionToInsert);
71
+ list.push(formatted);
82
72
  }
83
73
 
84
74
  return list;
85
75
  }, [ ]);
86
76
 
77
+ let comparator;
78
+
79
+ if (is.boolean(descending) && descending) {
80
+ comparator = comparatorDescending;
81
+ } else {
82
+ comparator = comparatorAscending;
83
+ }
84
+
87
85
  a.sort(comparator);
88
86
 
89
87
  a.forEach((t) => {
@@ -99,9 +97,21 @@ module.exports = (() => {
99
97
  * @public
100
98
  * @static
101
99
  * @param {Array.<Object>} transactions
100
+ * @param {Boolean=} descending
102
101
  * @returns {Array}
103
102
  */
104
- sort(transactions) {
103
+ sort(transactions, descending) {
104
+ assert.argumentIsArray(transactions, 'transactions');
105
+ assert.argumentIsOptional(descending, 'descending', Boolean);
106
+
107
+ let comparator;
108
+
109
+ if (is.boolean(descending) && descending) {
110
+ comparator = comparatorDescending;
111
+ } else {
112
+ comparator = comparatorAscending;
113
+ }
114
+
105
115
  return transactions.sort(comparator);
106
116
  }
107
117
 
@@ -277,15 +287,14 @@ module.exports = (() => {
277
287
  }
278
288
  }
279
289
 
280
- const comparator = ComparatorBuilder.startWith((a, b) => {
281
- return Day.compareDays(a.date, b.date);
282
- }).thenBy((a, b) => {
283
- return comparators.compareNumbers(getInstrumentTypePriority(a.instrument.type), getInstrumentTypePriority(b.instrument.type));
284
- }).thenBy((a, b) => {
285
- return comparators.compareStrings(a.instrument.id, b.instrument.id);
286
- }).thenBy((a, b) => {
287
- return comparators.compareNumbers(a.sequence, b.sequence);
288
- }).toComparator();
290
+ const comparatorAscending = ComparatorBuilder.startWith((a, b) => Day.compareDays(a.date, b.date))
291
+ .thenBy((a, b) => comparators.compareNumbers(getInstrumentTypePriority(a.instrument.type), getInstrumentTypePriority(b.instrument.type)))
292
+ .thenBy((a, b) => comparators.compareStrings(a.instrument.id, b.instrument.id))
293
+ .thenBy((a, b) => comparators.compareNumbers(a.sequence, b.sequence))
294
+ .toComparator();
295
+
296
+ const comparatorDescending = ComparatorBuilder.startWith((a, b) => comparatorAscending(b, a))
297
+ .toComparator();
289
298
 
290
299
  return TransactionFormatter;
291
300
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.68",
3
+ "version": "1.2.72",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",