Spreadsheet: Put common Range(s) functionality into CommonRange class

This commit is contained in:
u9g 2022-03-04 01:46:58 +00:00 committed by Ali Mohammad Pur
parent 7d052250f2
commit 87c818c571
Notes: sideshowbarker 2024-07-18 02:13:10 +09:00
2 changed files with 26 additions and 40 deletions

View file

@ -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;

View file

@ -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();