Skip to content

Commit

Permalink
Merge pull request #249 from flatironinstitute/scripts-normalize-line…
Browse files Browse the repository at this point in the history
…-endings

Normalize newlines before passing to scripts
  • Loading branch information
WardBrian authored Dec 4, 2024
2 parents e1ccaad + b05d832 commit 75d50f9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion gui/src/app/Scripting/ScriptEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ColorOptions, ToolbarItem } from "@SpComponents/ToolBar";
import { FileNames } from "@SpCore/FileMapping";
import { ProjectContext } from "@SpCore/ProjectContextProvider";
import { ProjectKnownFiles } from "@SpCore/ProjectDataModel";
import { normalizeLineEndings } from "@SpUtil/normalizeLineEndings";
import {
FunctionComponent,
RefObject,
Expand Down Expand Up @@ -67,7 +68,7 @@ const ScriptEditor: FunctionComponent<ScriptEditorProps> = ({
}, [dataKey, update]);

const runCode = useCallback(() => {
onRun(content);
onRun(normalizeLineEndings(content));
}, [content, onRun]);

const unsavedChanges = useMemo(() => {
Expand Down
3 changes: 3 additions & 0 deletions gui/src/app/util/normalizeLineEndings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const normalizeLineEndings = (str: string) => {
return str.replace(/\r\n/g, "\n");
};
15 changes: 15 additions & 0 deletions gui/test/app/util/normalizeLineEndings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { normalizeLineEndings } from "@SpUtil/normalizeLineEndings";
import { describe, expect, test } from "vitest";

describe("normalizeLineEndings", () => {
test("should replace all CRLF with LF", () => {
const actual = normalizeLineEndings("foo\r\nbar\r\nbaz\r\n");
expect(actual).toBe("foo\nbar\nbaz\n");
});

test("leaves LF alone", () => {
const before = "foo\nbar\nbaz\n";
const actual = normalizeLineEndings(before);
expect(actual).toBe(before);
});
});

0 comments on commit 75d50f9

Please sign in to comment.