From 654fe2fec4fa830f771c6dd24b96ee222807d5f3 Mon Sep 17 00:00:00 2001 From: Tony Grosinger Date: Fri, 26 Nov 2021 08:53:55 -0800 Subject: [PATCH] Fix multiple formula lines being applied from the bottom up They are now applied from the top down. Fixes #8 --- src/calc/calc.ts | 2 +- test/calc.ts | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/calc/calc.ts b/src/calc/calc.ts index 333daa8..f054b85 100644 --- a/src/calc/calc.ts +++ b/src/calc/calc.ts @@ -170,7 +170,7 @@ export const parseAndApply = ( // If there is no error, return formulas.andThen((innerFormulas: Formula[]) => // for each formula - innerFormulas.reduce>( + innerFormulas.reduceRight>( (prevValue, formula) => // If the previous formula didn't give an error prevValue.andThen( diff --git a/test/calc.ts b/test/calc.ts index d3924cb..8b66485 100644 --- a/test/calc.ts +++ b/test/calc.ts @@ -1957,5 +1957,72 @@ describe('Formulas', () => { ]); } }); + + it('should apply multiple formula lines sequentially', () => { + { + const textEditor = new TextEditor([ + 'foo', + '| A | B |', + '| --- | --- |', + '| 1 | 2 |', + '| 3 | 4 |', + '| 5 | 6 |', + '| | |', + '', + '', + ]); + textEditor.setCursorPosition(new Point(1, 0)); + const tableEditor = new TableEditor(textEditor); + const err = tableEditor.evaluateFormulas(defaultOptions); + const pos = textEditor.getCursorPosition(); + expect(err).to.be.undefined; + expect(pos.row).to.equal(1); + expect(pos.column).to.equal(0); + expect(textEditor.getSelectionRange()).to.be.undefined; + expect(textEditor.getLines()).to.deep.equal([ + 'foo', + '| A | B |', + '| --- | --- |', + '| 1 | 2 |', + '| 3 | 4 |', + '| 5 | 6 |', + '| 5 | 4 |', + '', + '', + ]); + } + { + const textEditor = new TextEditor([ + 'foo', + '| A | B |', + '| --- | --- |', + '| 1 | 2 |', + '| 3 | 4 |', + '| 5 | 6 |', + '| | |', + '', + '', + ]); + textEditor.setCursorPosition(new Point(1, 0)); + const tableEditor = new TableEditor(textEditor); + const err = tableEditor.evaluateFormulas(defaultOptions); + const pos = textEditor.getCursorPosition(); + expect(err).to.be.undefined; + expect(pos.row).to.equal(1); + expect(pos.column).to.equal(0); + expect(textEditor.getSelectionRange()).to.be.undefined; + expect(textEditor.getLines()).to.deep.equal([ + 'foo', + '| A | B |', + '| --- | --- |', + '| 1 | 2 |', + '| 3 | 4 |', + '| 5 | 6 |', + '| 7 | 4 |', + '', + '', + ]); + } + }); }); });