Skip to content

Commit

Permalink
Fix empty lines added on every exit
Browse files Browse the repository at this point in the history
  • Loading branch information
marcpage committed Nov 8, 2023
1 parent 419d73e commit ec788cb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
6 changes: 3 additions & 3 deletions brokerage_equities_sum.js
Original file line number Diff line number Diff line change
@@ -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/*
Expand All @@ -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.
Expand Down Expand Up @@ -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*$/)) {
Expand Down
19 changes: 13 additions & 6 deletions brokerage_equities_sun.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});

0 comments on commit ec788cb

Please sign in to comment.