Skip to content
This repository was archived by the owner on May 30, 2019. It is now read-only.

Commit a017f86

Browse files
qti3ery
authored andcommitted
Optimize util.formatImageName
1 parent 6ffc728 commit a017f86

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/util.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,21 @@ export function formatImageName(filename: string, i?: number) {
217217
filename = `propel-${random}`;
218218
}
219219
// Force a valid file extension at the end of `filename` (default to .png)
220-
filename = filename
221-
.replace(/\..+[^(png|jpg|jpeg)]$|[^(png|jpg|jpeg)]$/i, "$&.png");
220+
const validExtensions = [".png", ".jpg", ".jpeg"];
221+
const tmp = filename.toLowerCase();
222+
let insert = true;
223+
for (const ext of validExtensions) {
224+
if (tmp.endsWith(ext)) {
225+
insert = false;
226+
break;
227+
}
228+
}
229+
if (insert) filename += ".png";
230+
222231
if (typeof i === "number") {
223-
return filename.replace(/\.(?=png$|jpe?g$).+/ig, `-${i}$&`);
232+
const index = filename.lastIndexOf(".");
233+
return filename.slice(0, index) + `-${i}` + filename.slice(index);
224234
}
235+
225236
return filename;
226237
}

0 commit comments

Comments
 (0)