-
Notifications
You must be signed in to change notification settings - Fork 7
/
transactions.js
89 lines (70 loc) · 2.26 KB
/
transactions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var TransactionDates = require('./transaction-dates')
/**
* An array-like class that represents a collection of transactions
* @constructor
* @param {Array} transactions - An array of Transaction objects
* @param {Object} statement - The parent statement
* @returns {Array} - An array of transactions with convenience methods
*/
function Transactions (transactions, statement) {
Transactions._injectPrototypeMethods(transactions)
/**
* Some financial institutions omit the year part in their date cells.
* This workaround calculates the year for each transaction affected.
*/
if (!/Y{2,}/.test(statement.dateFormat)) {
if (!transactions.chronological()) transactions = transactions.reverse()
var succeedingDate = statement.date
for (var i = transactions.length - 1; i >= 0; i--) {
var transaction = transactions[i]
transaction.setDate({ succeedingDate: succeedingDate })
succeedingDate = transaction.get('date')
}
}
return transactions
}
Transactions.prototype.chronological = function () {
return dates.call(this).chronological()
function dates () {
var dates = this.map(function (transaction) {
return transaction.get('transactionDate')
})
return new TransactionDates(dates)
}
}
/**
* @returns {Transaction} The first transaction in the collection
*/
Transactions.prototype.first = function () {
return this[0]
}
/**
* @returns {Transaction} The last transaction in the collection
*/
Transactions.prototype.last = function () {
return this[this.length - 1]
}
/**
* @returns {Array} An array of formatted transaction attribute arrays
*/
Transactions.prototype.toArray = function (keys) {
return this.map(function (transaction) { return transaction.toArray(keys) })
}
/**
* @returns {Array} An array of formatted transaction objects
*/
Transactions.prototype.toJSON = function (keys) {
return this.map(function (transaction) { return transaction.toJSON(keys) })
}
/**
* Adds the prototype methods to transactions array to appear like inheritance
* @private
*/
Transactions._injectPrototypeMethods = function (array) {
for (var method in this.prototype) {
if (this.prototype.hasOwnProperty(method)) {
array[method] = this.prototype[method]
}
}
}
module.exports = Transactions