-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtransaction-dates.js
More file actions
70 lines (56 loc) · 1.57 KB
/
transaction-dates.js
File metadata and controls
70 lines (56 loc) · 1.57 KB
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
/**
* Represents a collection of transaction dates
* @constructor
* @private
* @param {Array} dates - An array of objects in the form { year: year, month: month, date: date }
*/
function TransactionDates (dates) {
this.dates = dates
}
/**
* Determines whether the dates are chronological or not
* @returns {Boolean}
*/
TransactionDates.prototype.chronological = function () {
var uniq = this.uniq()
if (uniq.length < 2) return true
return this.compare(uniq[0], uniq[1]) >= 0
}
/**
* @returns {Array} The unique dates
*/
TransactionDates.prototype.uniq = function () {
var uniqs = []
for (var i = 0; i < this.dates.length; i++) {
var date = this.dates[i]
if (inUniqs(date)) continue
uniqs.push(date)
}
return uniqs
// Determines whether a date already exists in the uniqs array
function inUniqs (d) {
return uniqs.some(function (u) {
return u.year === d.year && u.month === d.month && u.date === d.date
})
}
}
/**
* Compares two dates to test chronology
* @returns {Number} 0: a == b, 1: a > b, -1: a < b
*/
TransactionDates.prototype.compare = function (a, b) {
// If no year, and dates go from Dec - Jan, assume Dec date is older
if ((!a.year || !b.year) && a.month === 11 && b.month === 0) return 1
if (a.year === b.year) {
if (a.month === b.month) {
if (a.date > b.date) return -1
if (a.date < b.date) return 1
return 0
}
if (a.month > b.month) return -1
if (a.month < b.month) return 1
}
if (a.year > b.year) return -1
if (a.year < b.year) return 1
}
module.exports = TransactionDates