git ssb

0+

Rômulo Alves / plainbudget



Commit 803af62eca94e1a31162f52261b03aacd31cd8b2

Fixes export and accepts decimal numbers

Rômulo Alves committed on 8/30/2018, 4:37:25 PM
Parent: 184db972620d234dffd40e51def08b91c9e9d3e5

Files changed

index.jschanged
package-lock.jsonadded
index.jsView
@@ -1,9 +1,9 @@
11
22 const NAME_REGEX = /^([a-z0-9\-.\/ ]+)$/i // eslint-disable-line
33 const MULTIPLIER_REGEX = /^(.*?)\s+x\s+(\d+)$/
44
5-export default class Plainbudget {
5 +module.exports = class Plainbudget {
66
77 static computeSheet (sheet) {
88 const pb = new Plainbudget(sheet)
99
@@ -47,9 +47,9 @@
4747 this.text = text
4848 }
4949
5050 parseValue (line) {
51- let value = line.slice(1).match(/^\s+(\d+)/)
51 + let value = line.slice(1).match(/^\s+(\d+(.\d+)?)/)
5252
5353 if (value) {
5454 value = parseFloat(value[1])
5555
@@ -91,13 +91,12 @@
9191 this.groups = []
9292 this.lines = this.text.split(/\n/)
9393
9494 let group = null
95- let op, line
9695
9796 for (let i = 0; i < this.lines.length; i++) {
98- line = this.lines[i].trim()
99- op = line[0]
97 + const line = this.lines[i].trim()
98 + const op = line[0]
10099
101100 if ('=+'.includes(op) && group === null) {
102101 group = [this.parseLine(line)]
103102 } else if ('-~+x'.includes(op)) {
@@ -121,15 +120,15 @@
121120 }
122121
123122 const padding = Math.max(this.padding, this.getPadding())
124123 const updated = []
125- let group, op
126124
127125 for (let x = 0; x < this.groups.length; x++) {
128- group = this.groups[x]
126 + const group = this.groups[x]
129127
130128 for (let y = 0; y < group.length; y++) {
131- op = group[y]
129 + const op = group[y]
130 +
132131 updated.push(`${op[0]} ${op[1].toString().padStart(padding)} ${op[2]}\n`)
133132 }
134133
135134 updated.push('\n')
@@ -148,9 +147,9 @@
148147 this.named[label] = value
149148 }
150149 }
151150
152- getNamed (label, value) {
151 + getNamed (label) {
153152 if (label.match(MULTIPLIER_REGEX)) {
154153 label = label.split(/\s+x\s+/)[0]
155154 }
156155
@@ -211,46 +210,42 @@
211210 if (!groupIndices.length) {
212211 return
213212 }
214213
215- let group, value
216- let topOps, ops
217- let topOp, op
218- let multiplier
219- let named
214 + let value
220215
221216 for (let g = 0; g < groupIndices.length; g++) {
222- group = this.groups[groupIndices[g]]
217 + const group = this.groups[groupIndices[g]]
223218
224219 if ('='.includes(group[0][0])) {
225220 if (group[0][2] === '') {
226221 group[0][2] = '\n'
227222 }
228223
229224 value = 0
230- topOps = group.slice(1)
225 + const topOps = group.slice(1)
231226
232227 for (let x = 0; x < topOps.length; x++) {
233- topOp = topOps[x]
228 + const topOp = topOps[x]
234229
235230 if (topOp[1] === '?' || topOp[0] === 'x') {
236231 continue
237232 }
238233
239- multiplier = this.parseMultiplier(topOp[2])
234 + const multiplier = this.parseMultiplier(topOp[2])
240235
241236 if (multiplier) {
242- value += topOp[1] * parseInt(multiplier[1])
237 + value += topOp[1] * parseFloat(multiplier[1])
243238 } else {
244239 value += topOp[1]
245240 }
246241 }
247242 } else if (group[0][0] === '+') {
248243 value = group[0][1]
249- ops = group.slice(1)
244 + const ops = group.slice(1)
250245
251246 for (let y = 0; y < ops.length; y++) {
252- op = ops[y]
247 + const op = ops[y]
253248
254249 if (op[1] === '?' || op[0] === 'x') {
255250 continue
256251 }
@@ -275,16 +270,15 @@
275270 getPadding (groups) {
276271 groups = groups || this.groups
277272
278273 let p = 3
279- let nlen, group
280274
281275 for (let x = 0; x < groups.length; x++) {
282- group = groups[x]
276 + const group = groups[x]
283277
284278 for (let y = 0; y < group.length; y++) {
285279 if (group[y][1] !== null) {
286- nlen = group[y][1].toString().length
280 + const nlen = group[y][1].toString().length
287281
288282 if (nlen > (p + 1)) {
289283 p = nlen + 1
290284 }
package-lock.jsonView
@@ -1,0 +1,5 @@
1 +{
2 + "name": "plainbudget",
3 + "version": "0.0.1",
4 + "lockfileVersion": 1
5 +}

Built with git-ssb-web