From ec788cbafe4b545b92dac1245db90b63fb385702 Mon Sep 17 00:00:00 2001 From: Marc Page Date: Tue, 7 Nov 2023 18:50:24 -0600 Subject: [PATCH] Fix empty lines added on every exit --- brokerage_equities_sum.js | 6 +++--- brokerage_equities_sun.test.js | 19 +++++++++++++------ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/brokerage_equities_sum.js b/brokerage_equities_sum.js index 1372d5c..13b801e 100644 --- a/brokerage_equities_sum.js +++ b/brokerage_equities_sum.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Categorize Stocks // @namespace https://ResolveToExcel.com/ -// @version 1.1.7 +// @version 1.1.8 // @description Group and summarize stocks by category in your brokerage account // @author Marc Page // @match https://oltx.fidelity.com/ftgw/fbc/* @@ -21,6 +21,7 @@ 1.1.5 Staging for unit tests 1.1.6 Improved Fidelity input placement (11/5/2023) 1.1.7 Replaced TD Ameritrade with Schwab (11/5/2023) + 1.1.8 Constantly entering and leaving the input area now longer adds more blank lines (11/7/2023) */ /* Scrapes the symbols and the value of current value of equities in the positions tab on Fidelity's site. @@ -92,11 +93,10 @@ function parse_and_add(text, symbol_values) { for (var line_index = 0; line_index < lines.length; ++line_index) { var line = lines[line_index]; - if (line.match(/^\s*#/)) { + if (line.match(/^\s*#/) || line.match(/^\s*$/)) { continue; } - output += line + "\n"; if (line.match(/^\s*$/)) { diff --git a/brokerage_equities_sun.test.js b/brokerage_equities_sun.test.js index a514bd5..f6f93fc 100644 --- a/brokerage_equities_sun.test.js +++ b/brokerage_equities_sun.test.js @@ -2,31 +2,38 @@ const code_under_test = require("./brokerage_equities_sum"); test("Test parsing empty input, no symbols", () => { expect(code_under_test.parse_and_add("", {})) - .toBe("\n"); + .toBe(""); }); test("Test parsing empty input, one symbol", () => { expect(code_under_test.parse_and_add("", {AAPL: 55.00})) - .toBe("\n# unseen: AAPL\n# Total value = $55.00\n\n"); + .toBe("# unseen: AAPL\n# Total value = $55.00\n\n"); }); test("Test parsing one input, one symbol", () => { expect(code_under_test.parse_and_add("AAPL\n", {AAPL: 55.00})) - .toBe("AAPL\n# Total value = $55.00\n\n\n"); + .toBe("AAPL\n# Total value = $55.00\n\n"); }); test("Test parsing two inputs, one symbol", () => { expect(code_under_test.parse_and_add("AAPL MSFT\n", {AAPL: 55.00})) - .toBe("AAPL MSFT\n# Symbol MSFT was not found\n# Total value = $55.00\n\n\n"); + .toBe("AAPL MSFT\n# Symbol MSFT was not found\n# Total value = $55.00\n\n"); }); test("Test parsing two inputs, two symbols", () => { expect(code_under_test.parse_and_add("AAPL MSFT\n", {AAPL: 55.00, MSFT: 110.01})) - .toBe("AAPL MSFT\n# Total value = $165.01\n\n\n"); + .toBe("AAPL MSFT\n# Total value = $165.01\n\n"); }); test("Test parsing 2x2 inputs, 4 symbols", () => { expect(code_under_test.parse_and_add("AAPL MSFT\nVTI VYM\n", {AAPL: 55.00, MSFT: 110.01, VTI: 12.34, VYM: 43.21})) - .toBe("AAPL MSFT\n# Total value = $165.01\n\nVTI VYM\n# Total value = $55.55\n\n\n"); + .toBe("AAPL MSFT\n# Total value = $165.01\n\nVTI VYM\n# Total value = $55.55\n\n"); +}); + +test("Ensure extra blank lines are not added", () => { + expect(code_under_test.parse_and_add(code_under_test.parse_and_add("AAPL MSFT\nVTI VYM\n", + {AAPL: 55.00, MSFT: 110.01, VTI: 12.34, VYM: 43.21}), + {AAPL: 55.00, MSFT: 110.01, VTI: 12.34, VYM: 43.21})) + .toBe("AAPL MSFT\n# Total value = $165.01\n\nVTI VYM\n# Total value = $55.55\n\n"); });