mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 13:47:35 +00:00
Spreadsheet: Add find, findIndex, indexOf, & has to CommonRange
This commit is contained in:
parent
87c818c571
commit
75a02300ba
2 changed files with 107 additions and 16 deletions
|
@ -141,6 +141,49 @@ class CommonRange {
|
||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
findIndex(matcher) {
|
||||||
|
let i = 0;
|
||||||
|
let found = false;
|
||||||
|
this.forEach(cell => {
|
||||||
|
if (matcher(cell, i)) {
|
||||||
|
found = true;
|
||||||
|
return Break;
|
||||||
|
}
|
||||||
|
++i;
|
||||||
|
});
|
||||||
|
return found ? i : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
find(matcher) {
|
||||||
|
let value = null;
|
||||||
|
let i = 0;
|
||||||
|
this.forEach(cell => {
|
||||||
|
if (matcher(cell, i)) {
|
||||||
|
value = cell;
|
||||||
|
return Break;
|
||||||
|
}
|
||||||
|
++i;
|
||||||
|
});
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
indexOf(name) {
|
||||||
|
let i = 0;
|
||||||
|
let found = false;
|
||||||
|
this.forEach(cell => {
|
||||||
|
if (cell.name === name) {
|
||||||
|
found = true;
|
||||||
|
return Break;
|
||||||
|
}
|
||||||
|
++i;
|
||||||
|
});
|
||||||
|
return found ? i : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
has(name) {
|
||||||
|
return this.indexOf(name) !== -1;
|
||||||
|
}
|
||||||
|
|
||||||
toArray() {
|
toArray() {
|
||||||
const cells = [];
|
const cells = [];
|
||||||
this.forEach(val => cells.push(val));
|
this.forEach(val => cells.push(val));
|
||||||
|
|
|
@ -144,25 +144,53 @@ describe("Range", () => {
|
||||||
.toString()
|
.toString()
|
||||||
).toEqual("<Cell at A0>,<Cell at A1>,<Cell at A2>,<Cell at B0>,<Cell at B1>,<Cell at B2>");
|
).toEqual("<Cell at A0>,<Cell at A1>,<Cell at A2>,<Cell at B0>,<Cell at B1>,<Cell at B2>");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("CommonRange#findIndex", () => {
|
||||||
|
makeSheet();
|
||||||
|
const range = R`B`;
|
||||||
|
let idxs = [];
|
||||||
|
let values = [];
|
||||||
|
const idx = range.findIndex((val, idx) => {
|
||||||
|
idxs.push(idx);
|
||||||
|
values.push(val.value());
|
||||||
|
return integer(val.value()) === 4;
|
||||||
|
});
|
||||||
|
expect(idx).toEqual(1);
|
||||||
|
expect(values).toEqual(["1", "4"]);
|
||||||
|
expect(idxs).toEqual([0, 1]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("CommonRange#find", () => {
|
||||||
|
makeSheet();
|
||||||
|
const range = R`B`;
|
||||||
|
let idxs = [];
|
||||||
|
let values = [];
|
||||||
|
const val = range.find((val, idx) => {
|
||||||
|
idxs.push(idx);
|
||||||
|
values.push(val.value());
|
||||||
|
return integer(val.value()) === 4;
|
||||||
|
});
|
||||||
|
expect(val.name).toEqual("B1");
|
||||||
|
expect(values).toEqual(["1", "4"]);
|
||||||
|
expect(idxs).toEqual([0, 1]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("CommonRange#indexOf", () => {
|
||||||
|
makeSheet();
|
||||||
|
const range = R`B`;
|
||||||
|
expect(range.indexOf("B1")).toEqual(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("CommonRange#has", () => {
|
||||||
|
makeSheet();
|
||||||
|
const range = R`B`;
|
||||||
|
expect(range.has("B1")).toEqual(true);
|
||||||
|
expect(range.has("B4")).toEqual(false);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("R function", () => {
|
describe("R function", () => {
|
||||||
const workbook = createWorkbook();
|
makeSheet();
|
||||||
const sheet = createSheet(workbook, "Sheet 1");
|
|
||||||
sheet.makeCurrent();
|
|
||||||
/*
|
|
||||||
A B
|
|
||||||
+++
|
|
||||||
0+1 1
|
|
||||||
1+2 4
|
|
||||||
2+3 9
|
|
||||||
*/
|
|
||||||
for (const row of ["A", "B"]) {
|
|
||||||
for (let col of [0, 1, 2]) {
|
|
||||||
sheet.setCell(row, col++, row === "A" ? col : Math.pow(col, 2));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sheet.focusCell("A", 0);
|
|
||||||
|
|
||||||
test("Check for correctness: R`A0:A`", () => {
|
test("Check for correctness: R`A0:A`", () => {
|
||||||
const range = R`A0:A`;
|
const range = R`A0:A`;
|
||||||
|
@ -204,3 +232,23 @@ describe("R function", () => {
|
||||||
expect(sum(range)).toEqual(14);
|
expect(sum(range)).toEqual(14);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
A B
|
||||||
|
+++
|
||||||
|
0+1 1
|
||||||
|
1+2 4
|
||||||
|
2+3 9
|
||||||
|
*/
|
||||||
|
|
||||||
|
function makeSheet() {
|
||||||
|
const workbook = createWorkbook();
|
||||||
|
const sheet = createSheet(workbook, "Sheet 1");
|
||||||
|
sheet.makeCurrent();
|
||||||
|
for (const row of ["A", "B"]) {
|
||||||
|
for (let col of [0, 1, 2]) {
|
||||||
|
sheet.setCell(row, col++, row === "A" ? col : Math.pow(col, 2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sheet.focusCell("A", 0);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue