Skip to content
Permalink
Browse files Browse the repository at this point in the history
validate filename
  • Loading branch information
Robert Kesterson committed Jan 22, 2021
1 parent 6383ca8 commit 23671d4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
34 changes: 32 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -54,6 +54,7 @@
"typescript": "^4.0.5"
},
"dependencies": {
"filenamify": "^4.2.0",
"lodash": "^4.17.15"
}
}
6 changes: 6 additions & 0 deletions src/index.ts
Expand Up @@ -354,11 +354,17 @@ function plotCallack(options: PlotOptions) {
gnuplot.stdin.end();
}

import validFilename from "filenamify";

/**
* Plots data to a PDF file. If it does not exist, the PDF file will
* be created, otherwise this plot will be appended as a new page.
*/
export function plot(options: PlotOptions): Promise<boolean> | void {
const f = validFilename.path(options.filename);
if (f !== options.filename) {
throw new Error(`invalid filename of '${options.filename}' -- ${f}`);
}
if (options.finish) {
return plotCallack(options);
}
Expand Down
17 changes: 17 additions & 0 deletions src/test.ts
Expand Up @@ -6,6 +6,7 @@ import "mocha";

import { plot } from "./index";
import { expect as should } from "chai";
import { fail } from "assert";

function handleResult(
error: any,
Expand Down Expand Up @@ -55,6 +56,22 @@ describe("Plot tests", function () {
// });

describe("PNG output", function () {
it("bad filename ", async () => {
let passed = false;
try {
const ploted = await plot({
data: [3, 1, 2, 3, 4],
filename: __dirname + "/test/output1.png & frog > frog.txt",
format: "png",
});
} catch (e) {
should(e.message).contain("invalid filename of");
passed = true;
}
if (!passed) {
fail();
}
});
it("Async Output1", async () => {
const ploted = await plot({
data: [3, 1, 2, 3, 4],
Expand Down

0 comments on commit 23671d4

Please sign in to comment.