Skip to content

Commit 4074130

Browse files
committed
[REF] zones helper: remove unused helpers
closes #4134 Task: 3893550 Signed-off-by: Rémi Rahir (rar) <[email protected]>
1 parent e7f9ebe commit 4074130

File tree

2 files changed

+1
-222
lines changed

2 files changed

+1
-222
lines changed

src/helpers/zones.ts

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -384,82 +384,6 @@ export function isZoneInside(smallZone: Zone, biggerZone: Zone): boolean {
384384
return isEqual(union(biggerZone, smallZone), biggerZone);
385385
}
386386

387-
/**
388-
* Merge aligned adjacent columns into single zones
389-
* e.g. A1:A5 and B1:B5 are merged into A1:B5
390-
*/
391-
export function mergeAlignedColumns(columns: readonly Zone[]): Zone[] {
392-
if (columns.length === 0) {
393-
return [];
394-
}
395-
if (columns.some((zone) => zone.left !== zone.right)) {
396-
throw new Error("only columns can be merged");
397-
}
398-
const done: Zone[] = [];
399-
const cols = removeRedundantZones(columns);
400-
401-
const isAdjacentAndAligned = (zone, nextZone) =>
402-
zone.top === nextZone.top &&
403-
zone.bottom === nextZone.bottom &&
404-
zone.right + 1 === nextZone.left;
405-
while (cols.length) {
406-
const merged = cols.reduce(
407-
(zone, nextZone) => (isAdjacentAndAligned(zone, nextZone) ? union(zone, nextZone) : zone),
408-
cols.shift()!
409-
);
410-
done.push(merged);
411-
}
412-
return removeRedundantZones(done);
413-
}
414-
415-
/**
416-
* Remove redundant zones in the list.
417-
* i.e. zones included in another zone.
418-
*/
419-
function removeRedundantZones(zones: readonly Zone[]): Zone[] {
420-
const sortedZones = [...zones]
421-
.sort((a, b) => b.right - a.right)
422-
.sort((a, b) => b.bottom - a.bottom)
423-
.sort((a, b) => a.top - b.top)
424-
.sort((a, b) => a.left - b.left)
425-
.reverse();
426-
const checked: Zone[] = [];
427-
while (sortedZones.length !== 0) {
428-
const zone = sortedZones.shift()!;
429-
const isIncludedInOther = sortedZones.some((otherZone) => isZoneInside(zone, otherZone));
430-
if (!isIncludedInOther) {
431-
checked.push(zone);
432-
}
433-
}
434-
return checked.reverse();
435-
}
436-
437-
/**
438-
* Merge adjacent positions into vertical zones (columns)
439-
*/
440-
export function mergePositionsIntoColumns(positions: readonly Position[]): Zone[] {
441-
if (positions.length === 0) {
442-
return [];
443-
}
444-
const [startingPosition, ...sortedPositions] = [...positions]
445-
.sort((a, b) => a.row - b.row)
446-
.sort((a, b) => a.col - b.col);
447-
const done: Zone[] = [];
448-
let active = positionToZone(startingPosition);
449-
for (const { col, row } of sortedPositions) {
450-
if (isInside(col, row, active)) {
451-
continue;
452-
} else if (col === active.left && row === active.bottom + 1) {
453-
const bottom = active.bottom + 1;
454-
active = { ...active, bottom };
455-
} else {
456-
done.push(active);
457-
active = positionToZone({ col, row });
458-
}
459-
}
460-
return [...done, active];
461-
}
462-
463387
export function zoneToDimension(zone: Zone): ZoneDimension {
464388
return {
465389
numberOfRows: zone.bottom - zone.top + 1,

tests/helpers/zones_helpers.test.ts

Lines changed: 1 addition & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import {
22
createAdaptedZone,
33
isZoneValid,
4-
mergeAlignedColumns,
5-
mergePositionsIntoColumns,
64
overlap,
75
positions,
86
toCartesian,
97
toUnboundedZone,
108
toZone,
119
} from "../../src/helpers/index";
12-
import { Position, Zone } from "../../src/types";
13-
import { target } from "../test_helpers/helpers";
10+
import { Zone } from "../../src/types";
1411

1512
describe("overlap", () => {
1613
test("one zone above the other", () => {
@@ -43,148 +40,6 @@ describe("isZoneValid", () => {
4340
});
4441
});
4542

46-
describe("mergePositionsIntoColumns", () => {
47-
function p(xc: string): Position {
48-
const zone = toZone(xc);
49-
return { col: zone.left, row: zone.top };
50-
}
51-
const A1 = p("A1");
52-
const A2 = p("A2");
53-
const A3 = p("A3");
54-
const B1 = p("B1");
55-
const B2 = p("B2");
56-
const C1 = p("C1");
57-
const C2 = p("C2");
58-
test("no zone", () => {
59-
expect(mergePositionsIntoColumns([])).toEqual([]);
60-
});
61-
62-
test("a single zone", () => {
63-
expect(mergePositionsIntoColumns([A1])).toEqual(target("A1"));
64-
});
65-
66-
test("a duplicated zone", () => {
67-
expect(mergePositionsIntoColumns([A1, A1])).toEqual(target("A1"));
68-
});
69-
70-
test("two non-adjacent positions on the same column", () => {
71-
expect(mergePositionsIntoColumns([A1, A3])).toEqual(target("A1, A3"));
72-
});
73-
74-
test("two non-adjacent positions on the same row", () => {
75-
expect(mergePositionsIntoColumns([A1, C1])).toEqual(target("A1, C1"));
76-
});
77-
78-
test("two adjacent positions on the same column", () => {
79-
expect(mergePositionsIntoColumns([A1, A2])).toEqual(target("A1:A2"));
80-
});
81-
82-
test("two adjacent positions on the same row", () => {
83-
expect(mergePositionsIntoColumns([A1, B1])).toEqual(target("A1, B1"));
84-
});
85-
86-
test("four adjacent positions on different columns", () => {
87-
expect(mergePositionsIntoColumns([C2, C1, A1, A2])).toEqual(target("A1:A2, C1:C2"));
88-
});
89-
90-
test("four adjacent positions on adjacent columns", () => {
91-
expect(mergePositionsIntoColumns([C2, C1, B2, B1])).toEqual(target("B1:B2, C1:C2"));
92-
});
93-
});
94-
95-
describe("mergeAlignedColumns", () => {
96-
test("no column", () => {
97-
expect(mergeAlignedColumns([])).toEqual([]);
98-
});
99-
test("a row", () => {
100-
expect(() => mergeAlignedColumns(target("A1:B1"))).toThrow();
101-
expect(() => mergeAlignedColumns(target("A1:A2, A1:B1"))).toThrow();
102-
});
103-
test("a single column", () => {
104-
expect(mergeAlignedColumns(target("A1:A2"))).toEqual(target("A1:A2"));
105-
});
106-
107-
test("two zones on the same column", () => {
108-
expect(mergeAlignedColumns(target("A1:A2, A3:A4"))).toEqual(target("A1:A2, A3:A4"));
109-
});
110-
111-
test("duplicated columns", () => {
112-
expect(mergeAlignedColumns(target("A1:A2, A1:A2"))).toEqual(target("A1:A2"));
113-
});
114-
115-
test("two adjacent zones on the same column", () => {
116-
expect(mergeAlignedColumns(target("A1:A2, A2:A4"))).toEqual(target("A1:A2, A2:A4"));
117-
});
118-
test("two aligned zones on non-adjacent columns", () => {
119-
expect(mergeAlignedColumns(target("A1:A2, C1:C2"))).toEqual(target("A1:A2, C1:C2"));
120-
});
121-
122-
test("two non-aligned zones on adjacent columns", () => {
123-
expect(mergeAlignedColumns(target("A1:A2, B1:B3"))).toEqual(target("A1:A2, B1:B3"));
124-
});
125-
126-
test("two aligned zones on adjacent columns", () => {
127-
expect(mergeAlignedColumns(target("A1:A2, B1:B2"))).toEqual(target("A1:B2"));
128-
});
129-
130-
test("three aligned zones on adjacent columns", () => {
131-
expect(mergeAlignedColumns(target("A1:A2, B1:B2, C1:C2"))).toEqual(target("A1:C2"));
132-
});
133-
134-
test("three aligned zones on adjacent columns", () => {
135-
expect(mergeAlignedColumns(target("A1:A2, B1:B2, C1:C2"))).toEqual(target("A1:C2"));
136-
});
137-
138-
test("two aligned zones on adjacent and one on non-adjacent columns", () => {
139-
expect(mergeAlignedColumns(target("A1:A2, B1:B2, D1:D2"))).toEqual(target("A1:B2, D1:D2"));
140-
expect(mergeAlignedColumns(target("A1:A2, D1:D2, B1:B2"))).toEqual(target("A1:B2, D1:D2"));
141-
expect(mergeAlignedColumns(target("A1:A2, A3:A4, B3:B4"))).toEqual(target("A1:A2, A3:B4"));
142-
expect(mergeAlignedColumns(target("A3:A4, A1:A2, B3:B4"))).toEqual(target("A1:A2, A3:B4"));
143-
});
144-
test("two overlapping columns with one aligned column", () => {
145-
expect(mergeAlignedColumns(target("A1:A2, A2:A3, B2:B3"))).toEqual(target("A1:A2, A2:B3"));
146-
expect(mergeAlignedColumns(target("A2:A3, A1:A2, B2:B3"))).toEqual(target("A1:A2, A2:B3"));
147-
148-
expect(mergeAlignedColumns(target("A1:A2, A2:A3, B1:B2"))).toEqual(target("A1:B2, A2:A3"));
149-
expect(mergeAlignedColumns(target("A2:A3, A1:A2, B1:B2"))).toEqual(target("A1:B2, A2:A3"));
150-
});
151-
test("two overlapping columns with two aligned column", () => {
152-
expect(mergeAlignedColumns(target("A1:A2, A2:A3, B2:B3, C2:C3"))).toEqual(
153-
target("A1:A2, A2:C3")
154-
);
155-
expect(mergeAlignedColumns(target("B2:B3, C2:C3, A1:A2, A2:A3"))).toEqual(
156-
target("A1:A2, A2:C3")
157-
);
158-
159-
expect(mergeAlignedColumns(target("A1:A2, A2:A3, B1:B2, C1:C2"))).toEqual(
160-
target("A1:C2, A2:A3")
161-
);
162-
expect(mergeAlignedColumns(target("C1:C2, A1:A2, A2:A3, B1:B2"))).toEqual(
163-
target("A1:C2, A2:A3")
164-
);
165-
});
166-
test("one column inside another with zones on the right", () => {
167-
expect(mergeAlignedColumns(target("A1:A4, A2:A3"))).toEqual(target("A1:A4"));
168-
expect(mergeAlignedColumns(target("A1:A4, A2:A3, B1:B4"))).toEqual(target("A1:B4"));
169-
expect(mergeAlignedColumns(target("A2:A3, A1:A4, B1:B4"))).toEqual(target("A1:B4"));
170-
171-
expect(mergeAlignedColumns(target("A1:A4, A2:A3, B2:B3"))).toEqual(target("A1:A4, B2:B3"));
172-
expect(mergeAlignedColumns(target("A2:A3, A1:A4, B2:B3"))).toEqual(target("A1:A4, B2:B3"));
173-
174-
expect(mergeAlignedColumns(target("A1:A4, A1:A3, B1:B3"))).toEqual(target("A1:A4, B1:B3"));
175-
expect(mergeAlignedColumns(target("A1:A3, A1:A4, B1:B3"))).toEqual(target("A1:A4, B1:B3"));
176-
});
177-
test("one column inside another with zones on the left", () => {
178-
expect(mergeAlignedColumns(target("B1:B3, B1:B4, A1:A3"))).toEqual(target("A1:A3, B1:B4"));
179-
expect(mergeAlignedColumns(target("B1:B3, B1:B4, A1:A4"))).toEqual(target("A1:B4"));
180-
});
181-
test("two aligned respectively with one other", () => {
182-
expect(mergeAlignedColumns(target("A1:A2, A3:A4, B1:B2, B3:B4"))).toEqual(
183-
target("A1:B2, A3:B4")
184-
);
185-
});
186-
});
187-
18843
describe("toZone", () => {
18944
test.each([["A1"], ["$A1"], ["A$1"], ["$A$1"], ["Sheet1!A1"], ["Sheet1!$A$1"]])(
19045
"should support different simple cell reference",

0 commit comments

Comments
 (0)