mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:37:35 +00:00
Spreadsheet: Put common Range(s) functionality into CommonRange class
This commit is contained in:
parent
7d052250f2
commit
87c818c571
2 changed files with 26 additions and 40 deletions
|
@ -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) {
|
constructor(ranges) {
|
||||||
|
super();
|
||||||
this.ranges = ranges;
|
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") {
|
union(other, direction = "right") {
|
||||||
if (direction === "left") {
|
if (direction === "left") {
|
||||||
if (other instanceof Ranges) return Ranges.from(...other.ranges, ...this.ranges);
|
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() {
|
toString() {
|
||||||
return `Ranges.from(${this.ranges.map(r => r.toString()).join(", ")})`;
|
return `Ranges.from(${this.ranges.map(r => r.toString()).join(", ")})`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Range {
|
class Range extends CommonRange {
|
||||||
constructor(startingColumnName, endingColumnName, startingRow, endingRow, columnStep, rowStep) {
|
constructor(startingColumnName, endingColumnName, startingRow, endingRow, columnStep, rowStep) {
|
||||||
|
super();
|
||||||
// using == to account for '0' since js will parse `+'0'` to 0
|
// using == to account for '0' since js will parse `+'0'` to 0
|
||||||
if (columnStep == 0 || rowStep == 0)
|
if (columnStep == 0 || rowStep == 0)
|
||||||
throw new Error("rowStep or columnStep is 0, this will cause an infinite loop");
|
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) {
|
union(other) {
|
||||||
if (other instanceof Ranges) return other.union(this, "left");
|
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() {
|
toString() {
|
||||||
const endingRow = this.endingRow ?? "";
|
const endingRow = this.endingRow ?? "";
|
||||||
const showSteps = this.rowStep !== 1 || this.columnStep !== 1;
|
const showSteps = this.rowStep !== 1 || this.columnStep !== 1;
|
||||||
|
|
|
@ -109,7 +109,7 @@ describe("Range", () => {
|
||||||
expect(R`A2:A25`.first().name).toEqual("A2");
|
expect(R`A2:A25`.first().name).toEqual("A2");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("Range#at", () => {
|
test("CommonRange#at", () => {
|
||||||
const workbook = createWorkbook();
|
const workbook = createWorkbook();
|
||||||
const sheet = createSheet(workbook, "Sheet 1");
|
const sheet = createSheet(workbook, "Sheet 1");
|
||||||
sheet.makeCurrent();
|
sheet.makeCurrent();
|
||||||
|
@ -125,7 +125,7 @@ describe("Range", () => {
|
||||||
expect(Ranges.from(R`A0:A2`, R`B0:B2`).at(5).name).toEqual("B2");
|
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 workbook = createWorkbook();
|
||||||
const sheet = createSheet(workbook, "Sheet 1");
|
const sheet = createSheet(workbook, "Sheet 1");
|
||||||
sheet.makeCurrent();
|
sheet.makeCurrent();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue