mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:17:35 +00:00
Spreadsheet: Add SplitRange class and CommonRange#filter
This commit is contained in:
parent
1b6a1748f0
commit
b9d44eb022
2 changed files with 46 additions and 1 deletions
|
@ -189,6 +189,40 @@ class CommonRange {
|
||||||
this.forEach(val => cells.push(val));
|
this.forEach(val => cells.push(val));
|
||||||
return cells;
|
return cells;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filter(matches) {
|
||||||
|
const cells = [];
|
||||||
|
this.forEach(cell => {
|
||||||
|
if (matches(cell)) cells.push(cell);
|
||||||
|
});
|
||||||
|
return new SplitRange(cells);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SplitRange extends CommonRange {
|
||||||
|
constructor(cells) {
|
||||||
|
super();
|
||||||
|
this.cells = cells;
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromNames(...cellNames) {
|
||||||
|
return new SplitRange(cellNames.map(Position.from_name));
|
||||||
|
}
|
||||||
|
|
||||||
|
first() {
|
||||||
|
return this.cellNames[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
forEach(callback) {
|
||||||
|
for (const cell of this.cells) {
|
||||||
|
if (callback(cell) === Break) return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
toString() {
|
||||||
|
const namesFormatted = this.cells.map(cell => '"' + cell.name + '"').join(", ");
|
||||||
|
return `SplitRange.fromNames(${namesFormatted})`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Ranges extends CommonRange {
|
class Ranges extends CommonRange {
|
||||||
|
@ -388,7 +422,7 @@ function numericResolve(cells) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolve(cells) {
|
function resolve(cells) {
|
||||||
const isRange = cells instanceof Range || cells instanceof Ranges;
|
const isRange = cells instanceof CommonRange;
|
||||||
return isRange ? cells.toArray().map(cell => cell.value()) : cells;
|
return isRange ? cells.toArray().map(cell => cell.value()) : cells;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -189,6 +189,17 @@ describe("Range", () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("SplitRange", () => {
|
||||||
|
makeSheet();
|
||||||
|
test("Range#filter => SplitRange", () => {
|
||||||
|
const range = R`A0:B`.filter(c => c.value() % 2 === 1);
|
||||||
|
expect(range.toString()).toEqual('SplitRange.fromNames("A0", "A2", "B0", "B2")');
|
||||||
|
expect(resolve(range)).toEqual(["1", "3", "1", "9"]);
|
||||||
|
expect(numericResolve(range)).toEqual([1, 3, 1, 9]);
|
||||||
|
expect(count(range)).toEqual(4);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("R function", () => {
|
describe("R function", () => {
|
||||||
makeSheet();
|
makeSheet();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue