From 8b49b5fa6a16de4a2f2417fef0d9adc50cd79068 Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Mon, 29 Apr 2019 07:03:59 -0700 Subject: [PATCH] Move getFileExtension to skinparserutils --- js/skinParser.js | 5 +++-- js/skinParserUtils.ts | 5 +++++ js/spinParserUtils.test.ts | 16 ++++++++++++++++ js/utils.test.ts | 16 ---------------- js/utils.ts | 5 ----- 5 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 js/spinParserUtils.test.ts diff --git a/js/skinParser.js b/js/skinParser.js index 13653778..eb0b7480 100644 --- a/js/skinParser.js +++ b/js/skinParser.js @@ -1,7 +1,7 @@ import SKIN_SPRITES from "./skinSprites"; import regionParser from "./regionParser"; import { LETTERS, DEFAULT_SKIN } from "./constants"; -import { parseViscolors, parseIni, getFileExtension, objectMap } from "./utils"; +import { parseViscolors, parseIni, objectMap } from "./utils"; import * as SkinParserUtils from "./skinParserUtils"; const shallowMerge = objs => @@ -65,7 +65,8 @@ async function genImgFromFilename(zip, fileName) { return null; } - const mimeType = `image/${getFileExtension(file.name) || "*"}`; + const mimeType = `image/${SkinParserUtils.getFileExtension(file.name) || + "*"}`; // The spec for createImageBitmap() says the browser should try to sniff the // mime type, but it looks like Firefox does not. So we specify it here // explicitly. diff --git a/js/skinParserUtils.ts b/js/skinParserUtils.ts index d3cb96d4..d3a7ebb0 100644 --- a/js/skinParserUtils.ts +++ b/js/skinParserUtils.ts @@ -1,6 +1,11 @@ import JSZip from "jszip"; import { Sprite } from "./skinSprites"; +export const getFileExtension = (fileName: string): string | null => { + const matches = /\.([a-z]{3,4})$/i.exec(fileName); + return matches ? matches[1].toLowerCase() : null; +}; + export async function getFileFromZip( zip: JSZip, fileName: string, diff --git a/js/spinParserUtils.test.ts b/js/spinParserUtils.test.ts new file mode 100644 index 00000000..ff0d6574 --- /dev/null +++ b/js/spinParserUtils.test.ts @@ -0,0 +1,16 @@ +import * as SkinParserUtils from "./skinParserUtils"; + +describe("getFileExtension", () => { + it("can get bmp", () => { + expect(SkinParserUtils.getFileExtension("foo.bmp")).toBe("bmp"); + }); + it("can match four char extension", () => { + expect(SkinParserUtils.getFileExtension("foo.html")).toBe("html"); + }); + it("converts to lower case", () => { + expect(SkinParserUtils.getFileExtension("foo.BMP")).toBe("bmp"); + }); + it("returns null if a match is not found", () => { + expect(SkinParserUtils.getFileExtension("foo")).toBe(null); + }); +}); diff --git a/js/utils.test.ts b/js/utils.test.ts index 8ea3bcb9..957a2abb 100644 --- a/js/utils.test.ts +++ b/js/utils.test.ts @@ -11,7 +11,6 @@ import { segment, moveSelected, spliceIn, - getFileExtension, makeCachingFilterFunction, replaceAtIndex, } from "./utils"; @@ -50,21 +49,6 @@ describe("getTimeStr", () => { }); }); -describe("getFileExtension", () => { - it("can get bmp", () => { - expect(getFileExtension("foo.bmp")).toBe("bmp"); - }); - it("can match four char extension", () => { - expect(getFileExtension("foo.html")).toBe("html"); - }); - it("converts to lower case", () => { - expect(getFileExtension("foo.BMP")).toBe("bmp"); - }); - it("returns null if a match is not found", () => { - expect(getFileExtension("foo")).toBe(null); - }); -}); - describe("clamp", () => { it("respects the max value", () => { const actual = clamp(101, 0, 100); diff --git a/js/utils.ts b/js/utils.ts index f6b3944e..6b2710fb 100644 --- a/js/utils.ts +++ b/js/utils.ts @@ -75,11 +75,6 @@ export const getTimeStr = ( ].join(""); }; -export const getFileExtension = (fileName: string): string | null => { - const matches = /\.([a-z]{3,4})$/i.exec(fileName); - return matches ? matches[1].toLowerCase() : null; -}; - export const parseViscolors = (text: string): string[] => { const entries = text.split("\n"); const regex = /^\s*(\d+)\s*,?\s*(\d+)\s*,?\s*(\d+)/;