Skip to content

Commit f3639c2

Browse files
committed
Added function to generate output filenames
1 parent cb72d2b commit f3639c2

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { join } from "path";
2+
import { cwd } from "process";
3+
import { FileType } from "./file-type.enum";
4+
import { generateOutputPath } from "./generate-output-path.function";
5+
6+
describe("generate-output-path", () => {
7+
it("should default to a PNG file without pre- or postfix in the current directory", () => {
8+
// GIVEN
9+
const filename = "asdf";
10+
const ext = FileType.PNG;
11+
const expectedPath = join(cwd(), `${filename}${ext}`);
12+
13+
// WHEN
14+
const result = generateOutputPath(filename);
15+
16+
// THEN
17+
expect(result).toEqual(expectedPath);
18+
});
19+
20+
it("should should allow to add a prefix to the filename", () => {
21+
// GIVEN
22+
const filename = "asdf";
23+
const pre = "foo_";
24+
const ext = FileType.PNG;
25+
const expectedPath = join(cwd(), `${pre}${filename}${ext}`);
26+
27+
// WHEN
28+
const result = generateOutputPath(filename, {prefix: pre});
29+
30+
// THEN
31+
expect(result).toEqual(expectedPath);
32+
});
33+
34+
it("should should allow to add a postfix to the filename", () => {
35+
// GIVEN
36+
const filename = "asdf";
37+
const post = "_bar";
38+
const ext = FileType.PNG;
39+
const expectedPath = join(cwd(), `${filename}${post}${ext}`);
40+
41+
// WHEN
42+
const result = generateOutputPath(filename, {postfix: post});
43+
44+
// THEN
45+
expect(result).toEqual(expectedPath);
46+
});
47+
48+
it("should should allow to add both a prefix and a postfix to the filename", () => {
49+
// GIVEN
50+
const filename = "asdf";
51+
const pre = "foo_";
52+
const post = "_bar";
53+
const ext = FileType.PNG;
54+
const expectedPath = join(cwd(), `${pre}${filename}${post}${ext}`);
55+
56+
// WHEN
57+
const result = generateOutputPath(filename, {
58+
postfix: post,
59+
prefix: pre,
60+
});
61+
62+
// THEN
63+
expect(result).toEqual(expectedPath);
64+
});
65+
66+
it("should should allow to configure the file path", () => {
67+
// GIVEN
68+
const filename = "asdf";
69+
const filepath = "/foo/test/bar";
70+
const ext = FileType.PNG;
71+
const expectedPath = join(filepath, `${filename}${ext}`);
72+
73+
// WHEN
74+
const result = generateOutputPath(filename, {
75+
path: filepath,
76+
});
77+
78+
// THEN
79+
expect(result).toEqual(expectedPath);
80+
});
81+
82+
it("should handle relative file path", () => {
83+
// GIVEN
84+
const filename = "asdf";
85+
const filepath = "/foo/../bar";
86+
const ext = FileType.PNG;
87+
const expectedPath = join(filepath, `${filename}${ext}`);
88+
89+
// WHEN
90+
const result = generateOutputPath(filename, {
91+
path: filepath,
92+
});
93+
94+
// THEN
95+
expect(result).toEqual(expectedPath);
96+
});
97+
98+
it("should handle different file types", () => {
99+
// GIVEN
100+
const filename = "asdf";
101+
const ext = FileType.JPG;
102+
const expectedPath = join(cwd(), `${filename}${ext}`);
103+
104+
// WHEN
105+
const result = generateOutputPath(filename, {
106+
type: FileType.JPG
107+
});
108+
109+
// THEN
110+
expect(result).toEqual(expectedPath);
111+
});
112+
});

lib/generate-output-path.function.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { join, parse } from "path";
2+
import { cwd } from "process";
3+
import { FileType } from "./file-type.enum";
4+
5+
export const generateOutputPath = (
6+
filename: string,
7+
params?: {
8+
type?: FileType,
9+
path?: string,
10+
prefix?: string,
11+
postfix?: string
12+
}
13+
) => {
14+
const name = parse(filename).name;
15+
const imageType = (params && params.type) ? params.type : FileType.PNG;
16+
const path = (params && params.path) ? params.path : cwd();
17+
const prefix = (params && params.prefix) ? params.prefix : "";
18+
const postfix = (params && params.postfix) ? params.postfix : "";
19+
return join(path, `${prefix}${name}${postfix}${imageType}`);
20+
};

0 commit comments

Comments
 (0)