-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathledger-api.js
More file actions
154 lines (128 loc) · 3.71 KB
/
ledger-api.js
File metadata and controls
154 lines (128 loc) · 3.71 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const util = require('util')
const exec = util.promisify(require('child_process').exec)
// TODO: Make this component composable as opposed to passing in multiple args
// Since most of the arguments are redundant anyway
// Wrapper functions
const cloneInstance = (instance) => {
return Object.assign(Object.create(Object.getPrototypeOf(instance)), instance)
}
const padSpace = (str) => {
return str.padStart(str.length + 1, ' ')
}
// Cleans the string so that it removes
// all spaces before and after the string
// then adds a space in front of it
const cleanInput = (str) => {
return padSpace(str.trim())
}
class LedgerApi {
constructor () {
this._file = ``
this._commodity = ``
this._period = ``
this._extraArgs = ``
this._query = ``
this.BY_MONTH = 0
this.BY_DAY = 1
};
file (_file) {
const clone = cloneInstance(this)
clone._file = cleanInput(_file)
return clone
};
commodity (_commodity = ``) {
const clone = cloneInstance(this)
const commodityArg = (_commodity.length > 0) ? `-X` + cleanInput(_commodity) : ``
clone._commodity = cleanInput(commodityArg)
return clone
};
// Month by default
period (_period = this.BY_MONTH) {
const clone = cloneInstance(this)
const periodType = _period === this.BY_MONTH ? '-M' : '-D'
clone._period = cleanInput(periodType)
return clone
};
extraArgs (_extraArgs = ``) {
const clone = cloneInstance(this)
clone._extraArgs = cleanInput(_extraArgs)
return clone
};
setQuery (_query) {
const clone = cloneInstance(this)
clone._query = cleanInput(_query)
return clone
};
async getAccounts (filterExpr = ``) {
const cleanedFilterExpr = cleanInput(filterExpr)
const { stdout } = await exec(`ledger accounts -f` + this._file + cleanedFilterExpr + this._extraArgs)
const accounts = stdout.split('\n').filter(x => x.length > 0)
return { accounts }
};
async getTimeline () {
// Convert datetime format from:
/*
2018-08-17 -63.26
2018-08-18 -2032.85
2018-08-20 -32.36
2018-08-21 -2050.08
to
[
{
date: [2018/08/17, 2018/08/18, 2018/08/20, 2018/08/21],
data: [-63.26 ,-2032.85 ,-32.36 ,-2050.08]
}
]
*/
const { stdout } = await exec(`ledger reg -f` +
this._file +
this._query +
this._commodity +
` -j` +
this._period +
this._extraArgs +
` --collapse` +
` --plot-total-format="%(format_date(date, "%Y-%m-%d")) %(abs(quantity(scrub(display_total))))"`.split('\n').join('')
)
return stdout
.split('\n')
.filter(x => x.length > 0)
.reduce((obj, x) => {
const s = x.split(' ')
const date = s[0].split('-').join('/')
const data = parseFloat(s[1])
return {
date: obj.date.concat([date]),
data: obj.data.concat([data])
}
}, {date: [], data: []})
};
async getCommodities () {
const { stdout } = await exec('ledger commodities -f' + this._file + this._extraArgs)
const commodities = stdout.split('\n').filter(x => x.length > 0)
return { commodities }
};
async getGrowth () {
const { stdout } = await exec(`ledger reg -f` +
this._file +
this._query +
this._commodity +
` -J -M` +
this._extraArgs +
` --collapse`.split('\n').join('')
)
const growth = stdout
.split('\n')
.filter(x => x.length > 0)
.reduce((acc, x) => {
const xSplit = x.split(' ')
const date = xSplit[0].split('-').splice(0, 2).join('/')
const value = parseFloat(xSplit[1])
acc[date] = value
return acc
}, {})
return { growth }
};
}
const ledgerApi = new LedgerApi()
module.exports = ledgerApi