Skip to content

Commit b5f72fd

Browse files
committed
Utils refactoring
1 parent 7441380 commit b5f72fd

File tree

1 file changed

+12
-35
lines changed

1 file changed

+12
-35
lines changed

lib/utils.js

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,22 @@
1-
var Url = require('url');
2-
3-
function trimUrl(url) {
4-
var questionMarkIndex = url.indexOf('?');
5-
if (questionMarkIndex > 0) {
6-
url = url.substring(0, questionMarkIndex);
7-
}
8-
return url;
9-
}
1+
var url = require('url');
102

113
function trimFilename(filename) {
12-
var questionMarkIndex = filename.indexOf('?'),
13-
hashIndex = filename.indexOf('#'),
14-
indexToCut = filename.length;
15-
16-
if (questionMarkIndex >= 0 && hashIndex >= 0) {
17-
indexToCut = questionMarkIndex < hashIndex ? questionMarkIndex : hashIndex
18-
} else if (questionMarkIndex >= 0) {
19-
indexToCut = questionMarkIndex;
20-
} else if (hashIndex >= 0) {
21-
indexToCut = hashIndex;
22-
}
23-
24-
return filename.substring(0, indexToCut);
4+
var queryRegexp = /[\?#](.*)$/;
5+
return filename.replace(queryRegexp, '');
256
}
267

278
function isPathAbsolute(path) {
28-
var absolutePathRegexp = /^http:\/|^https:\/|^\/\//;
9+
var absolutePathRegexp = /^(http:\/|https:\/|\/\/)/;
2910
return absolutePathRegexp.test(path);
3011
}
3112

32-
function getAbsolutePath(curPath, link) {
33-
if (isPathAbsolute(link)) {
34-
var absolutePathNoProtocolRegexp = /^\/\//;
35-
if (absolutePathNoProtocolRegexp.test(link)) {
36-
link = 'http:' + link;
37-
}
38-
return link;
39-
} else {
40-
return Url.resolve(curPath, link);
13+
function getAbsolutePath(currentUrl, path) {
14+
var pathObj = url.parse(path);
15+
if (isPathAbsolute(path) && !pathObj.protocol) {
16+
pathObj.protocol = 'http';
17+
path = url.format(pathObj);
4118
}
19+
return url.resolve(currentUrl, path);
4220
}
4321

4422
function pathToUnixFormat(filepath) {
@@ -47,12 +25,11 @@ function pathToUnixFormat(filepath) {
4725

4826
// Check if path contains base64 encoded image
4927
function isEmbeddedSource(path) {
50-
var embeddedImageRegexp = /data:(.*?);base64,/;
51-
return embeddedImageRegexp.test(path);
28+
var embeddedRegexp = /data:(.*?);base64,/;
29+
return embeddedRegexp.test(path);
5230
}
5331

5432
module.exports.getAbsolutePath = getAbsolutePath;
5533
module.exports.pathToUnixFormat = pathToUnixFormat;
5634
module.exports.isEmbeddedSource = isEmbeddedSource;
57-
module.exports.trimUrl = trimUrl;
5835
module.exports.trimFilename = trimFilename;

0 commit comments

Comments
 (0)