Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/io/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,7 @@ function files(p5, fn) {
fn.saveJSON(args[0], args[1], args[2]);
return;
case 'txt':
fn.saveStrings(args[0], args[1], args[2]);
fn.saveStrings(args[0], args[1], args[2], args[3]); // Takes the third argument
return;
// =================================================
// OPTION 3: decide based on object...
Expand Down
13 changes: 12 additions & 1 deletion test/unit/io/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { vi } from 'vitest';
const mockAnchorElement = vi.mockObject({
href: null,
download: null,
click: () => {}
click: () => { }
});
const originalCreateElement = document.createElement;
vi.spyOn(document, 'createElement').mockImplementation((...args) => {
Expand Down Expand Up @@ -191,6 +191,17 @@ suite('Files', function () {
assert.equal(mockAnchorElement.download, 'filename.txt');
});

test('should preserve CRLF when saving a text file', async () => {
const myStrings = ['aaa', 'bbb'];
mockP5Prototype.save(myStrings, 'filename', 'txt', true);

const saveData = new Blob([myStrings.join('\r\n')]);
expect(document.createElement).toHaveBeenCalledTimes(1);
expect(mockAnchorElement.click).toHaveBeenCalledTimes(1);
expect(URL.createObjectURL).toHaveBeenCalledWith(saveData);
assert.equal(mockAnchorElement.download, 'filename.txt');
});

test('should download a json file', async () => {
const myObj = { hi: 'hello' };
mockP5Prototype.save(myObj, 'filename.json');
Expand Down