From 87c818c571c6a3bf3e653b06dba0b0eca4e86284 Mon Sep 17 00:00:00 2001 From: u9g Date: Fri, 4 Mar 2022 01:46:58 +0000 Subject: [PATCH] Spreadsheet: Put common Range(s) functionality into CommonRange class --- Base/res/js/Spreadsheet/runtime.js | 62 +++++++------------ .../Applications/Spreadsheet/Tests/basic.js | 4 +- 2 files changed, 26 insertions(+), 40 deletions(-) diff --git a/Base/res/js/Spreadsheet/runtime.js b/Base/res/js/Spreadsheet/runtime.js index 5bff2f269b..daed33169d 100644 --- a/Base/res/js/Spreadsheet/runtime.js +++ b/Base/res/js/Spreadsheet/runtime.js @@ -128,8 +128,29 @@ class Position { } } -class Ranges { +class CommonRange { + at(wantedIx) { + let ix = 0; + let found = null; + this.forEach(cell => { + if (ix++ === wantedIx) { + found = cell; + return Break; + } + }); + return found; + } + + toArray() { + const cells = []; + this.forEach(val => cells.push(val)); + return cells; + } +} + +class Ranges extends CommonRange { constructor(ranges) { + super(); this.ranges = ranges; } @@ -147,18 +168,6 @@ class Ranges { } } - at(wantedIx) { - let ix = 0; - let found = null; - this.forEach(cell => { - if (ix++ === wantedIx) { - found = cell; - return Break; - } - }); - return found; - } - union(other, direction = "right") { if (direction === "left") { if (other instanceof Ranges) return Ranges.from(...other.ranges, ...this.ranges); @@ -171,19 +180,14 @@ class Ranges { } } - toArray() { - const cells = []; - this.forEach(val => cells.push(val)); - return cells; - } - toString() { return `Ranges.from(${this.ranges.map(r => r.toString()).join(", ")})`; } } -class Range { +class Range extends CommonRange { constructor(startingColumnName, endingColumnName, startingRow, endingRow, columnStep, rowStep) { + super(); // using == to account for '0' since js will parse `+'0'` to 0 if (columnStep == 0 || rowStep == 0) throw new Error("rowStep or columnStep is 0, this will cause an infinite loop"); @@ -235,18 +239,6 @@ class Range { } } - at(wantedIx) { - let ix = 0; - let found = null; - this.forEach(cell => { - if (ix++ === wantedIx) { - found = cell; - return Break; - } - }); - return found; - } - union(other) { if (other instanceof Ranges) return other.union(this, "left"); @@ -273,12 +265,6 @@ class Range { } } - toArray() { - const cells = []; - this.forEach(val => cells.push(val)); - return cells; - } - toString() { const endingRow = this.endingRow ?? ""; const showSteps = this.rowStep !== 1 || this.columnStep !== 1; diff --git a/Userland/Applications/Spreadsheet/Tests/basic.js b/Userland/Applications/Spreadsheet/Tests/basic.js index 93011cb41d..c38b566db7 100644 --- a/Userland/Applications/Spreadsheet/Tests/basic.js +++ b/Userland/Applications/Spreadsheet/Tests/basic.js @@ -109,7 +109,7 @@ describe("Range", () => { expect(R`A2:A25`.first().name).toEqual("A2"); }); - test("Range#at", () => { + test("CommonRange#at", () => { const workbook = createWorkbook(); const sheet = createSheet(workbook, "Sheet 1"); sheet.makeCurrent(); @@ -125,7 +125,7 @@ describe("Range", () => { expect(Ranges.from(R`A0:A2`, R`B0:B2`).at(5).name).toEqual("B2"); }); - test("Range(s)#toArray", () => { + test("CommonRange#toArray", () => { const workbook = createWorkbook(); const sheet = createSheet(workbook, "Sheet 1"); sheet.makeCurrent();