mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 19:37:34 +00:00
Spreadsheet: Add CommonRange#unique()
This commit is contained in:
parent
b9d44eb022
commit
c8803afe3e
2 changed files with 28 additions and 0 deletions
|
@ -197,6 +197,19 @@ class CommonRange {
|
||||||
});
|
});
|
||||||
return new SplitRange(cells);
|
return new SplitRange(cells);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unique() {
|
||||||
|
const cells = [];
|
||||||
|
const values = new Set();
|
||||||
|
this.forEach(cell => {
|
||||||
|
const value = cell.value();
|
||||||
|
if (!values.has(value)) {
|
||||||
|
values.add(value);
|
||||||
|
cells.push(cell);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return new SplitRange(cells);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class SplitRange extends CommonRange {
|
class SplitRange extends CommonRange {
|
||||||
|
|
|
@ -198,6 +198,21 @@ describe("SplitRange", () => {
|
||||||
expect(numericResolve(range)).toEqual([1, 3, 1, 9]);
|
expect(numericResolve(range)).toEqual([1, 3, 1, 9]);
|
||||||
expect(count(range)).toEqual(4);
|
expect(count(range)).toEqual(4);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("Range#unique => SplitRange", () => {
|
||||||
|
makeSheet();
|
||||||
|
|
||||||
|
const origRange = R`A0:B`;
|
||||||
|
const uniqueRange = origRange.unique();
|
||||||
|
expect(uniqueRange.toString()).toEqual(
|
||||||
|
'SplitRange.fromNames("A0", "A1", "A2", "B1", "B2")'
|
||||||
|
);
|
||||||
|
|
||||||
|
const uniqueCount = count(uniqueRange);
|
||||||
|
// We expect that making a set (unique array) of the original range should equal the length of our unique range
|
||||||
|
expect(new Set(resolve(origRange)).size).toEqual(uniqueCount);
|
||||||
|
expect(uniqueCount).toEqual(5);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("R function", () => {
|
describe("R function", () => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue