1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:57:44 +00:00

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