diff --git a/Base/res/js/Spreadsheet/runtime.js b/Base/res/js/Spreadsheet/runtime.js index daed33169d0..92b003729c2 100644 --- a/Base/res/js/Spreadsheet/runtime.js +++ b/Base/res/js/Spreadsheet/runtime.js @@ -141,6 +141,49 @@ class CommonRange { 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() { const cells = []; this.forEach(val => cells.push(val)); diff --git a/Userland/Applications/Spreadsheet/Tests/basic.js b/Userland/Applications/Spreadsheet/Tests/basic.js index c38b566db7a..f8b6074ae96 100644 --- a/Userland/Applications/Spreadsheet/Tests/basic.js +++ b/Userland/Applications/Spreadsheet/Tests/basic.js @@ -144,25 +144,53 @@ describe("Range", () => { .toString() ).toEqual(",,,,,"); }); + + 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", () => { - const workbook = createWorkbook(); - 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); + makeSheet(); test("Check for correctness: R`A0:A`", () => { const range = R`A0:A`; @@ -204,3 +232,23 @@ describe("R function", () => { 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); +}