Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ describe('snapToCells', () => {
});

describe('auto mode', () => {
it('should snap both boundaries when cells are covered by more than 50%', () => {
it('should stretch appointments that occupy less than two cells', () => {
const items = [
{
cellIndex: 0, endCellIndex: 1, startDateUTC: 2, endDateUTC: 16, duration: 14,
cellIndex: 0, endCellIndex: 1, startDateUTC: 2, endDateUTC: 19, duration: 17,
},
];

Expand All @@ -79,30 +79,30 @@ describe('snapToCells', () => {
]);
});

it('should not snap boundary when cell is covered by less than 50%', () => {
it('should not stretch appointments that occupy exactly two cells', () => {
const items = [
{
cellIndex: 0, endCellIndex: 0, startDateUTC: 2, endDateUTC: 7, duration: 5,
cellIndex: 0, endCellIndex: 2, startDateUTC: 1, endDateUTC: 21, duration: 20,
},
];

expect(snapToCells(items as any, cells, 'auto')).toEqual([
expect.objectContaining({
startDateUTC: 2, endDateUTC: 7, duration: 5,
startDateUTC: 1, endDateUTC: 21, duration: 20,
}),
]);
});

it('should not snap boundary when cell is covered by exactly 50%', () => {
it('should not stretch appointments that occupy more than two cells', () => {
const items = [
{
cellIndex: 0, endCellIndex: 0, startDateUTC: 0, endDateUTC: 5, duration: 5,
cellIndex: 0, endCellIndex: 2, startDateUTC: 2, endDateUTC: 26, duration: 24,
},
];

expect(snapToCells(items as any, cells, 'auto')).toEqual([
expect.objectContaining({
startDateUTC: 0, endDateUTC: 5, duration: 5,
startDateUTC: 2, endDateUTC: 26, duration: 24,
}),
]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@ import type {
CellInterval, ListEntity, Position,
} from '../../types';

const getCellFill = (
startDateUTC: number,
endDateUTC: number,
cell: CellInterval,
): number => {
const cellDuration = cell.max - cell.min;
if (cellDuration <= 0) return 0;

const overlapStart = Math.max(startDateUTC, cell.min);
const overlapEnd = Math.min(endDateUTC, cell.max);
const overlapDuration = Math.max(0, overlapEnd - overlapStart);

return overlapDuration / cellDuration;
};

export const snapToCells = <T extends ListEntity & Position>(
entities: T[],
cells: CellInterval[],
Expand All @@ -29,14 +14,13 @@ export const snapToCells = <T extends ListEntity & Position>(
return entities.map((entity) => {
const startCell = cells[entity.cellIndex];
const endCell = cells[entity.endCellIndex];
const cellDuration = startCell.max - startCell.min;
const appointmentDuration = entity.endDateUTC - entity.startDateUTC;
const isLessThanTwoCells = cellDuration > 0 && appointmentDuration / cellDuration < 2;
const shouldSnap = mode === 'always' || isLessThanTwoCells;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: snapStart and snapEnd are always identical now. Can simplify to one boolean:

const shouldSnap = mode === 'always' || isLessThanTwoCells;
return { snapStart: shouldSnap, snapEnd: shouldSnap };

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice suggestion. Applied

const snapStart = mode === 'always'
|| getCellFill(entity.startDateUTC, entity.endDateUTC, startCell) > 0.5;
const snapEnd = mode === 'always'
|| getCellFill(entity.startDateUTC, entity.endDateUTC, endCell) > 0.5;

const startDateUTC = snapStart ? startCell.min : entity.startDateUTC;
const endDateUTC = snapEnd ? endCell.max : entity.endDateUTC;
const startDateUTC = shouldSnap ? startCell.min : entity.startDateUTC;
const endDateUTC = shouldSnap ? endCell.max : entity.endDateUTC;

return {
...entity,
Expand Down
Loading