Skip to content

Commit

Permalink
Remove splitWithLineBreaker and integrate its logic into split utilit…
Browse files Browse the repository at this point in the history
…y function
  • Loading branch information
WooJunKang committed Dec 5, 2023
1 parent 0525675 commit 33fec1d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 29 deletions.
11 changes: 2 additions & 9 deletions src/matrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,10 @@ describe("Matrix.split()", () => {
test("Constructs a matrix from a CSV string", () => {
expect(Matrix.split(CSV, Number)).toEqual(EXAMPLE_MATRIX);
});
});
describe("Matrix.splitWithLineBreaker()", () => {
it("Splits CSV string without line breaks correctly", () => {
const csv = "Value1\tValue2\tValue3";
const result = Matrix.splitWithLineBreaker(csv, (value) => value);
expect(result).toEqual([["Value1", "Value2", "Value3"]]);
});

it("Keeps line breaks inside double quotes", () => {
test("Keeps line breaks inside double quotes", () => {
const csv = '"Value\n1"\tValue2\t"Value\n3"';
const result = Matrix.splitWithLineBreaker(csv, (value) => value);
const result = Matrix.split(csv, (value) => value);
expect(result).toEqual([["Value\n1", "Value2", "Value\n3"]]);
});
});
Expand Down
16 changes: 0 additions & 16 deletions src/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,6 @@ export function split<T>(
transform: (value: string) => T,
horizontalSeparator = "\t",
verticalSeparator: string | RegExp = /\r\n|\n|\r/
): Matrix<T> {
return csv
.split(verticalSeparator)
.map((row) => row.split(horizontalSeparator).map(transform));
}

/**
* Parses a CSV string that may contain line breaks within double quotes.
* This function splits the CSV content into rows and columns, considering line breaks
* inside double quotes as part of the data rather than a new row.
*/
export function splitWithLineBreaker<T>(
csv: string,
transform: (value: string) => T,
horizontalSeparator = "\t",
verticalSeparator: string | RegExp = /\r\n|\n|\r/
): Matrix<T> {
// Temporarily replace line breaks inside quotes
const replaced = csv.replace(/"([^"]*?)"/g, (match, p1) => {
Expand Down
7 changes: 3 additions & 4 deletions src/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
EntireRowsSelection,
EntireWorksheetSelection,
} from "./selection";
import { hasLineBreaker, isActive } from "./util";
import { isActive } from "./util";
import * as Actions from "./actions";
import { Model, updateCellValue, createFormulaParser } from "./engine";

Expand Down Expand Up @@ -177,9 +177,8 @@ export default function reducer(
if (!active) {
return state;
}
const copied = hasLineBreaker(text)
? Matrix.splitWithLineBreaker(text, (value) => ({ value }))
: Matrix.split(text, (value) => ({ value }));

const copied = Matrix.split(text, (value) => ({ value }));
const copiedSize = Matrix.getSize(copied);

const selectedRange = state.selected.toRange(state.model.data);
Expand Down

0 comments on commit 33fec1d

Please sign in to comment.