Skip to content

Commit

Permalink
Fix the problem when copy paste the number of columns are not increased.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sun Yi committed Aug 2, 2022
1 parent 3a7a907 commit f9fedab
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/matrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,39 @@ describe("Matrix.padRows()", () => {
});
});

describe("Matrix.pad()", () => {
test("Pads matrix with empty columns and columns to match given size", () => {
expect(Matrix.pad(EXAMPLE_MATRIX, { rows: 5, columns: 4 })).toEqual([
[1, 2, 3, undefined],
[4, 5, 6, undefined],
[7, 8, 9, undefined],
[undefined, undefined, undefined, undefined],
[undefined, undefined, undefined, undefined],
]);
});
test("Pads rows only.", () => {
expect(Matrix.pad(EXAMPLE_MATRIX, { rows: 5, columns: 3 })).toEqual([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[undefined, undefined, undefined],
[undefined, undefined, undefined],
]);
});
test("Pads columns only.", () => {
expect(Matrix.pad(EXAMPLE_MATRIX, { rows: 2, columns: 5 })).toEqual([
[1, 2, 3, undefined, undefined],
[4, 5, 6, undefined, undefined],
[7, 8, 9, undefined, undefined],
]);
});
test("Does nothing if matrix is large enough.", () => {
expect(Matrix.pad(EXAMPLE_MATRIX, { rows: 3, columns: 2 })).toBe(
EXAMPLE_MATRIX
);
});
});

describe("Matrix.toArray()", () => {
const flattedMatrix = [
...EXAMPLE_MATRIX[0],
Expand Down
37 changes: 37 additions & 0 deletions src/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,43 @@ export function padRows<T>(matrix: Matrix<T>, totalRows: number): Matrix<T> {
return [...matrix, ...emptyRows];
}

/**
* Pads matrix with empty columns to match given total columns
* @param matrix - matrix to pad
* @param size - minimum size of the matrix after padding.
* @returns the updated matrix
*/
export function pad<T>(matrix: Matrix<T>, size: Size): Matrix<T> {
const { rows, columns } = getSize(matrix);

if (rows >= size.rows && columns >= size.columns) {
// Optimization, no padding required.
return matrix;
}

const resultSize: Size = {
rows: size.rows > rows ? size.rows : rows,
columns: size.columns > columns ? size.columns : columns,
};

let padded = [...matrix];
if (resultSize.columns > columns) {
const padColumns = resultSize.columns - columns;
padded = padded.map((row) => [
...row,
...Array(padColumns).fill(undefined),
]);
}

if (resultSize.rows > rows) {
const padRows = resultSize.rows - rows;
const emptyRow = Array(resultSize.columns).fill(undefined);
padded = [...padded, ...Array(padRows).fill(emptyRow)];
}

return padded;
}

export function toArray<T>(matrix: Matrix<T>): T[];
export function toArray<T1, T2>(
matrix: Matrix<T1>,
Expand Down
7 changes: 5 additions & 2 deletions src/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,11 @@ const reducer = createReducer(INITIAL_STATE, (builder) => {
};

const copiedSize = Matrix.getSize(copiedMatrix);
const requiredRows = active.row + copiedSize.rows;
const paddedData = Matrix.padRows(state.data, requiredRows);
const requiredSize: Matrix.Size = {
rows: active.row + copiedSize.rows,
columns: active.column + copiedSize.columns,
};
const paddedData = Matrix.pad(state.data, requiredSize);

const { data, commit } = PointMap.reduce<Accumulator, Types.CellBase>(
(acc, value, point) => {
Expand Down

0 comments on commit f9fedab

Please sign in to comment.