Skip to content

Commit 98e7f77

Browse files
authored
Merge pull request chavyleung#201 from ipuppet/master
更简洁的getPath()
2 parents 5d88d9c + 35b44d6 commit 98e7f77

2 files changed

Lines changed: 10 additions & 17 deletions

File tree

‎box/chavy.boxjs.js‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,11 @@ $.ver = 'https://gitee.com/chavyleung/scripts/raw/master/box/release/box.release
7777
* http://boxjs.com/api/getdata => `/api/getdata`
7878
*/
7979
function getPath(url) {
80-
// 如果以`/`结尾, 先去掉最后一个`/`
81-
const fullpath = /\/$/.test(url) ? url.replace(/\/$/, '') : url
82-
const domain = getDomain(url)
83-
return new RegExp(domain).test(url) ? fullpath.split(domain)[1] : undefined
84-
}
85-
86-
/**
87-
* http://boxjs.com/ => `boxjs.com`
88-
* http://127.0.0.1:9999/ => `127.0.0.1:9999`
89-
*/
90-
function getDomain(url) {
91-
const [, domain] = /https?:\/\/(.*?)\/.*/.exec(url)
92-
return domain
80+
// 如果以`/`结尾, 去掉最后一个`/`
81+
let end = url.lastIndexOf("/") === url.length - 1 ? -1 : undefined
82+
// slice第二个参数传 undefined 会直接截到最后
83+
// indexOf第二个参数用来跳过前面的 "https://"
84+
return url.slice(url.indexOf("/", 8), end)
9385
}
9486

9587
/**

‎chavy.box.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ $.html = $.name
6969
* https://dns.google/api => `/api`
7070
*/
7171
function getPath(url) {
72-
// 如果以`/`结尾, 先去掉最后一个`/`
73-
const fullpath = /\/$/.test(url) ? url.replace(/\/$/, '') : url
74-
const [, domain] = /https?:\/\/(.*?)\/.*/.exec($request.url)
75-
return new RegExp(domain).test(url) ? fullpath.split(domain)[1] : undefined
72+
// 如果以`/`结尾, 去掉最后一个`/`
73+
let end = url.lastIndexOf("/") === url.length - 1 ? -1 : undefined
74+
// slice第二个参数传 undefined 会直接截到最后
75+
// indexOf第二个参数用来跳过前面的 "https://"
76+
return url.slice(url.indexOf("/", 8), end)
7677
}
7778

7879
function getSystemCfgs() {

0 commit comments

Comments
 (0)