-
Notifications
You must be signed in to change notification settings - Fork 7
/
statement.js
52 lines (43 loc) · 1.34 KB
/
statement.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
var result = require('./lib/result')
var Table = require('./lib/table')
var weld = require('./lib/weld')
var Transaction = require('./transaction')
var Transactions = require('./transactions')
/**
* Represents a Statement
* @constructor
* @param {Object} attributes - Usually a statement definition
*/
function Statement (attributes) {
for (var key in attributes) {
if (attributes.hasOwnProperty(key)) this[key] = result(attributes[key])
}
// Convert table rows to array of transactions
var transactions = Table.prototype.rowsToArray(this.rows, {
processRow: function (row) {
return this.createTransaction(weld(this.columns, row))
}.bind(this)
})
this.transactions = new Transactions(transactions, this)
}
/**
* Creates a transaction from an object of attributes.
* @returns {Transaction}
*/
Statement.prototype.createTransaction = function (attributes) {
attributes.dateString = attributes.date
attributes.dateFormat = this.dateFormat
delete attributes.date
return new Transaction(attributes)
}
/**
* @returns {String} The name of the statement based on the statement date
*/
Statement.prototype.name = function () {
var label = this.institution + ' Statement'
if (this.transactions.length) {
return label + ' ' + this.transactions.last().getFormatted('date')
}
return label
}
module.exports = Statement