Skip to content

Commit

Permalink
feat: rename
Browse files Browse the repository at this point in the history
  • Loading branch information
weird94 committed Sep 19, 2024
1 parent 8364b0b commit dfeaad7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
12 changes: 6 additions & 6 deletions packages/core/src/shared/__tests__/rectangle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('multiSubtractMulti', () => {
{ startColumn: 2, endColumn: 4, startRow: 2, endRow: 4 },
];
const expected: IRange[] = multiSubtractMultiRanges(ranges1, ranges2);
expect(rangesToMatrix(Rectangle.multiSubtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected));
expect(rangesToMatrix(Rectangle.subtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected));
});

it('should handle subtracting multiple ranges from a single range', () => {
Expand All @@ -147,7 +147,7 @@ describe('multiSubtractMulti', () => {
{ startColumn: 6, endColumn: 8, startRow: 6, endRow: 8 },
];
const expected: IRange[] = multiSubtractMultiRanges(ranges1, ranges2);
expect(rangesToMatrix(Rectangle.multiSubtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected));
expect(rangesToMatrix(Rectangle.subtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected));
});

it('should handle non-overlapping subtraction ranges', () => {
Expand All @@ -158,7 +158,7 @@ describe('multiSubtractMulti', () => {
{ startColumn: 6, endColumn: 8, startRow: 6, endRow: 8 },
];
const expected: IRange[] = multiSubtractMultiRanges(ranges1, ranges2);
expect(rangesToMatrix(Rectangle.multiSubtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected));
expect(rangesToMatrix(Rectangle.subtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected));
});

it('should handle subtraction ranges that completely overlap', () => {
Expand All @@ -169,7 +169,7 @@ describe('multiSubtractMulti', () => {
{ startColumn: 1, endColumn: 5, startRow: 1, endRow: 5 },
];
const expected: IRange[] = multiSubtractMultiRanges(ranges1, ranges2);
expect(rangesToMatrix(Rectangle.multiSubtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected));
expect(rangesToMatrix(Rectangle.subtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected));
});

it('should handle empty ranges', () => {
Expand All @@ -178,7 +178,7 @@ describe('multiSubtractMulti', () => {
{ startColumn: 2, endColumn: 4, startRow: 2, endRow: 4 },
];
const expected: IRange[] = multiSubtractMultiRanges(ranges1, ranges2);
expect(rangesToMatrix(Rectangle.multiSubtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected));
expect(rangesToMatrix(Rectangle.subtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected));
});

it('should handle empty subtraction ranges', () => {
Expand All @@ -187,6 +187,6 @@ describe('multiSubtractMulti', () => {
];
const ranges2: IRange[] = [];
const expected: IRange[] = multiSubtractMultiRanges(ranges1, ranges2);
expect(rangesToMatrix(Rectangle.multiSubtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected));
expect(rangesToMatrix(Rectangle.subtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected));
});
});
7 changes: 7 additions & 0 deletions packages/core/src/shared/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,10 @@ export function mergeRanges(ranges: IRange[]): IRange[] {
// return horizontalMerged;
}

export function multiSubtractSingleRange(ranges: IRange[], toDelete: IRange) {
const res: IRange[] = [];
ranges.forEach((range) => {
res.push(...Rectangle.subtract(range, toDelete));
});
return Rectangle.mergeRanges(res);
};
14 changes: 3 additions & 11 deletions packages/core/src/shared/rectangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { AbsoluteRefType, type IRange, type IRectLTRB, RANGE_TYPE } from '../sheets/typedef';
import { mergeRanges } from './range';
import { mergeRanges, multiSubtractSingleRange } from './range';
import type { Nullable } from './types';

/**
Expand Down Expand Up @@ -323,22 +323,14 @@ export class Rectangle {
return mergeRanges(ranges);
}

static multiSubtractSingle(ranges: IRange[], toDelete: IRange) {
const res: IRange[] = [];
ranges.forEach((range) => {
res.push(...Rectangle.subtract(range, toDelete));
});
return Rectangle.mergeRanges(res);
};

static multiSubtractMulti(ranges1: IRange[], ranges2: IRange[]): IRange[] {
static subtractMulti(ranges1: IRange[], ranges2: IRange[]): IRange[] {
if (!ranges2.length) {
return ranges1;
}

let res: IRange[] = ranges1;
ranges2.forEach((range) => {
res = Rectangle.multiSubtractSingle(res, range);
res = multiSubtractSingleRange(res, range);
});

return Rectangle.mergeRanges(res);
Expand Down
8 changes: 4 additions & 4 deletions packages/sheets-data-validation/src/models/rule-matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class RuleMatrix {
const ranges = rule.ranges.map((range) => Range.transformRange(range, this._worksheet));

this._map.forEach((value, key) => {
const newRanges = Rectangle.multiSubtractMulti(value, ranges);
const newRanges = Rectangle.subtractMulti(value, ranges);
if (newRanges.length === 0) {
this._map.delete(key);
} else {
Expand All @@ -60,7 +60,7 @@ export class RuleMatrix {
removeRange(_ranges: IRange[]) {
const ranges = _ranges.map((range) => Range.transformRange(range, this._worksheet));
this._map.forEach((value, key) => {
const newRanges = Rectangle.multiSubtractMulti(value, ranges);
const newRanges = Rectangle.subtractMulti(value, ranges);
if (newRanges.length === 0) {
this._map.delete(key);
} else {
Expand All @@ -77,7 +77,7 @@ export class RuleMatrix {
this._map.delete(ruleId);
const ranges = _newRanges.map((range) => Range.transformRange(range, this._worksheet));
this._map.forEach((value, key) => {
const newRanges = Rectangle.multiSubtractMulti(value, ranges);
const newRanges = Rectangle.subtractMulti(value, ranges);
if (newRanges.length === 0) {
this._map.delete(key);
} else {
Expand Down Expand Up @@ -190,7 +190,7 @@ export class RuleMatrix {
if (key === ruleId) {
return;
}
const newRanges = Rectangle.multiSubtractMulti(value, ranges);
const newRanges = Rectangle.subtractMulti(value, ranges);
if (newRanges.length === 0) {
this._map.delete(key);
} else {
Expand Down

0 comments on commit dfeaad7

Please sign in to comment.