Created
October 21, 2017 07:11
-
-
Save otakustay/1ba51604640ad8747e3599d16cc66c74 to your computer and use it in GitHub Desktop.
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
const pathToRegExp = require('path-to-regexp'); | |
const repo = { | |
index: pathToRegExp('/repos/:organization/:dir/:name'), | |
blob: pathToRegExp('/repos/:organization/:dir/:name/blob/:path+') | |
}; | |
const legacyRepo = { | |
index: pathToRegExp('/repos/:organization/:dir/:name/:extra+'), | |
blob: pathToRegExp('/repos/:organization/:dir/:name/:extra+/blob/:path+') | |
}; | |
// 所有非三级结构的库中不会含有tree和blob这2个关键字,也就是说从第4级开始是不会有blob或者tree的, | |
// 那么将前3级写死,后面部分全部给extra就能保证/:extra+/blob和/:extra+/tree的匹配没有任何歧义 | |
console.log(repo.index.exec('/repos/baidu/icode/frontend-v2')); | |
console.log(repo.blob.exec('/repos/baidu/icode/frontend-v2/blob/src/common/components/index.js')); | |
console.log(legacyRepo.index.exec('/repos/asop/android/platform/native/bridge')); | |
console.log(legacyRepo.blob.exec('/repos/asop/android/platform/native/bridge/blob/src/Main.java')); | |
// 当前存在第3级是tree或blob的库 | |
console.log(repo.index.exec('/repos/baidu/zhangdi07/blob')); // 能匹配 | |
console.log(repo.blob.exec('/repos/baidu/zhangdi07/blob/blob/src/Main.java')); // 能匹配 | |
console.log(legacyRepo.index.exec('/repos/baidu/zhangdi07/blob')); // 不应该匹配 | |
console.log(legacyRepo.blob.exec('/repos/baidu/zhangdi07/blob/blob/src/Main.java')); // 不应该匹配 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment