Skip to content
Closed
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
8 changes: 6 additions & 2 deletions src/io/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -1669,13 +1669,13 @@ 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]);
return;
// =================================================
// OPTION 3: decide based on object...
default:
if (args[0] instanceof Array) {
fn.saveStrings(args[0], args[1], args[2]);
fn.saveStrings(args[0], args[1], args[2], args[3]);
} else if (args[0] instanceof p5.Table) {
fn.saveTable(args[0], args[1], args[2]);
} else if (args[0] instanceof p5.Image) {
Expand Down Expand Up @@ -1938,6 +1938,10 @@ function files(p5, fn) {
*/
fn.saveStrings = function (list, filename, extension, isCRLF) {
// p5._validateParameters('saveStrings', arguments);
if (typeof extension === 'boolean') {
isCRLF = extension;
extension = undefined;
}
const ext = extension || 'txt';
const pWriter = new p5.PrintWriter(filename, ext);
for (let item of list) {
Expand Down
33 changes: 33 additions & 0 deletions test/unit/io/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ suite('Files', function () {
expect(URL.createObjectURL).toHaveBeenCalledWith(saveData);
assert.equal(mockAnchorElement.download, 'myfile.txt');
});

test('should download a file with expected contents with CRLF when extension is omitted', async () => {
const strings = ['some', 'words'];
mockP5Prototype.saveStrings(strings, 'myfile.txt', true);

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

// saveJSON()
Expand Down Expand Up @@ -191,6 +202,28 @@ suite('Files', function () {
assert.equal(mockAnchorElement.download, 'filename.txt');
});

test('should download a text file with CRLF when isCRLF is passed as 4th arg', 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 text file with CRLF when isCRLF is passed as 3rd arg with extension in filename', 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