Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support extensions containing dots
Also, allow specifying extensions without the initial dot. Fixes #127
- Loading branch information
1 parent
930d8fd
commit 2f7e315
Showing
3 changed files
with
78 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"use strict" | ||
|
||
const getFileMode = require("../getFileMode") | ||
|
||
test("undefined if filename is empty", () => { | ||
expect( | ||
getFileMode({ htmlExtensions: [], xmlExtensions: [] }, undefined) | ||
).toBe(undefined) | ||
}) | ||
|
||
test("'html' if filename matches an HTML extension", () => { | ||
expect( | ||
getFileMode({ htmlExtensions: ["html"], xmlExtensions: [] }, "foo.html") | ||
).toBe("html") | ||
}) | ||
|
||
test("'html' if filename matches an HTML and an XML extension", () => { | ||
expect( | ||
getFileMode( | ||
{ htmlExtensions: ["html"], xmlExtensions: ["html"] }, | ||
"foo.html" | ||
) | ||
).toBe("html") | ||
}) | ||
|
||
test("'xml' if filename matches an XML extension", () => { | ||
expect( | ||
getFileMode({ htmlExtensions: ["html"], xmlExtensions: ["xml"] }, "foo.xml") | ||
).toBe("xml") | ||
}) | ||
|
||
test("undefined if filename ends with extension without dot", () => { | ||
expect( | ||
getFileMode({ htmlExtensions: [], xmlExtensions: ["xml"] }, "fooxml") | ||
).toBe(undefined) | ||
}) | ||
|
||
test("works with extensions starting with a dot", () => { | ||
expect( | ||
getFileMode({ htmlExtensions: [".html"], xmlExtensions: [] }, "foo.html") | ||
).toBe("html") | ||
}) | ||
|
||
test("works with extensions containgin a dot", () => { | ||
expect( | ||
getFileMode( | ||
{ htmlExtensions: [".html.in"], xmlExtensions: [] }, | ||
"foo.html.in" | ||
) | ||
).toBe("html") | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"use strict" | ||
|
||
module.exports = function getMode(pluginSettings, filenameOrOptions) { | ||
const filename = | ||
typeof filenameOrOptions === "object" | ||
? filenameOrOptions.filename | ||
: filenameOrOptions | ||
|
||
if (!filename) { | ||
return | ||
} | ||
if (pluginSettings.htmlExtensions.some(hasExtension)) { | ||
return "html" | ||
} | ||
if (pluginSettings.xmlExtensions.some(hasExtension)) { | ||
return "xml" | ||
} | ||
|
||
function hasExtension(extension) { | ||
if (!extension.startsWith(".")) { | ||
extension = `.${extension}` | ||
} | ||
return filename.endsWith(extension) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters