-
-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
55 additions
and
14 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 @@ | ||
function startsWith(string, searchString) { | ||
var stringLength = string.length; | ||
var searchLength = searchString.length; | ||
|
||
// early out if the search length is greater than the search string | ||
if(searchLength > stringLength) { | ||
return false; | ||
} | ||
var index = -1; | ||
while(++index < searchLength) { | ||
if(string.charCodeAt(index) !== searchString.charCodeAt(index)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
module.exports = function createResolver(alias) { | ||
if(typeof alias !== "object" || Array.isArray(alias)) { | ||
return function(url) { return url }; | ||
} | ||
|
||
alias = Object.keys(alias).map(function(key) { | ||
var onlyModule = false; | ||
var obj = alias[key]; | ||
if(/\$$/.test(key)) { | ||
onlyModule = true; | ||
key = key.substr(0, key.length - 1); | ||
} | ||
if(typeof obj === "string") { | ||
obj = { | ||
alias: obj | ||
}; | ||
} | ||
obj = Object.assign({ | ||
name: key, | ||
onlyModule: onlyModule | ||
}, obj); | ||
return obj; | ||
}); | ||
|
||
return function(url) { | ||
alias.forEach(function(obj) { | ||
var name = obj.name; | ||
if(url === name || (!obj.onlyModule && startsWith(url, name + "/"))) { | ||
url = obj.alias + url.substr(name.length); | ||
} | ||
}); | ||
return url; | ||
} | ||
} |
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
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