diff options
author | u9g <git@u9g.dev> | 2022-03-04 21:48:12 -0500 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2022-03-09 10:22:20 +0330 |
commit | b9d44eb0226d5399f60ace3fe978b22174beb1e9 (patch) | |
tree | 42bbecbe383cb7a17aa21934278db89d45cbf139 /Base | |
parent | 1b6a1748f06078084e53c8ff58802c97cf5bbf8d (diff) | |
download | serenity-b9d44eb0226d5399f60ace3fe978b22174beb1e9.zip |
Spreadsheet: Add SplitRange class and CommonRange#filter
Diffstat (limited to 'Base')
-rw-r--r-- | Base/res/js/Spreadsheet/runtime.js | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/Base/res/js/Spreadsheet/runtime.js b/Base/res/js/Spreadsheet/runtime.js index d34b3e3ffe..a0604d24f3 100644 --- a/Base/res/js/Spreadsheet/runtime.js +++ b/Base/res/js/Spreadsheet/runtime.js @@ -189,6 +189,40 @@ class CommonRange { this.forEach(val => cells.push(val)); 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 { @@ -388,7 +422,7 @@ function numericResolve(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; } |