Skip to content

Commit 0b1a08d

Browse files
authored
Merge pull request #197 from mathieudutour/patch-1
add stripQuery method and strip the query from the extension
2 parents e274bf7 + 27d3364 commit 0b1a08d

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/util/url.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,25 @@ exports.getProtocol = function getProtocol (path) {
6969
exports.getExtension = function getExtension (path) {
7070
let lastDot = path.lastIndexOf(".");
7171
if (lastDot >= 0) {
72-
return path.substr(lastDot).toLowerCase();
72+
return url.stripQuery(path.substr(lastDot).toLowerCase());
7373
}
7474
return "";
7575
};
7676

77+
/**
78+
* Removes the query, if any, from the given path.
79+
*
80+
* @param {string} path
81+
* @returns {string}
82+
*/
83+
exports.stripQuery = function stripQuery (path) {
84+
let queryIndex = path.indexOf("?");
85+
if (queryIndex >= 0) {
86+
path = path.substr(0, queryIndex);
87+
}
88+
return path;
89+
};
90+
7791
/**
7892
* Returns the hash (URL fragment), of the given path.
7993
* If there is no hash, then the root hash ("#") is returned.

test/specs/util/url.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"use strict";
2+
3+
const chai = require("chai");
4+
const chaiSubset = require("chai-subset");
5+
chai.use(chaiSubset);
6+
const { expect } = chai;
7+
const $url = require("../../../lib/util/url");
8+
9+
describe("Return the extension of a URL", () => {
10+
it("should return an empty string if there isn't any extension", async () => {
11+
const extension = $url.getExtension("/file");
12+
expect(extension).to.equal("");
13+
});
14+
15+
it("should return the extension in lowercase", async () => {
16+
const extension = $url.getExtension("/file.YML");
17+
expect(extension).to.equal(".yml");
18+
});
19+
20+
it("should return the extension without the query", async () => {
21+
const extension = $url.getExtension("/file.yml?foo=bar");
22+
expect(extension).to.equal(".yml");
23+
});
24+
});

0 commit comments

Comments
 (0)