Skip to content

Commit 35b44d6

Browse files
committed
更简洁的getPath()
1 parent 6e9a39e commit 35b44d6

1 file changed

Lines changed: 5 additions & 13 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
/**

0 commit comments

Comments
 (0)